|
|
Log in / Subscribe / Register

Memory-safe C code

Memory-safe C code

Posted Nov 21, 2025 12:19 UTC (Fri) by khim (subscriber, #9252)
In reply to: Memory-safe C code by anton
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

> There were several C implementations that caught bounds violations presented at the poster session of PLDI 2002 (probably elsewhere, too, but I was involved in that poster session, so I remember that).

I'm, mostly, ignoring bound violations here: these were implemented in ALGOL 68, more than half-century ago, why are they even wort talking about? There were actually dropped out when ALGOL ideas were adopted into BCPL to create C.

Their reintroduction is a welcome change, but not ground-breaking at all.

The interesting part, as I wrote many time, is memory safety and the associated tracking of pointer validity.

Sorry if I was unclear, but I wrote that more than once and couldn't repeat that in every message here.

> Somehow that did not catch on, but in recent years I have read about similar things, this time including protection against dangling pointers and double free, with even fatter pointers.

That's different from what we are discussing here, though: Rust does things in compile time, isn't it? For the C variant to be ground-breaking it needs that, too.

Of course C that may achieve safety with fatter pointers is nice thing, obviously… just not ground-breaking.

> After the research project ended (AFAIK in 2009 or so), nobody productized this work, so apparently it was not "very interesting" to the C programmers at the time.

Obviously. C programmers were secure in their knowledge that they are indispensable… only when Rust have grown to the point where there are more new Rust code in Android they started thinking about doing something. It's typical situation with disruptive innovations.

> I still think it's saner to add some annotations to a C program than to rewrite it in Rust, but apparently there is some cultural hurdle that cannot be taken: Some programmers welcome the checking, but they don't program in C, not even in deputized C; other programmers program in C, but they are not interested in the features that Ivy/Deputy or the fat-pointer C implementations offer. And it seems to me that this is just as true now as it was 20 or 10 years ago.

It's the same story with all disruptive innovations. Story, typically, happens like this:

  1. Year Y0: disruptive innovation happen, but couldn't be immediately adapted by users of “old tech” because there are lots of problems with it. Adoptions happens in the “new industry”, instead.
  2. Year Y0 + N: two things are happening:
    • “New teach” is improved enough to start being used in places where “old tech” is dominating.
    • “Old tech” developers wake up and start new project to bring “old tech” to the parity with “new tech”.
  3. Year Y0 + 2*N: two things are happening:
    • “New tech” is adopted by almost everyone (90% of developers either use it or would never adopt new innovations at all)
    • “Old tech” have grown to the point where it's on the parity with “new tech”… but nobody cares anymore.

See the problem? Disruption (and, often, destruction of “old tech”) happening not because it's not possible to extend the “old teach”, but because between stages #1 and #2 users of “old teach” are not interested (and thus technology is not developed and not adopted), while between stages #2 and #3 they are interested and they already have they way forward: adopt the “new teach”.

Here, with Rust:

  1. Year 2015: Rust appears, but have too many deficiencies to be used in all places where C is used — no inline assembler, borrow checker is extremely painful, etc.
  2. Year 2025:
    • Rust is growing like a weed.
    • C people take notice and start reforming C.
  3. Year 2035 (prediction):
    • Rust is adopted almost everywhere.
    • C got C35, developers of C35 have finally made something as good as Rust… and nobody cares anymore.


to post comments

Never "as good as" Rust

Posted Nov 26, 2025 5:11 UTC (Wed) by gmatht (guest, #58961) [Link] (2 responses)

C is already BETTER at targeting embedded devices with only 4K of RAM. Gcc is better at compiling legacy code from the previous century, and small snippets of code that directly interfaces with this legacy code.

Given that there are always trade-offs, it seems a safe bet that C35 will still be better at things C is good at and not as good at things Rust is good at. There would be no point in turning C into just another Rust.

Never "as good as" Rust

Posted Nov 26, 2025 7:13 UTC (Wed) by mb (subscriber, #50428) [Link] (1 responses)

>C is already BETTER at targeting embedded devices with only 4K of RAM

Why?
I do use Rust on an AtTiny861a with 512 bytes of RAM.

There are a couple of quirks in the current experimental compiler implementation.
But there's nothing in Rust as the language itself that would make it incapable or even worse than C on tiny platforms.
The last thing that is missing in the language itself is in place initializations, which we will get in the near future. But I can live without them for the time being.

Never "as good as" Rust

Posted Nov 26, 2025 12:13 UTC (Wed) by Wol (subscriber, #4433) [Link]

> >C is already BETTER at targeting embedded devices with only 4K of RAM

> Why?

And Forth is (allegedly) better still.

So that's no argument against Rust. If it was, embedded developers wouldn't be using C.

Cheers,
Wol

Memory-safe C code

Posted Dec 2, 2025 11:02 UTC (Tue) by anton (subscriber, #25547) [Link] (1 responses)

Bounds checking is worth talking about, because Algol 60 (which usually was implemented with bounds checking) and Algol 68 failed to catch on (at least to the same degree as C), while C (which passes arrays as pointers without size information) caught on and a lot of code is written in C. Interestingly, in the Linux man-pages 6.03 I see things in the spirit of Deputy, like
void *memmove(void dest[.n], const void src[.n], size_t n);
which indicates how n relates to the passed pointers. Is this a C23 innovation? In any case, some Deputy-like features are finding the way into C, 15 years later.

There are no invalid pointers in Lisp (released 1960) and many later languages (e.g., Java), so why are you talking about that? Because invalid pointers in C code are a problem, and there is lots of C code around.

Concerning compile-time vs. run-time checking, Ivy/Deputy is like Rust: Checking at run time what cannot be checked at compile time. Maybe Ivy/Deputy cannot do all the things that Rust can do, but that would be just an issue of adding the additional features to the safe C variant.

A bigger problem in that context is probably that in some cases you would either stay with unsafe C in significant pieces of code or have to do thinks like Rust does with Rc or Arc (run-time reference counting and checking), a cost that C programmers that think that their code is correct do not want to take, so they will leave the C code for handling the data structure unsafe. Still, I think that in much code, lots of code could be converted to such a safe C variant with less effort than rewriting it in Rust.

Memory-safe C code

Posted Dec 2, 2025 11:17 UTC (Tue) by alx.manpages (subscriber, #145117) [Link]

> Interestingly, in the Linux man-pages 6.03 I see things in the spirit of Deputy, like
>
> void *memmove(void dest[.n], const void src[.n], size_t n);
>
> which indicates how n relates to the passed pointers. Is this a C23 innovation?

C has no such syntax. It was sugar used in the manual pages for documentation purposes.

I used that syntax for a few versions. It's a syntax that was discussed for some time (including in GCC and the C Committee), but we found out it wasn't great. In C, you can't use stuff before declaring it (we had implicit declarations in K&R C, but we got rid of those for good). It can cause cyclic dependencies, etc.

More recently, I've changed the Linux man-pages to use a syntax that is an old GNU extension:

wchar_t *
wmemcpy(size_t n;
wchar_t dest[restrict n], const wchar_t src[restrict n], size_t n);

This is perfectly valid C code for GCC. It looks a bit redundant, but it's okay, because the compiler verifies everything, and it's difficult to make bugs using it.

In the case of memcpy(3), the latest version of the manual pages has

void *
memcpy(size_t n;
void dest[restrict n], const void src[restrict n], size_t n);

which isn't valid because arrays of void are not valid. However, everything else is valid GNU C. I decided to use this sugar with void, for having more useful documentation.

See also: <https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html>.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds