News for hackers. Stuff that matters.
Re:Checked C by Animats on Thursday December 22, 2022 @01:31AM (#34090479)
Many people have been down this road, including me.[1] Back in my 2012 paper, I listed prior art:

• Microsoft source code annotation language (SAL)

• Cyclone (AT&T research language)

• SCC, the Safe C compiler.

• Ccured

• MemSafe

The breakthrough will come when someone can create an automated system to convert unsafe C into safe something. AI may be far enough along to do that now. Every array in a valid C program has a size, defined by some expression known to the programmer. All you have to do is find that expression and tell the language about it. Most of the time this is easy. Sometimes it's hard. Sometimes it's impossible, in which case the program is probably a buffer overflow waiting to happen.

[1] http://animats.com/papers/languages/safearraysforc43.pdf

Re:Checked C by 0xbadcafebee on Thursday December 22, 2022 @02:31AM (#34090798)
> an automated system to convert unsafe C into safe something

Can't we just make macros to redefine types as some 'safe' type variant system, and #include "safetypes.h" or something? It's ugly, but searching your source code for 'int a[5]' and replacing it with 'INT(a,5)' might be dumb enough to work.

Re:Checked C by friendzis on Thursday December 22, 2022 @03:20AM (#34091055)
Statically allocated/sized arrays are relatively easy. The problem is dynamically allocated/sized arrays coupled with pointer "interface", which supports type casting.
Re:Checked C by Animats on Thursday December 22, 2022 @05:22PM (#34099662)
Something smart enough to convert pointer arithmetic to slice expressions is needed. Most C pointer arithmetic is really just slice access.

(Who invented array slices? It was a concept that came along much later than one might have expected.)

Re:Checked C by musicale on Thursday December 22, 2022 @03:43AM (#34091178)
What about Fail-Safe C [1] and SoftBound [2]:

[1] https://staff.aist.go.jp/y.oiwa/FailSafeC/index-en.html

[2] https://people.cs.rutgers.edu/~sn349/softbound/

The former apparently implements full ANSI C, and the latter apparently has a memory safety proof in Coq.

The group that did [1] also did an ANSI C to Java compiler.

It seems to me that we could really use a safe ABI (x86_64_safe, etc.) for C/C++, and that it should be supported by clang and gcc.

Maybe CHERI will catch on someday...

Re:Checked C by kinggencha on Thursday December 22, 2022 @03:48AM (#34091199)
I can already see it:

> This C code is guaranteed to be safe. We had an AI go through it and make it safe.

No, thanks. That sounds more like a problem than a solution.

Re:Checked C by mwhicks1 on Thursday December 22, 2022 @08:17AM (#34092930)
> The breakthrough will come when someone can create an automated system to convert unsafe C into safe something

https://dl.acm.org/doi/abs/10.1145/3527322

Re:Checked C by Animats on Thursday December 22, 2022 @01:15PM (#34096500)
Now that's real progress.

"We find that typ3c automatically converted 67.9% of pointers in our benchmark programs to checked types, which improves on the 48.4% inferred by unification-style algorithms used in prior work. boun3c was able to infer bounds for 77.3% of pointers that required them."

That's reasonably good. The real goal is to convert all pointers to either run-time checked pointers (slower) or compile-time checked pointers. It's already possible to convert everything to "fat pointers". GCC used to have that as an option. But the goal is to eliminate the need for that for code that's frequently executed. That is, inner loops. It's good to see activity in this area.

The output code is kind of clunky looking, but that could be fixed.