News for hackers. Stuff that matters.
Re:Imposing memory security in C [video] by blacksqr on Thursday February 27, 2025 @02:40PM (#43197685)
There are and have been many techniques and projects for making C more memory-safe. The crucial question it always comes down to is what performance hit do you take using them?

That's why C has been on top for so long. Seat-of-the-pants hand-crafted C has always been the fastest high-level language.

Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @02:49PM (#43197772)
I found that C programs rarely evolve beyond their initial design. The trouble is, it's hard to refactor C programs. For example,

    struct S { int a; };
    struct S s; s.a = 3;
    struct S *p; p->a = 3;
I.e. a . is for direct access, -> for indirect access. Let's say you want to change passing S by value to passing S by pointer. Now you have to update every use, instead of just the declaration.

This is how it would work in D:

    struct S { int a; }
    S s; s.a = 3;
    S* p; p.a = 3;
    ref S q; q.a = 3;
And so refactoring becomes much easier, and so happens more often.
Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @02:57PM (#43197832)
> C has always been the fastest high-level language.

C has another big speed problem. Strings are 0 terminated, rather than length terminated. This means constant scanning of strings to find their length. Even worse, the scanning of the string reloads the cache with the string contents, which is pretty bad for performance.

Of course, you could use `struct String { char *p; size_t length; };` but since every library you want to connect to uses 0 terminated strings, you're out on your island all alone, so pragmatically it does not work.

Another speed-destroying problem with C strings is you cannot take a substring that does not require allocating a new string and then copying the data. (Unless the substring is right-justified.) This is not fast in any universe.

D uses length-denoted strings as a basic data type, and with string processing code, it is much faster than C. Substrings are quick and easy. You can still interface with C because D string literals implicitly convert to C string literals, as the literals are 0 terminated. So this works in D:

    printf("hello world!\n");
(People sometimes rag on me for still using printf, but printf is the most optimized and debugged library function in the world, so I take advantage!)
Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @03:09PM (#43197932)
Yeah totally.

It's a perfect example of C being optimized for simple mapping onto linear memory, rather than some kind of performance optimum

Re:Imposing memory security in C [video] by Dylan16807 on Friday February 28, 2025 @09:35AM (#43206066)
I don't know, a pointer with a size is also quite simple.
Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @03:01PM (#43197873)
C's memory safety could be drastically improved with the addition of bounds-checked arrays (which is an extension, and does not change existing code):

https://www.digitalmars.com/articles/C-biggest-mistake.html

25 years of experience with D has shown this to be a huge improvement.

D also has references as an alternative to pointers. References cannot have arithmetic done on them. Hence, by replacing pointers with references, and with array bounds checking, the incidence of memory corruption is hugely reduced.

Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @03:06PM (#43197908)
> C's memory safety could be drastically improved with the addition of bounds-checked arrays (which is an extension, and does not change existing code):

If you solved that problem then you'd still have a dumpster fire of memory safety issues from bad casts, use after free, etc

Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @03:48PM (#43198347)
Array overflows is consistently the number one memory safety bug in shipped code, by a wide margin.
Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @04:00PM (#43198464)
Citation needed.

(I would have guessed similar to what you said, minus the "by a wide margin" bit.)

Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @04:10PM (#43198543)
Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @04:46PM (#43198858)
Neither of those support your claim that buffer overflows are the top issue by a wide margin.

If you are saying that “most memory safety issues are bounds related” then I agree. I’m just disagreeing on the wide margin bit.

Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @06:50PM (#43199948)
> Neither of those support your claim that buffer overflows are the top issue by a wide margin

That's true, but I have seen that statistic more than once, and decided that I wasn't going to spend more time searching for it.

If that's not good enough for you, so be it.

Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @02:53PM (#43197801)
> There are and have been many techniques and projects for making C more memory-safe.

Sort of. None of them got all the way to safety, or they never got all the way to compatibility with C.

Fil-C is novel in that it achieves both safety and compatibility.

> The crucial question it always comes down to is what performance hit do you take using them?

Is that really the crucial question?

I don't think you would have even gotten to asking that question with most attempts to make C memory safe, because they involved experimental academic compilers that could only compile a subset of the language and only worked for a tiny corpus of benchmarks.

Lots of C/C++ code is not written with a perf mindset. Most of the UNIX utilities are like that, for example.

> That's why C has been on top for so long. Seat-of-the-pants hand-crafted C has always been the fastest high-level language.

I don't think that's the reason. C rose to where it is today even when it was much slower than assembly. C was slower than FORTRAN for a long time (maybe still is?) but people preferred C over FORTRAN anyway.

C's biggest superpower is how easy it makes it to talk to system ABI (syscalls, dynamic linking, etc).

Re:Imposing memory security in C [video] by blacksqr on Thursday February 27, 2025 @03:58PM (#43198441)
>> There are and have been many techniques and projects for making C more memory-safe.

> Sort of.

Yes. That's why I used the qualifier "more." Our statements are not in conflict.

> Fil-C is novel in that it achieves both safety and compatibility.

How does it affect performance?

>> The crucial question it always comes down to is what performance hit do you take using them?

> Is that really the crucial question?

Yes, because it's the factor that industry leaders use to decide on which language to use. For example, Apple switching from Pascal to C way back in the Stone Age. The fact that it's the crucial question doesn't mean that lots of people don't consider other factors for their own reasons.

> I don't think you would have even gotten to asking that question with most attempts to make C memory safe.

Yes, most. But for example, Microsoft's Checked C comes with a performance penalty of almost 10% for a partial solution. Not academic. Very commercial.

> C rose to where it is today even when it was much slower than assembly

Yes, that's why I said "high-level language." I don't consider assembly high-level, do you?

> people preferred C over FORTRAN anyway

People preferred C in the 1970s/80s because at the time you could allocate memory dynamically in C but not in FORTRAN. FORTRAN fixed that in the 1990s, but by then there were too few FORTRAN programmers to compete. Since then C has serially defeated all newcomers. Maybe Go or Rust are poised to take it on. When a major operating system switches from C, we'll know.

Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @04:08PM (#43198524)
> How does it affect performance?

Right now, 1.5x-5x, but considering how many optimizations I know I can do but haven't done yet, I think those numbers are an upper bound.

> Yes, because it's the factor that industry leaders use to decide on which language to use. For example, Apple switching from Pascal to C way back in the Stone Age. The fact that it's the crucial question doesn't mean that lots of people don't consider other factors for their own reasons.

I don't think this is true at all, sorry. Top reason for using C/C++ is inertia. If you've got a pile of C code, then you'll keep writing C.

> Yes, most. But for example, Microsoft's Checked C comes with a performance penalty of almost 10% for a partial solution.

Checked C didn't make C memory safe, so I don't think it's interesting.

> Yes, that's why I said "high-level language." I don't consider assembly high-level, do you?

No, I don't consider assembly to be high-level. The point is: serious engineers don't just blindly reach for the fastest programming language. They'll take slow downs if it makes them more productive. Happens all the time.

> People preferred C in the 1970s/80s because at the time you could allocate memory dynamically in C but not in FORTRAN. FORTRAN fixed that in the 1990s, but by then there were too few FORTRAN programmers to compete. Since then C has serially defeated all newcomers. Maybe Go or Rust are poised to take it on. When a major operating system switches from C, we'll know.

The last time I saw a benchmark of FORTRAN beating C was the early 2000's. FORTRAN is much easier to optimize and compile.

C is great for writing operating systems because C has the right abstractions, such as the abstractions necessary for doing dynamic linking and basically any kind of ABI compatibility. Rust and Go don't have that today. Even C++ is worse than C in this regard. Swift has ABI, but it took heroic efforts to get there.

C didn't eat the world because of its stellar performance. It's the other way around. C has stellar performance because it ate the world, and then the industry had no choice but to make it fast.