92 points · by arunc · 11y ago · 65 comments
For example "use of an unitialized variable". Given that the compiler already has to analyze the program flow to determine if the variable could be use unitialized to output that warning, the optimization step to not initialize those variables that are written to before used doesn't seem so far away. This happens at compile time.
But, there's also the fact that a modern OS gives you heap memory zeroed mostly for free. A call to calloc is highly optimized nowadays. It needs to be for security reasons, as a program must never receive memory with data left from a different process.
Likewise the signed integer overflow check. Modern CPUs are so superscalar internally that an overflow check (which doesn't get flagged most of the time anyway) doesn't add anything to the execution speed.
And finally we are able to use that without resorting to assembler. GCC 5 has added support for built-in functions for arithmetics with overflow checking.
Memory often is coming from previously-freed heap space within the process. There is no security need to zero such memory. Zeroing it wouldn't even help, because the process is free to read the memory before malloc returns it.
And calloc, no matter how optimized it is, will still be more expensive than malloc which doesn't have to zero the memory.
> Modern CPUs are so superscalar internally that an overflow check (which doesn't get flagged most of the time anyway) doesn't add anything to the execution speed.
"Doesn't add anything" is not correct. These checks are not free:
The danluu link doesn't really support your argument. Yes, the checks aren't free, but they aren't very costly either in practice, and much of the cost is down to C compilers not being optimized for generating range-checking code.
I feel you're also possibly missing a longer term feedback loop issue. When software starts relying on things like overflow checks and zeroed memory, hardware manufacturers like Intel start building and optimizing hardware to make them faster.
The "prefer performance over correctness" bias that's existed in the C world has been responsible for a lot of damage. We need to get out of it.
It must be nice living a world where energy is free, you have unlimited cores and unbounded caches, and context switches, thread synchronisation and cache coherence have no cost or latency.
I'm not saying that reducing the number of cases with undefined behaviour isn't worth it in most cases, but claiming that you can just zero application memory at virtually no cost on a multicore is misguided.
If your plan is to use other threads to zero the memory, then your malloc() implementation needs to move every freed region into a thread-safe queue to be zero'd and wait for the other thread to pick it up before the memory is eligible to be allocated again. This adds synchronization costs, creates opportunities for false sharing, and introduces starvation issues.
Also, there are applications that genuinely peg all CPUs already. In these cases, this approach makes even less sense because there is no spare CPU sitting around, so you pay the overhead for no actual parallelism.
> When software starts relying on things like overflow checks and zeroed memory, hardware manufacturers like Intel start building and optimizing hardware to make them faster.
I'm pretty sure Intel already has plenty of incentive to make memset(ptr, 0, n) fast. Also I'm not sure how overflow checks could be made much faster, except maybe to create an entirely separate set of integer instructions that raise hardware exceptions on overflow. Maybe that would be worthwhile, I don't know.
> The "prefer performance over correctness" bias that's existed in the C world has been responsible for a lot of damage. We need to get out of it.
The "correctness checks are free because we want them to be" bias that exists in other languages has been responsible for a lot of languages and runtimes that are "nice" but that no one could actually write a competitive web browser (for example) in.
http://randomascii.wordpress.com/2014/12/10/hidden-costs-of-...
> "Doesn't add anything" is not correct. These checks are not free:
This statement was in regard to what happens inside the CPU. What happens on the level of the individual compilers is still a problem as we only see about now that the compilers are starting to optimize those cases.
In that regard we don't know yet the true costs of those checks but time will tell.
In that regard, the statement is still incorrect. The link I posted above analyzes the cost at the CPU level given the optimal machine code. It estimates an upper bound of a 2x overhead per overflow-checked add. ie. not free.
Determining whether or not variables will be used uninitialized is equivalent to solving the halting problem.
People don't build unterminated communications lines nor use PIO without pullups/pulldowns. Programmers should be the same way.
Perhaps I've led a charmed life, but I've never seen a case where proper initialization was a problem.
If your program makes initialization depend on many conditions which makes it hard to analyze for the compiler, you're probably writing very fragile code that will soon bite you with UB.
[citation needed]
> If your program makes initialization depend on many conditions which makes it hard to analyze for the compiler, you're probably writing very fragile code that will soon bite you with UB.
Say you allocate a stack, and access it with the following pattern:
bool push(stack *st) {
if (st->top == st->limit) return false;
st->top++;
st->top->foo = FOO_INIT;
st->top->bar = BAR_INIT;
return true;
}
void pop(stack *st) {
assert(st->top != st->stack);
st->top--;
}
void stack_alloc(stack *st, int n) {
st->stack = malloc(n * sizeof(*st->stack));
st->top = st->stack;
st->limit = st->stack + n - 1;
}
If you only access the stack through st->top, it is easy to see (for a human) that the stack will never be accessed uninitialized. Good luck getting the compiler to build an inductive proof of that though.This is just the first example that popped into my mind because I was doing this yesterday.
Because you are already using it. You, the median C developer, are already writing code assuming that it will be compiled by a friendly C compiler. Friendly C is one of the most widely used programming languages on the planet as of 2015. The most widely used SSL/TLS stacks are written in friendly C.
The “friendly C” manifesto is not intended for C developers, an amazing proportion of whom already write friendly C. The manifesto is aimed at C compiler developers.
Friendly C just needs a compiler, or perhaps several of them.
As of Rust, it's not even 1.0-stable out yet. Even if it was, it would take 4-5 years to mature, get enough support, tooling etc to be something you'd use instead of C/C++ for everything.
...and "Expert C Programming: Deep C Secrets":
https://www.google.com/search?q=expert+c+programming+deep+c+...
http://www.sqlite.org/talks/wroclaw-20090310.pdf
If you forgive my plug, my bullet point summary plus copy-ready code for coverage testing examples:
http://aleccolocco.blogspot.com.ar/2009/08/sqlite-lesson-in-...
For example:
[[dwis]]
{
if ( signed_int + another_signed_int < signed_int ) {
// do stuff if there is an overflow
}
}
Or something similar, which wouldn't be optimized away?Maybe some day Rust will be a good candidate, but that day hasn't come yet.
No, only because the Linux kernel started as an UNIX clone and C is the UNIX's systems programming language.
Other OS were written in another systems programming languages. Before and after C got invented.
C only became widespread because a few successful startups in the 80's adopted UNIX for their workstation OS.
Borland started out with Pascal, but went to 'C'.
But it's still a language you need for certain things, like responsiveness or real time applications. Like linus torvalds said it, C is like a more comprehensive assembly generator than anything else.
Having such a language allows you to change every little boring details to do exactly what you want. It has its use, and it's surely not intended to be mainstream as of today. But there are many cases where C comes back as a constant common denominator.
It's sure that it's kinda shitty that there's nothing really better than a language designed in the 70s, but I guess you could either blame language designers for not making anything better. There also are business/political issues and preferences for certain things that comes into play.
All in all, C just seems to be pretty good when you want to have code that works well with systems. Maybe there's just inertia as to where computer development is headed, which might be heavy related to C.
All I wish, is someday to be able to learn some more "virtuous" language like haskell, scheme, or smalltalk.
The article doesn't make much sense at times: Dereferencing a NULL Pointer: >>>contrary to popular belief<<<, dereferencing a null pointer in C is undefined.
I guess C programmers are not counted in the "popular" group.
https://news.ycombinator.com/item?id=8879948
A lot of this is C compiler writers using the spec to perform counterintuitive "optimizations" (with dubious performance value), and forgetting that they're writing software for C programmers to use, not trying to win a pedantry war with them.