News for hackers. Stuff that matters.
Re:DARPA: Translating All C to Rust (Tractor) by childintime on Tuesday July 30, 2024 @12:24PM (#41110923)
Hard for humans. But it's DARPA, is it hard for AI? Image classification used to be hard also, today cars drive themselves.

I'd say it's good timing.

Re:DARPA: Translating All C to Rust (Tractor) by Calavar on Tuesday July 30, 2024 @12:36PM (#41111091)
> today cars drive themselves

You can attach about a hundred asterisks to that.

If anything, I think self the failure to hit L5 driving after billions of dollars and millions of man hours invested is probably reflective of how automatic C to Rust translation will go. We'll cruise 90% of the way, but the last 10% will prove insurmountable with current technology.

Think about the number of C programs in the wild that rely on compiler-specific or libc-specific or platform-specific behavior, or even undefined behavior plus the dumb luck of a certain brittle combination of {compiler version} ∩ {libc version} ∩ {linker version} ∩ {build flags} emitting workable machine code. There's a huge chunk of C software where there's not enough context within the source itself (or even source plus build scripts) to understand the behavior. It's not even clear that this is a solvable problem in the abstract.

None of that is to say that DARPA shouldn't fund this. Research isn't always about finding an industrial strength end product; the knowledge and expertise gained along the way is important too.

Re:DARPA: Translating All C to Rust (Tractor) by sqeaky on Tuesday July 30, 2024 @04:16PM (#41113682)
This is the exact formulation of the argument before computers beat humans at chess, or drew pictures, or represented color correctly, or... Self driving cars will be solved. There is at least one general purpose computer that can solve it already (a human brain), so of a purpose built computer can also be made to solve it.

In 10 (or 2 or 50 or X) years when Chevy, Ford, and others are rolling out cheap self driving this argument stops working. The important thing is that this argument stops working with no change in how hard C to Rust conversion is.

We really should be looking at the specifics of both problems. What makes computer language translation hard? Why is driving hard? One needs to be correct while inferring intent and possibly reformulating code to meet new restrictions. The other needs to be able to make snap judgments and in realtime avoid hitting things even if it just means stopping to prefer safety over motion. One problem can be solved piecewise without significant regard to time and the other solved in realtime as it happens without producing unsafe output.

These problems really aren't analogous.

I think you picked self driving cars just because it is a big and only partially solved problem. One could just as easily pick a big solved problem or a big unstarted problem and formulate equally bad arguments.

I am not saying this problem is easy, just that it seems solvable with sufficient effort.

Re:DARPA: Translating All C to Rust (Tractor) by mywittyname on Tuesday July 30, 2024 @04:58PM (#41114078)
> These problems really aren't analogous.

I'd put money on the solutions to said problems looking largely the same though - big ass machine learning models.

My prediction is that a tool like copilot (but specialized to this domain) will do the bulk of source code conversions, with a really smart human coming behind to validate.

Re:DARPA: Translating All C to Rust (Tractor) by fch42 on Thursday August 01, 2024 @02:52AM (#41126736)
With you, except for the conclusion "[ the tool ] will do the bulk of source code conversions, with a really smart human coming behind to validate".

The director orders the use of the tool when the dev team got downsized (and the two most-seniors left for greener pastures just after that). Validation is in the "extensive" tests anyway, we have those, right, so the new intern shall have a look, make it all work (fudge the tests where possible and remove the persistently failing ones as they've probably been always broken). The salesman said it comes from the DOA or DOD or something. If the spooks can do it so can we.

Re:DARPA: Translating All C to Rust (Tractor) by lmm on Tuesday July 30, 2024 @09:27PM (#41115708)
> This is the exact formulation of the argument before computers beat humans at chess, or drew pictures, or represented color correctly, or...

Which are things that took 20 or 50 years longer than expected in some cases.

> I think you picked self driving cars just because it is a big and only partially solved problem. One could just as easily pick a big solved problem or a big unstarted problem and formulate equally bad arguments.

But C to Rust translation is a big and only partially solved problem.

Re:DARPA: Translating All C to Rust (Tractor) by psychoslave on Tuesday July 30, 2024 @01:03PM (#41111474)
Ok, but if it's like 90% of small projects can use it as direct no pain bridge, that can be a huge win.

Even if it's "can handle well 90%" of the transition for any project, this is still interesting. Unlike cars on the road, most code transition project out there doesn't need to be 100% fine to provide some useful value.

Re:DARPA: Translating All C to Rust (Tractor) by 0cf8612b2e1e on Tuesday July 30, 2024 @01:53PM (#41112133)
Even if every project can only be 90% done, that’s a huge win. Best would be if it could just wrap the C equivalent code into an unsafe block which would be automatically triaged for human review.

Just getting something vaguely Rust shaped which can compile is the first step in overcoming the inertia to leave the program in its current language.

Re:DARPA: Translating All C to Rust (Tractor) by swiftcoder on Wednesday July 31, 2024 @05:18AM (#41117559)
c2rust exists today, and pretty much satisfies this. I've used it to convert a few legacy math libraries to unsafe rust, and then been able to do the unsafe->safe refactor in the relative comfort of the full rust toolset (analyser + IDE + tests)

There is real utility in slowly fleshing out the number of transforms in a tool like c2rust that can recognise high-level constructs in C code and produce idiomatic safe equivalents in rust

Re:DARPA: Translating All C to Rust (Tractor) by fch42 on Thursday August 01, 2024 @03:10AM (#41126838)
"real" (large) C/C++ programs get much of their complexity from the fact that it's hundred of "sources" (both compiled and libraries) that sometimes, or even often, share global state and at best use a form of "opportunistic sharing". Global variables are (deliberately, and justifiedly-so) hard in rust, but (too) trivial in C/C++, cross-references / pointer chains / multi-references likewise. And once you enter threading, it becomes even harder to output "good" rust code - you'd have to prove func() is called from threaded code and should in rust best take Arc<> or some such instead of a pointer.

It'll be great for "pure" functions. For the grimey parts of the world, funcs taking pointer args and returning pointers, for things that access and modify global data without locks, for threaded code with implicit (and undocumented) locking, the tool would add most value. If it can. Even only by saying "this code looks grimey. here's why. A bit of FFI will also be thrown in because it links against 100 libraries. I suggest changes along those lines ... use one of the 2000000 hint flags to pick-your-evil".

Re:DARPA: Translating All C to Rust (Tractor) by D-Coder on Tuesday July 30, 2024 @01:28PM (#41111826)
In addition to the other replies, this is a one-time project. After everything (or almost everything) has been translated, you're done, you won't be running into new edge cases.
Re:DARPA: Translating All C to Rust (Tractor) by programd on Tuesday July 30, 2024 @01:10PM (#41111565)
> > today cars drive themselves

> You can attach about a hundred asterisks to that.

Not in San Francisco. There are about 300 Waymo cars safely driving in one of the most difficult urban environments around (think steep hills, fog, construction, crazy traffic, crazy drivers, crazier pedestrians). Five years ago this was "someday" science-fiction. Frankly I trust them much more then human drivers and envision a future utopia where human drivers are banned from urban centers.

To get back on topic, I don't think automatic programming language translation is nearly as hard, especially since we have a deterministic model of the machines it runs on. I can see a possible approach where AI systems take the assembler code of a C++ program, then translate that into Rust, or anything else. Can they get 100% accuracy and bit-for-bit compatibility on output? I would not bet against it.

Re:DARPA: Translating All C to Rust (Tractor) by creata on Tuesday July 30, 2024 @04:03PM (#41113542)
Isn't 100% accuracy (relatively) easy? c2rust already does that, or at least comes close, as far as I know.

Getting identical outputs on safe executions, catching any unsafe behavior (at translation-time or run-time), and producing efficient, maintainable code all at once is a million times harder.

Re:DARPA: Translating All C to Rust (Tractor) by m0llusk on Tuesday July 30, 2024 @04:06PM (#41113564)
Opinions about automated driving systems vary. Just from my own experience doing business all around San Francisco I have seen at least a half dozen instances of Waymo vehicles making unsafe maneuvers. Responders have told me and local government officials that Waymo vehicles frequently fail to acknowledge emergency situations or respond to driving instructions. Driving is a social exercise which requires understanding of a number of abstractions.
Re:DARPA: Translating All C to Rust (Tractor) by fragmede on Wednesday July 31, 2024 @12:39AM (#41116440)
they're not perfect, sure, but they're out there, just driving around all autonomously and all, contrary to GGP's assertion that they don't exist.
Re:DARPA: Translating All C to Rust (Tractor) by cuu508 on Wednesday July 31, 2024 @01:47AM (#41116658)
GGGP talked about L5 self-driving, isn't Waymo L4?
Re:DARPA: Translating All C to Rust (Tractor) by saagarjha on Tuesday July 30, 2024 @05:10PM (#41114173)
San Francisco, for all its challenges, mostly has traffic laws that people follow. This is not true throughout the world.
Re:DARPA: Translating All C to Rust (Tractor) by TestingWithEdd on Wednesday July 31, 2024 @12:58AM (#41116498)
Limited to specific areas during specific hours, and have caused crashes (at least when I lived there till last summer).
Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Tuesday July 30, 2024 @12:32PM (#41111030)
Well, Claude 3.5 can do translation from one language to another in a fairly competent manner if the languages are close enough. I've used it for that task myself with success (Java -> JavaScript).

But, this isn't just about rewriting code from one language to another. It's about reverse engineering complex information out of the code, which may not be immediately visible in it, and then finding a way to make it "safe" according to Rust's type system. Where's the training data for that? It'd be really hard even for skilled humans.

Personally I think the most pragmatic way to make C/C++ memory safe quicker is one of two approaches:

1. Incrementally. Make std::vector[] properly bounds checked (still not done even in chrome!), convert allocations to allocations that know their own size and do bounds checking e.g. https://issues.chromium.org/issues/40285824

2. Or, go the whole hog and use runtime techniques like garbage collection and runtime bounds checks.

A good example of approach (2) is Managed Sulong, which extends the JVM to execute LLVM bitcode directly whilst exposing to the C/C++/FORTRAN a virtualized Linux syscall interface. The whole piece of code can be sandboxed with permissions, and memory safety errors are caught at runtime. The compiler tries to optimize out as many bounds checks as possible. The interesting thing about this approach is it doesn't require big changes to the source code (as long as it's already been ported to Linux), which means the work of making something safe can be done by teams independent of the original authors. In practice "rewrite it in Rust" will usually mean a fork, which introduces lots of complicated technical, cultural and economic issues.

Managed Sulong is also a research project and has a bunch of problems to solve, for instance it needs to lose the JITC dependency and go fully AOT compiled (doable, there's no theoretical issue with it and much of the needed infra already exists). And performance/memory usage can always be improved of course, it regresses vs the original C. But those are "just" systems engineering problems, not rewrite-the-world and solve-static-analysis problems.

Disclosure: I do work part time at Oracle Labs which developed Managed Sulong, but I don't work on it.

Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @12:36PM (#41111088)
std::vector [] has had bounds checking since forever if you set the correct compiler flag. Since they aren't using it this is a choice, presumably they prefer the speed gain.
Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Tuesday July 30, 2024 @12:43PM (#41111167)
You mean _GLIBCXX_DEBUG? It's got some issues. Linux only, it doesn't always work [1] and it's all or nothing. What's really needed is the ability to selectively opt-out on a per-instantiation level so very hot paths can keep the needed performance whilst all the rest gets opted into safety checks.

Microsoft has this:

https://learn.microsoft.com/en-us/cpp/standard-library/safe-...

but it doesn't seem to actually make std::vector[] safe.

It's frustrating that low hanging fruit like this doesn't get harvested.

[1] "although there are precondition checks for some string operations, e.g. operator[], they will not always be run when using the char and wchar_t specializations (std::string and std::wstring)."

Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @08:36PM (#41115471)
With MSVC you can use _CONTAINER_DEBUG_LEVEL=1 to get a fast bounds check that can be used in release builds. Or just use it in development to catch errors.
Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Wednesday July 31, 2024 @03:58AM (#41117205)
Interesting thanks. Seems the reason I couldn't find anything on that is because it's internal only and not a feature you're actually meant to use?

https://github.com/microsoft/STL/issues/586

> We talked about this at the weekly maintainer meeting and decided that we're not comfortable enough with the (lack of) design of this feature to begin documenting it for wide usage.

Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:05AM (#41118463)
What you want should be _ITERATOR_DEBUG_LEVEL instead, that is the public macro for bounds checking configuration.
Re:DARPA: Translating All C to Rust (Tractor) by Calavar on Tuesday July 30, 2024 @12:51PM (#41111293)
As far as I am aware, the standard doesn't mandate bounds checking for std::vector::operator[] and probably never will for backwards compatibility reasons. Most standard library implementations have opt-out std::vector[] bounds checking in unoptimized builds, but not in optimized builds.

I tried a toy example with GCC [1], Clang [2], and MSVC [3], and none of them emit bounds checks with basic optimization flags.

[1] https://godbolt.org/z/W5e3n5oWM

[2] https://godbolt.org/z/Pe8nPPvEd

[3] https://godbolt.org/z/YTdv3nabn

Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @08:38PM (#41115482)
As I said you need the correct flag set.. MSVC use _CONTAINER_DEBUG_LEVEL=1 and it can be used in release. They have had this feature since 2010 or so, though the flag name has changed.
Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:06AM (#41118474)
The correct name is _ITERATOR_DEBUG_LEVEL.
Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:09AM (#41118496)
Add a "#define _ITERATOR_DEBUG_LEVEL 1" on top for VC++.
Re:DARPA: Translating All C to Rust (Tractor) by Animats on Tuesday July 30, 2024 @04:29PM (#41113805)
> But, this isn't just about rewriting code from one language to another. It's about reverse engineering complex information out of the code, which may not be immediately visible in it, and then finding a way to make it "safe" according to Rust's type system. Where's the training data for that? It'd be really hard even for skilled humans.

That might not be too bad.

A combination of a formal system and an LLM might work here. Suppose we see a C function

   void somefn(char* buf, int n);
First question: is "buf" a pointer to an array, or a pointer to a single char? That can be answered by looking at what the function does with "buf", and what callers pass to it.

If it's an array, how big is it? We don't have enough info to know that yet. But a reasonable guess, and one than an LLM might make, is that the length of buf is "n".

Following that assumption, it's reasonable to translate this to Rust as

   fn somefn(buf: &[u8])
and, if n is needed within the function, use

   buf.len()
The next step is to validate that guess. The run-time approach is to write all calls to "somefn" with

   assert!(buf.len() == n);
   somefn(buf, n);
Maybe formal methods can prove the assert true, and we can take it out. Or if a SAT solver or a fuzz tester can generate a counterexample, we know that the guess was wrong and this has to be done the hard way, as

   fn somefn(buf: &[u8], int n)
implying more subscript checks inside "somefn".

The idea is to recognize common C idioms and do clean translations to Rust for them. This should handle a high percentage of cases.

Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Wednesday July 31, 2024 @03:56AM (#41117199)
Yes, this is similar to what IntelliJ does for Java->Kotlin. Do a first pass that's extremely non-idiomatic and mechanical, then do lots of automated refactoring to bring it closer to idiomatic.

But if you're going to do it that way, the right place to start is probably to a safer form of C++ not Rust. That way code can be ported file-at-a-time or even function-at-a-time, and so you'll have a chance to run the assertions in the context of the original code. Which of course may not have good test coverage, as C codebases often don't, so you'll have to be testing your assertions in production.

Re:DARPA: Translating All C to Rust (Tractor) by Animats on Wednesday July 31, 2024 @01:23PM (#41121157)
> But if you're going to do it that way, the right place to start is probably to a safer form of C++ not Rust.

There's something to be said for that. You're going to need at least an internal representation that's a safe C/C++.

Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @02:38AM (#41116866)
> Make std::vector[] properly bounds checked

Most compilers do have flags to turn this on, which I use all the time.

The issue is the "performance trumps safety" culture that pushes back against using them.

Re:DARPA: Translating All C to Rust (Tractor) by eesmith on Tuesday July 30, 2024 @01:27PM (#41111808)
As a reminder, DARPA funded self-driving car research since at least the 1980s with the Autonomous Land driven Vehicle (ALV) project, plus the DARPA Grand Challenges, and more.
Re:DARPA: Translating All C to Rust (Tractor) by childintime on Saturday August 03, 2024 @05:34AM (#41145572)
In my experience claude.ai has near perfect grasp of what a program (that fits its window) is written to do. It can already make a program in another language that can do the same. What this means is that the cost of a full rewrite is going to come down dramatically over the next few years.

This is an excellent example of government action I like to see, as it isn't about favoritism or the swamp dynamics. Just provide a target, a bounty and no or low barriers to entry.

This challenge does push Rust out in front of everybody. That's a mixed blessing. I hope this challenge gets modified to not specify the target language, but instead the requirement of memory and type safety. Rust is likely an intermediate stop on the way to something better, and it shouldn't matter if that language is called Rust 2.0 or something else.