Reducing undefined behavior in the C language
lwn.net
[9 comments hidden]
[8 comments hidden]
struct User {
char name[100];
bool is_admin;
};
Where a buffer overflow can still overwrite `is_admin`.Both also require recompilation of everything, which might be possible for CHERI but not for Fil-C - which is why, for example, there cannot be Fil-C support for Windows or macOS.
[5 comments hidden]
[2 comments hidden]
[hidden]
You can also limit access permissions IIUC; I'm only going by old memory here, but you'd be able to hand out a read-only pointer derived from an RWX pointer.
[2 comments hidden]
This "name overflows to is_admin" example doesn't come up in real code or real CVEs. It does come up in HN comments on Fil-C threads though.
CHERI is less compatible than Fil-C, in terms of how many changes you have to make to code to make it work, and this is an example of a policy decision that causes that result.
[hidden]
It is interesting that C++ decided to disallow it in compile time evaluation ( constexpr contexts ).
I have already seen fixes where code that used pointer arithmetic to go from one struct member to another generated garbage in real-world gamedev projects. That code had been like that for 20+ years and worked fine, then suddenly broke after toolchain update.
[hidden]
[hidden]
What makes memory safety so important is that in C/C++, a missing bounds check on `name` gives you access to the entire heap, not simply the other fields within that allocation. Attackers aren't using missing bounds checks to look for `is_admin` fields in the same object. They're looking for a pivot to full memory control, so they can plug in their RCE exploit chain. So, the only time that attackers are looking for an intra-object overflow is if they have something like `struct { char buf[100]; void* ptr; }`, since that does lead to a pivot to full memory control. Fil-C protects that; you can't use that overflow to corrupt `ptr`'s capability.
This is why memory safety tech like MIE, MTE, and Fil-C focus on allocation granularity. If I wanted to support subobject granularity, I could; it's not a huge change to the capability model. Fil-C's first two capability models (back in late 2023, early 2024) supported subobject granularity. I'm choosing not to support it now because I understand the security implications well enough to know that it's not worth the complexity.
Fil-C started out as a macOS project. Fil-C supports Windows and macOS today via APE. Requiring recompiliation of everything is a luxury I can choose to support on Linux that results in memory safety of the whole process. If you find Fil-C useful and understand compilers, you could probably get a variant working that has FFI and then expose Windows and Mac API to it. You'd get something with a weaker safety guarantee, but no weaker than what you get today from Rust or Java.
[43 comments hidden]
I'm OK with compiler doing wild thing, OK, whatever. Well, I'm not OK but I can accept it in this crazy world.
But I want to have loud warnings! Like WARNING: this conditional operator has been collapsed to one branch because earlier division by zero is UB. And now I can notice it and rewrite it or just remove that condition.
I understand that this code can be result of macro expansion. That's OK. Macros should either include some pragmas to temporary disable specific diagnostics or user should surround macro usage with these pragmas, if they can't edit the macro. It's already happening with other warnings.
Or maybe compiler could be smart enough to distinguish macro expansion from honest user mistake, I don't know.
I remember when C++ compiler just removed function epilogue where I wrote simple infinite loop. That was so crazy. So instead of entering the infinite loop, my program just continued to execute the function that happened to be linked below. Imagine debugging that. Zero diagnostics.
[18 comments hidden]
[8 comments hidden]
The problem here is that the C compiler ever assumes that UB doesn't happen. That is empirically very much not the case, therefore the compiler should never be allowed to assume a lack of UB unless it can somehow prove that to be true. I honestly don't really care how many optimizations that would break; correctness is king. Software that goes fast is only worthwhile if it works correctly.
[6 comments hidden]
It's approximately all optimizations. Good news: there's already a compiler option that does exactly what you want: -O0. It's even enabled by default (unless overridden by another -O switch)!
[5 comments hidden]
[4 comments hidden]
Under the as-is rule, optimizations must not change program behavior. A C program could theoretically use out-of-bounds pointers to scan its own stack, observing whether a value occurs on the stack. Thus, the as-is rule prohibits storing local variables in registers!
But because out-of-bounds pointers are undefined behavior, the compiler can ignore the programs doing stack scanning, and so register allocation becomes possible under the as-is.
So boring old register allocation is one of those "assume that UB doesn't happen" optimizations! Of course, the compiler never actually reasons "this pointer arithmetic is out of bounds therefore I can put that variable over there into a register" -- the reasoning from undefined behavior doesn't happen at compile-time, it already happened when the "register allocation" optimization was designed. But that's the case for most "assume that UB doesn't happen" optimizations! (this is also why it's so difficult for compilers to warn about undefined behavior -- they optimize based on it without ever detecting it!)
If you want to eliminate "reasoning from undefined behavior", you'd also need to replace the "as-is" rule with something else -- an explicit list of allowed optimizations in the language standard?
[2 comments hidden]
Though it might be possible to have more "sensible" UB. For example, assuming daemonic non-determinism for allocation address it is possible to turn stack storage into registers, without the need for an aliasing model. Benchmarking it has the same difficulty.
[hidden]
[hidden]
Imagine you have a tail-recursive function and you'd like to convert it to iteration. This is only semantic preserving if there is no UB!
Or worse, if you can never assume that UB doesn't occur then you need all code to be compiled in a way that defends against data races. You can't change the allocation order on the stack, since there might be some write past the end of a buffer and now suddenly stack allocation order changes the semantics of the program. Nightmare.
Imagine this program:
int f() {
int x = 1;
a(x);
b(x);
return x;
}
Can we use constant propagation to replace "x" with 1 in the instructions? No. After all, a could smash the stack and write over x. You need a new read of that memory location every time you plan on using x.[7 comments hidden]
If I wrote some code, I expect it to be present in the binary. I don't just write code to be removed by the compiler. If that expectation was wrong, compiler should inform me about that.
[3 comments hidden]
It's very common to write code in templates or inline functions expecting the compiler to remove it if it's not relevant on the calling site. That's part of what makes "zero cost abstractions" have zero cost at runtime.
For instance, I have a SIMD routine with extra code to process the tail (leftover elements smaller than the native vector size). When the compiler can prove that the size of the input will always be a multiple of the vector size (which is very common for my use cases), it will completely remove that tail handling code.
[2 comments hidden]
[hidden]
[hidden]
That's oversimplified. If after inlining and constant folding an if-condition turns out to be always true or false I would definitely expect that the compiler removes the dead branch.
This type of optimization is the base for the fabled "zero-cost-abstraction" (which isn't only a C++ thing, C code depends on it just as much), and removing those optimization would seriously tank peformance in any non-trivial codebase.
[hidden]
It is definitely a hopeless path.
[hidden]
Suppose you have the following program:
unsigned f(unsigned x) { return 2*x; }
Can this be optimized to instead perform a bit shift? That's faster than multiplying. But you wrote a multiplication operator. Should the compiler be responsible for keeping it?
[2 comments hidden]
Regardless you are right there is a cost is it worth paying hard to say
[hidden]
Checked divs are present in release mode too. Omitting them would be UB.
Other checks (e.g. for additions) are omitted in release mode, but they were not preventing UB in the first place because they guard a wrapping addition, not an unchecked one (the kind that's UB in C).
[16 comments hidden]
For the static ones there are often warnings you can set, but you’ll have to go through the list. Or possibly external checkers (e.g. clang-tidy has one for infinite loops but not sure it’s 1:1 with the optimiser on complex cases)
[15 comments hidden]
[12 comments hidden]
The resulting performance difference is basically the price to pay for such a 'strict' compiler which translates the input source code straight into machine code instructions without attempting to simplify the output code via inlining, constant folding and dead code removal.
[8 comments hidden]
[3 comments hidden]
> Dead code removal is unexpected by the programmer, because programmer does not write dead code.
Of course it is expected, because the optimizations leading to dead code are fundamental for the performance you get out of a release-mode binary (and that's also true for languages with less UB like Rust btw).
When you have a function:
static int add(int a, int b) {
return a + b;
}
And you call that function with const parameters: int c = add(2, 3);
Then you want that entire function call to be removed and "folded" into its result 5, and when this was the only place the function was called, you'd also want the actual function to be removed from the binary (because what's the point of lugging code around in the binary that's guaranteed to never be executed).[2 comments hidden]
Here's example from article:
static int f(int a, int b)
{
x = b ? 42 : 43;
return a/b;
}
Compiler can inline `f` but if it removes conditional branch, that's where issue is. I wrote this branch because I expected `b == 0` to be a valid value. Compiler deduced that it can't be a valid value and decided to remove `43`. That's understandable. But let me know, so I'd rewrite my code myself instead. Because I have assumptions and compiler have assumptions and these assumptions do not match. It means that there's a bug.[hidden]
Because as far as it’s concerned that is the same thing.
Both clang-tidy and sonar will flag this since it’s broken on its own, but when you’ve fixed it you want the compiler to warn you every time this is inlined in a function where b is known non-zero and the check is redundant?
As flo told you, if you want a non-optimizing compiler use a non-optimizing compiler.
[2 comments hidden]
[hidden]
[hidden]
Dead code elimination is like the single most profitable optimization in the compiler, especially in conjunction with inlining and constant propagation that makes several branches become trivially dead. And its removal is very much expected by programmers, because (for example) bounds checking tends to work by generating bounds checks for every possible array access and relying on the compiler to be able to prove most of those bounds checks redundant.
No, my suspicion is you're actually only against certain kinds of dead code removal, but the kinds you're against are so inconsistent that it's impossible to implement a decision program except by including a routine to read the programmer's mind.
[3 comments hidden]
[hidden]
void clear_buf(char *buf, size_t len) {
char *end = &buf[len];
if (end < buf) return; /* in case len is so long the pointer wraps the memory space */
while (buf < end) *buf++ = 0;
}
(which it can do, by virtue of saying that constructing a pointer beyond end of buf is itself undefined behaviour, therefore it can expect that end is never less than buf. the more appropriate way to write this is "while (--len > 0) *buf++ = 0" and let the compiler invisibly change that to end=buf+len if it thinks that'll work in all situations and be more efficient than counting down len while moving along buf)...but you wouldn't be upset if it changed this:
for (int i = 0; i < 1000000; i++) {
if (i < 5) foo(i);
/*else something_else(i);*/
}
to effectively this: for (int i = 0; i < 5; i++) {
foo(i);
}
or even: foo(0); foo(1); foo(2); foo(3); foo(4);[hidden]
Memory error checking in C and C++: Comparing Sanitizers and Valgrind (quite comprehensive) - https://developers.redhat.com/blog/2021/05/05/memory-error-c...
[3 comments hidden]
Also AFAIK the point where UB causes 'runtime disruption' is way after the C frontend in the optimizer passes, e.g. much too late for issuing compilation warnings even if the UB situation could be detected (because as far as I understand the problem, the breakage happens mainly because of unexpected 'spooky actions at a distance' between different optimizer passes, e.g. a specific optimizer pass doesn't even notice that it broke the code).
You can get runtime errors for a lot of serious UB problems via UBSAN though of course (at the cost of some performance).
[2 comments hidden]
Accepting tag soup led to differences in rendering per browser. Trying to go harder on correctness, XHTML, was just too strict for people to write themselves. WHATWG came to the conclusion that they should standardise how to correct badly-written HTML, rather than say "do what you want" for anything that doesn't parse correctly.
[hidden]
AFAIK the C committee is on the right track though. Start with collecting and classifying all mentions of UB in the standard (IIRC it's somewhere around 200 items), then decide what to do with each of those. A lot of the UB items are completely silly ancient artifacts which can easily be clarified (like the "no newline at the end of a source file").
Then there's probably quite a few items that can become implementation defined behaviour instead.
And only then decide what should happen to the remaining UB items that are actually important for optimization, maybe require a new 'checked mode' which turns those cases into runtime errors.
Also I think the "nasal demons" problem needs to tackled somehow (e.g. the thing that a compiler is allowed to do anything in the presence of UB) - obviously this is the trickiest part, and in reality there's hardly any C program which doesn't contain UB.
[2 comments hidden]
Might be wrong but the big issue with warning about UB probably isn't C it's C++ and it's templates. Modern C is mostly what you see is all there is. Where with C++ lord knows really.
C is often a bit slower than C++. Which also means trading a little speed to eliminate UB isn't a big hit. Where doing that inside a template expansion can slow things down a lot.
My preferred solution is people stop using C++.
[hidden]
Suppose you have the following program.
int f(int x, int y) {
if (x < 0 || y < 0) return -1;
if (x > 100 || y > 100) return -1;
return x + y > 200;
}
A compiler class computes the possible ranges that x and y can take at the return statement. Another pass does arithmetic simplification and simplifies the final instruction to "return 0." Should the compiler warn you here? No, obviously.What you want is for this to only warning you if some conclusion about the program that led to a simplified conditional operation came from an assumption about UB. But compiler passes can't really track the "whys" of each conclusion it makes. It spreads so broadly so quickly.
[hidden]
What Every C Programmer Should Know About Undefined Behavior https://blog.llvm.org/2011/05/what-every-c-programmer-should...
[hidden]
1790673092 | Reducing undefined behavior in the C language | https://lwn.net/SubscriberLink/1095811/efcdbcf080cfa4c6/ | https://news.ycombinator.com/item?id=49890290 | 0 comments
[18 comments hidden]
C is "the right tool for the right job" which is operating systems and its code which is called thousands of times per second. You cannot afford even one iota of runtime checks in that code. The developer must know what he's doing or he should get out of the kitchen.
We should discourage the usage of C in application programming and prod application developers towards memory safe languages like Rust or Go.
And I'm not even sure if Rust solves this case as far as UB is concerned.
[6 comments hidden]
Fortran has historcally led this realm (see the Numerical Recipes book).
Julia is a newer option, and I understand that both are commonly used in Python objects.
"Read the older 2nd ed. book in Fortran online for free."
[hidden]
The issue with floating point and aliasing preventing vectorisation was from the late 1980s when early C compilers lacked sophisticated alias analysis and standards were loose. is not a really a thing anymore. C can go as fast, specially compiled with strict aliasing. Maybe more work in compilation.
[4 comments hidden]
- Procedure arguments are not allowed to alias. Similar to restrict pointers in C99+. This is often critical to allow loops to be vectorized, but the onus is on the programmer to ensure no aliasing or else you get UB.
- Unspecified evaluation order for expressions. E.g. C requires that "a+b+c+d" be evaluated as "((a+b)+c)+d)" and with floating point it can't do it another way due to rounding. Fortran can do e.g. "(a+b) + (c+d)" where each subterm can be computed in parallel, but again at the cost of slightly different results due to rounding behavior for floats.
- Old school Fortran lacked pointers which led programmers to program algorithms using arrays rather than fancier data structures, which cpu's love.
In principle there's nothing preventing a competent C or C++ programmer can reach Fortran level performance. In practice, might be difficult.
Of course, nowadays performance is much about designing for cache hierarchies (see e.g. "Data Oriented Design") where Fortran doesn't have a built-in advantage.
[3 comments hidden]
https://en.wikipedia.org/wiki/Row-_and_column-major_order
In short, people solving numerical problems want to write like M[i,j] where i is iterated through on the innermost loop and j is iterated through on the outermost loop... and if the language works in sympathy with them and gets good cache-coherence, that's great, but if the language works against them and would make them write M[j,i] to get the same performance, they don't like it, so they keep writing M[i,j] and let the slowness be the other language's problem.
[2 comments hidden]
But speaking of arrays, one definitive advantage of Fortran is that it has built-in multidimensional arrays in the language. Which is very nice when writing code that needs them. In C many people resort to the Fischer-Price My First Multidimensional Array implementation where they allocate each row separately, which is of course horrible. (In C++ you can use libraries like Eigen or Armadillo which use expression templates and operator overloading to have sensible multidimensional array support even if it isn't built-in.)
[hidden]
It has to be row-oriented because the language allows you to take the address of each intermediate dimension, e.g. int *c = &a[3] gets you a pointer to the equivalent offset of b[30], and legally you can access c[0] through to c[9]. Accessing c[10] would be equivalent to a[4][0] and compilers will let you do it, but I think it's officially undefined behaviour?
[4 comments hidden]
The problem with C is that modern compilers do a lot of transformations between your source code and the final machine code, so the actual behavior could be very far afield from what you would expect.
> And I'm not even sure if Rust solves this case as far as UB is concerned.
If your entire program is inside unsafe, then Rust is actually worse than C as far as UB is concerned. On the other hand, no one writes Rust like that, and Rust restricts all UB to unsafe blocks.
[hidden]
Nitpicking: it's not the unsafe blocks themselves, but a few language features which are only allowed inside unsafe blocks. Language features which are allowed outside unsafe blocks work identically within unsafe blocks, and gain no extra UB just by being inside an unsafe block.
[2 comments hidden]
Rust gets nasty if you want to build safe interfaces around unsafe code because you have to consider every single potential safe interaction (Rust aggressively reorders everything because the aliasing model allows it, in that respect it is "worse" than C but not less safe), whereas with an unsafe caller you're allowed to shrug with your shoulders and tell the higher up caller that it is his problem to figure out, but if you keep handing off to ever higher level unsafe callers, then you are literally back to C levels of unsafety.
If it is not clear, if you use unsafe {} everywhere, you can use *const T, *mut T everywhere. Hence the no aliasing rules don't matter to you at all, which brings us back to the &mut T and &T unsafe-safe interactions between *mut T and *const T. Turning *mut T to &mut T or turning *const T to &T is hard.
This is what makes it so hard to write unsafe code, it's supposed to be usable from a safe interface!
It also explains why static mut was a huge mistake. You cannot take a mutable reference to them. Mutable reference semantics make no sense with static mut.
[hidden]
It is not, and that's the issue. If it was just "high level assembly" there would be no UB, and no need to have UB. Instead you have UB (although arguably not all of it is really needed) because you need optimizations, which in turn you need because otherwise C would be too slow for that "operating system" job.
> code which is called thousands of times per second
Scripting languages can easily have loops running thousand of times per second and even more. You're off by some order of magnitudes here if you want to describe operations that happen in operating systems.
[hidden]
[2 comments hidden]
While I don’t condone writing new code in C, there are major conceptions about its relationship to performance and C++.
[hidden]
Compile time is not the main issue with static analysis. It's that C doesn't provide enough information and not the right information to make it efficient and effective.
If you add this info you unavoidably will end up with something looking like Rust.
(Whose long compile times are not caused by its static analysis parts BTW)
[hidden]
Edit: To be fair for the current topic it's more of an "Inverted Design-by-Contract" issue. The compiler infers the contract via UB instead of exposing it to the developer.
when you divide a/b the contract b != 0 is formed on the function signature, but never made visible. Then someone inevitably violates the invisible contract and all hell breaks loose.
// What the developer thinks the signature is
int f(int a, int b);
// What the compiler secretly changed the signature to
int f(int a, int b)
__attribute__((requires(b != 0))); // The invisible contract![hidden]
That's already a fundamentally wrong assumption :) With today's compilers, C is a high level language like all the others. The optimizations happening to C code are not fundamentally different than for any other compiled high level language.
> We should discourage the usage of C in application programming and prod application developers towards memory safe languages like Rust or Go.
No that's rubbish, just as it would be rubbish trying to 'discourage' people from writing programs in assembly code or any other programming language.
Ultimately, guaranteeing "safety" is the job of the sandbox your untrusted code is running in (e.g. the browser, operating system or VM).
The only difference between Rust and any "unsafe" language should be that one fails already at compilation time and the other at runtime (because the sandbox killed your rogue process).
E.g. if an operating system is exploitable because it allows untrusted programs to leak out of the sandbox, then that problem must be fixed in the operating system.
[hidden]
Imagine a definition of g() that uses longjmp to jump out of f to a jmp_buf context set up by a caller of f bypassing the division by b in the abstract semantics.
[4 comments hidden]
[3 comments hidden]
However if you are willing to restrict what programs you allow, you can make guarantees possible.
Silly example: if you compile valid (safe) Rust programs to C, you know that the resulting code will not invalidate Rust's borrowing rules by construction; and in principle you could try to establish this guarantee just from the C code alone, never having seen the Rust original.
However, you still wouldn't be able to have an algorithm that tells you for any arbitrary C code whether it has these problems or not.
[2 comments hidden]
[hidden]
[2 comments hidden]
[26 comments hidden]
[2 comments hidden]
There wasn't room for safe programming practices, and direct manipulation of the hardware was a design requirement.
It assumes that you know what you are doing.
There are also ports to the Zilog Z80, an architecture with similar limitations (UZI, FUZIX).
[hidden]
Rust's unsafe mode, for instance, has undefined behavior. A whole list of them, in fact.
[10 comments hidden]
Does Rust define what I get when I dereference NULL in unsafe code? I doubt it, since it would require a NULL check before every pointer dereference.
The only insane thing about UB is that compiler writers took what everyone understand meant "the compiler emits what it emits and you get what you get" and turned it into "since it's undefined it means it can never happen so we can delete your null check".
[5 comments hidden]
(Here's a link with a bit more detail: https://courses.cs.vt.edu/cs3214/spring2026/questions/catchi...)
So, while it's accurate to say that inserting null checks before every dereference is one way that you could implement this to make it well-defined, that is not the only way. We have lots of clever tricks to solve problems more efficiently than may seem possible at first glance—Fil-C is a bit of a modern marvel in that regard!
[2 comments hidden]
Fil-C is amazing and a prime example that undefined behavior means implementor freedom, and the implementor can choose to always trap on null pointer use. Sometimes the implementor freedom doesn't buy you much; for example why should it be UB to do
(const char*)NULL + 1
Dereferencing null is and should be UB but why is just calculating a pointer problematic? I just did some research and some old architectures would actually trap on creating an invalid address. So if we want C to support those machines, the standard can't define the behavior to do something other than what the hardware does.[2 comments hidden]
If you want to run everywhere where C does, you can't rely on that. I gave WASM as an example - that's a widely used target that just exposes a flat memory model where 0 is literally just a normal address and there's no way to trap it (unless they've released extensions I'm unaware of). Same deal with most microcontrollers as far as I'm aware of, although I don't do embedded.
I can't think of a way you'd implement null trapping efficiently on those platforms.
[hidden]
That's an argument in favour of 'implementation defined behaviour'. Not 'undefined behaviour'.
[hidden]
IMO UB as "undefined but don't be crazy please" was the original meaning of the standard but people argue on that. It is a fact that compilers didn't exploit UB as strongly back then. However there is a good reason for this change: if you want formal semantics (which you do want, at least possibly) it is pretty much impossible to distinguish the two. If "undefined behavior" is undefined in the math sense, or in formal semantics of languages - the operation can reach any Abstract Machine state, then the fact that you cannot reason about anything follows immediately. The only dubious thing is time-travel, and this was indeed removed in the last version of the standard (and also for Rust now).
[hidden]
This is the bit that seems insane to me. Some of the behaviour of C/C++ compilers when they encounter undefined behaviour seems less like 'this is undefined, we'll just do something vaguely reasonable given the context/produce an error' and more like 'ahaha, the user has fallen into our trap, lets fuck them up'.
[hidden]
That's just compilers optimizing your code under the assumption that it correctly follows the rules of the language, which is the basis for any optimizing compiler and completely sane. The compiler isn't optmizing any null checks that actually correctly prevent all UB only ones that are faulty and never fully worked to begin with and as a result are indistinguishable form any other pointless code paths.
[10 comments hidden]
if (x > 0) {…}. But what if you entered the body when x <= 0?
if (false) {…}. But what if you execute the body?
These are “impossible”. What happens when the impossible occurs is “undefined”.
When the older standards said signed integer overflow for addition is undefined what they are actually saying is that the real definition of + is:
int +(int x, int y) {
assert(in_range(actual_math_add(x, y), signed_int_min, signed_int_max));
return machine_add(x, y);
}
So of course what happens when you get signed overflow is undefined; you should hit that assert and your program should explode and die. You should “never” get to the next instruction.But, in the interest of performance, “release mode” (which in this case is just any compilation) elides asserts since as a programmer you should not write code that asserts in much the same way that you should not write assert(false) in a normal code path that is supposed to run. Assertions are intended for “impossible” code paths and usually get compiled out in “release mode” though maybe your code is buggy and can actually hit them and then your program goes off the rails because it had a bug.
Put another way, if you did write assert(false) in a regular code path, would you find it unreasonable for the compiler to just delete the code after it? That is what undefined behavior is for.
[7 comments hidden]
For example, this code with an improper guard:
if (!p) puts("error");
printf("%d", *p);
Since the program dereferences p in line 2, and dereferencing null is UB, the compiler is allowed to assume p is never null, so it’s allowed to delete line 1, even though it would have executed before the point where UB would happen.Even worse, the compiler isn't just allowed to not do things you told it to do, it's also allowed to do anything too.
[3 comments hidden]
I was explaining why undefined behavior as a concept is a very sensible idea. Whether the expansive interpretation of the optimizations you are allowed to do when encountering the “impossible” are reasonable is a different question.
[2 comments hidden]
[hidden]
UB is a formal tool for the optimizer.
Most people who argue about UB on HN have no clue wth it means and how it differs from Unspecified and Implementation-defined behaviours. The standard already explains expressions/statements and how they relate to sequence-points/sequenced-before/sequenced-after code points which is what is needed to understand the anomalous behaviour above.
Add in a introductory class in numerical analysis w.r.t. accuracy/precision/limits/rounding and the C/C++ programmer has enough knowledge to avoid problems in practice.
[hidden]
> if (false) {…}. But what if you execute the body?
> These are “impossible”. What happens when the impossible occurs is “undefined”.
By the way, that's exactly what happens with Spectre and its class of ghostly vulnerabilities! The processor speculatively executes these "impossible" paths, and discards the result once it detects they couldn't happen; but there are ways to "leak" information from that irreal world through side-effects like cache lines being discarded.
[hidden]
> Surely the compiler can just check if each access is valid.
Well, sometimes it can, but sometimes it doesn't know how long the array is. What if the array is passed as a pointer?
> Maybe each array could be annotated with its size at runtime, and accesses could be checked at runtime too.
That works, but it adds runtime cost that may legitimately be too much for some applications, for example, a Gameboy game (set aside that many Gameboy games were written in assembly).
> Fine, so we'll make the programmer promise to ensure array accesses are always valid. Maybe they'll make a mistake sometimes, but what's the worst that could happen? Throwing your hands in the air and saying the compiler is allowed to do anything, that's just stupid.
Well, maybe it's stupid, but this is one thing that could happen if you accidentally write past the end of an array: https://www.youtube.com/watch?v=Vjm8P8utT5g. I'm sure neither the programmers nor compiler writers intended that.
Ultimately, the compiler can't guarantee any behavior if its assumptions are violated. The example may seem contrived, but it demonstrates that, given the right circumstances, the results of the logical contradiction are unbounded. This is a direct consequence of the "Principle of explosion": https://en.wikipedia.org/wiki/Principle_of_explosion. On second thought, maybe the runtime costs of array bounds checking are an acceptable trade-off after all.
[hidden]
int main() {
int x = 1;
a(x);
b(x);
return x;
}
Can I use constant propagation to replace the reads of x with the constant 1? This relies on the assumption that there is no UB, since otherwise our function calls could smash the stack and overwrite where x is stored on the stack.[19 comments hidden]
As per the linked article:
“There are currently about 100 instances of undefined behavior in the C standard, but the in-progress C2y draft has removed 45 of them.”
I wonder how they handle the specific case of uninitialized but allocated memory.
Let’s look at something which will result in undefined behavior in C99: [1]
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#define b(z) for(c=0;c<z;c++)
uint32_t c,e[42],f[42],g=19,h
=13,n[45],i,j,k;void m(){j=0;
b(12)f[c+c%3*h]^=e[c+1];b(g){
i=c*7%g;k=e[i++];k^=e[i%g]|~e
[(i+1)%g];j=j+c;n[c]=n[c+g]=k
>>j%32|k<<-j%32;}for(i=39;i--
;f[i+1]=f[i])e[i]=n[i]^n[i+1]
^n[i+4];b(3)e[c+h]^=f[c*h]=f[
c*h+h];*e^=1;}int main(int c,
char**v){char*q=malloc(2);if(
q==0)return 0;q[0]&=31;q[0]|=
64;q[1]=0;for(;;m()){b(3){
for(j=0;j<4;){f[c*h]^=k=(*q?
255&*q:1)<<8*j++;e[c+16]^=k;
if(!*q++){b(18)m();b(2){j=c;
b(4)printf("%02x",(e[1+j%2]
>>8*c)&255);c=j;if(c%2)m();}
puts("");return 0;}}}}}
The key part of the above brick of code is this: char *q=malloc(2);
if(q==0)return 0;
q[0]&=31;
q[0]|=64;
q[1]=0;
Here, we see that q[0] is an allocated but undefined byte. As per C99, this results in undefined behavior, however 20 years ago this was a good trick to get kinda-randomish bytes to use as a possible entropy source.Someone claimed that the above brick of code will compile in newer versions of clang such that, since the complex cryptographic pseudo random number generator code depends on uninitialized but allocated memory, the entire cryptographic operation isn’t performed.
So I tested it against multiple versions of GCC and clang; I also tested it against TCC for good measure.
In all cases, with all levels of optimization, the cryptographic routine ran. I even ran it against clang 23. In cygwin, it was a randomish but consistent byte (except for clang at a higher level of optimization, at which point the uninitialized byte had a value of 0); in Ubuntu 26, the uninitialized memory consistently had a value of 0 (in tcc/gcc/clang).
I am hoping the up and coming C2y spec has very clearly defined behavior when using unintialized memory (ideally where it will work but the bytes can have any values).
Naturally, I have updated my code to no longer use uninitialized memory as a source of entropy. 20 years ago, MacOS didn’t support clock_gettime() with nanosecond resolution, so that wasn’t a portable way to get pseudo-random bits; these days clock_gettime() is universal across modern development environments, and it provides pretty good entropy (along with using /dev/urandom in *NIX, which isn’t in POSIX but is widely supported, as well as CryptGenRandom() in the legacy Win32 port). [2]
[1] Said person said the appendices to C99 aren’t authoritative, but if something is in the spec, including in the appendices, it’s authoritative.
[2] I don’t blindly trust /dev/urandom to always make really hard to guess pseudo-random bits, because my code is open source, and, as such, doesn’t just compile in Linux. It often times will be compiled in embedded systems, and even Linux has had at times issues with /dev/urandom on Raspberry Pis.
[3] I would also like to see uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, and int64_t mandated. They exist in C99, but aren’t mandated, even though every real world compiler from this century supports all of the above types. Yes, I know about _BitInt(8/16/32/64/128/etc.) but a compiler from 2004—and yes I still use one to make win32 binaries—doesn’t support these new C23 datatypes.
[6 comments hidden]
That's not true. There is a difference between normative text and informative text. Informative text is not authoritative, and you were citing an appendix that is labeled as informative.
> I am hoping the up and coming C2y spec has very clearly defined behavior when using unintialized memory (ideally where it will work but the bytes can have any values).
It won't. Uninitialized memory can't have "very clearly defined behavior" without breaking essentially every single implementation, and WG14 is very loth to break existing implementations.
[4 comments hidden]
You can initialize with some bit pattern. This is what was accepted for C++ (but only for certain types of memory).
[3 comments hidden]
In fact I hate that C mandates static variables being initialized to zero. This is dumb. When I'm writing for MCU, that's useless cycles spent at the power on. Thankfully it's possible to fix with linker tricks, but it should not be an issue in the first place.
[hidden]
Having accessing uninitialized memory be UB is just insane, there's only two possible sane implementations - potential trap for caps based systems (or for a static analyzer) or you get a pseudo random value i.e 'whatever happened to be there'. So just make it implementation defined.
[2 comments hidden]
[10 comments hidden]
int* p = malloc(sizeof(int));
int v = *p;
if (!(v < 0 || v == 0 || v > 0)) {
exit(1);
}
This compiles into `exit(1)` in Clang under -O3.[9 comments hidden]
[2 comments hidden]
[4 comments hidden]
(Note that it's not just the compiler that plays the game--a memory allocator might choose to use MADV_FREE for freed pages of memory, which means that at any point until its next write, the OS may replace the contents of the page with a zero page. So if a subsequent call to malloc reuses a MADV_FREE'd page, then reads of that data before anything it has been written to it may suddenly switch to a different value depending on the whims of the kernel.)
[3 comments hidden]
(NULL < 0 || NULL == 0 || NULL > 0)
where even though it seems like one of those would have to be true, yet all 3 are individually unknown? Got it. Thanks for the explanation![2 comments hidden]
[hidden]
SQLite-3.54 in-memory-> select NULL < 0 or NULL == 0 or NULL > 0;
╭──────────────────────╮
│ NULL < 0 or NULL ... │
╞══════════════════════╡
│ NULL │
╰──────────────────────╯[hidden]
The real problem I see is the inability of independent compiler writers upgrading to the latest standard. I think this is also an issue that the standards body should pay attention. Help implementers.
[13 comments hidden]
[3 comments hidden]
C23 already requires 2's-complement representation for signed integer types, but signed overflow still has undefined behavior. I think that mandating 2's-complement wraparound would be a mistake.
Some instances of undefined behavior can be detected at compile time. For example, if I write
int too_big = INT_MAX + 1;
a reasonably clever compiler can warn about it (and in fact both gcc and clang do so). If the result of INT_MAX + 1 were defined by the language to be INT_MIN, there would be no basis for such a warning.If you evaluate n + 1 and it's possible for n to be equal to INT_MAX before the addition what do you want the result to be? Would quietly yielding INT_MIN really be useful?
Ideally, if I (accidentally) evaluate INT_MAX + 1, I'd like to be told that I've made a mistake. C doesn't have a good mechanism for doing so.
gcc has a non-standard option "-fsanitize=signed-integer-overflow" that can be used to catch signed overflow at runtime. If signed overflow yielded a well defined result, that option would be non-conforming.
[hidden]
Compilers warn about perfectly well defined behaviour all the time. That's why these are warnings, not errors.
[hidden]
[3 comments hidden]
However if you wan, you can already get that via a flag in pretty much any C compiler you care about.
[3 comments hidden]
[hidden]
Currently, based on experience, this level of complexity is categorized into the language layer, and that level of complexity into the operating system layer. But in the future, won't there be some sort of complexity theorem that determines which layer minimizes complexity the most, and won't systems be completely rewritten based on that?
[12 comments hidden]
If a compiler can detect UB, I think the only reasonable way for it is to have the program terminate immediately, not release "nasal demons". (And, of course, many bugs get smashed against the -Wall which should be the default.)
Of course, not all UB can be detected at compile time, like reading the padding bytes in a struct mentioned in the article. I wish there was a way to opt out of all UB, in an arbitrary but predictable way (an immediate crash would be fine), something like -fsanitize but for any UB at all that might happen at runtime. In quite some important code, I'd agree to tolerate the performance hit, it may be cheaper than dealing with the aftermath or an RCE exploited. I see that it's not entirely realistic though.
[2 comments hidden]
In other words: if you write `a[i]` in C, the compiler could either add index checks if its knows the size of `a` with `abort()` calls; or it can just compile it to single access instruction. The latter is faster. The former might be useful for debug build, I guess, but otherwise it's too slow to be acceptable for C usage.
[6 comments hidden]
In the general case it can not, that is why they were made UBs in the first place.
And that is generally not how compilers see UB, they don’t look for UBs lying around, they assume UBs can’t happen and use that for constraints, which they then propagate.
For instance
*i = 1
if (id) { … }
The compiler will likely remove the check, because the dereference tags i as non-null, which makes the test redundant.And this occurs and is extremely desirable every time e.g. a function is inlined in an other one which already checked for null.
[3 comments hidden]
AFAICT, UBI is stuff like that:
short int i;
for (i = 0; i < 33000; i++) do_something();
Whether this program continues as normal, crashes, or enters an infinite loop is platform-dependent, because short int can be as small as 16 bit, and an integer overflow past 32767 can be a crash or a wraparound.[hidden]
The goal of my comment was to demonstrate how compilers use UBs to drive analysis.
Dereferencing a null pointer is one of the most basic UBs you can find in C. Since a valid program can not contain UBs, the compiler concludes i must be non-null, propagates that to the test, which is constant and true, thus redundant and eliminated.
[2 comments hidden]
Again with the backwards word choices...
C developer: "this is extremely desirable"
Translation: Our system is unable to specify intent (nullability of the pointer) so we just guess.
Further context: Other systems specify intent and have no need to guess and as a consequence have this "extremely desirable" feature. The "clever" C tricks are always told from a C centric perspective.
[hidden]
if v.is_none() {
return 0;
}
v.unwrap()
(or any other similar pattern) and enable optimisations, rustc will also elide the second check and the impossible panic call. Because any time you inline there’s good odds you’re creating redundant code (and other such).That is the main reason inlining is so fundamental to modern optimizing compilers, it’s nice that it removes funcall overhead, but it’s more relevant that it unlocks a whole slew of further optimization opportunities (including even more inlining).
[hidden]
You dereference a pointer? That changes the contract of the function so that you must never pass in a non-nullable pointer.
Divide by b? You are not allowed to pass in a zero as b parameter to the function.
But you don't get to see that. Nobody tells you.
[hidden]
"Oh, it was quite a while ago. I kind of stopped when C came out. That was a big blow. We were making so much good progress on optimizations and transformations. We were getting rid of just one nice problem after another. When C came out, at one of the SIGPLAN compiler conferences, there was a debate between Steve Johnson from Bell Labs, who was supporting C, and one of our people, Bill Harrison, who was working on a project that I had at that time supporting automatic optimization...The nubbin of the debate was Steve's defense of not having to build optimizers anymore because the programmer would take care of it. That it was really a programmer's issue.... Seibel: Do you think C is a reasonable language if they had restricted its use to operating-system kernels? Allen: Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. By 1960, we had a long list of amazing languages: Lisp, APL, Fortran, COBOL, Algol 60. These are higher-level than C. We have seriously regressed, since C developed. C has destroyed our ability to advance the state of the art in automatic optimization, automatic parallelization, automatic mapping of a high-level language to the machine. This is one of the reasons compilers are ... basically not taught much anymore in the colleges and universities."
-- Fran Allen interview, Excerpted from: Peter Seibel. Coders at Work: Reflections on the Craft of Programming
[hidden]
These days, it's mostly taken as an invitation by very powerful compilers to sometimes do heroic optimizations (that we like) -- and sometimes screw us over in really obscure ways (which we don't like). We would really like to keep their ability to perform heroic optimizations and that's why there's so much pushback against eliminating undefined behaviour.
chasil[3 comments hidden]
OS 2200 has 36-bit words. It is still a supported platform.
https://en.wikipedia.org/wiki/UNIVAC_1100/2200_series
This platform was the first SMP UNIX implementation:
"Any configuration supplied by Sperry, including multiprocessor ones, can run the UNIX system."
https://www.nokia.com/bell-labs/about/dennis-m-ritchie/other...
eru[hidden]
Yes, but that's perhaps an argument for 'implementation defined behaviour', not in favour of 'undefined behaviour'.
strenholme[hidden]
If so, then said compiler should still have support for (u)int_8/16/32/64_t. Said compiler already needs to have that support with (unsigned) _BitInt(8/16/32/64)—and, yes, one can also have 36-bit ints on x86_64 in C23 with _BitInt(36) if one must—and allowing stuff like int32_t and uint32_t will allow said (imaginary) 2200 system to cleanly compile a lot of pre-C23 open source code out there. Yes, uint32_t will look a little ugly at the assembly level, just as _BitInt(36) will look a little ugly on, say, a Xeon processor, but the code will compile and run the same.
Of course, these days they can buy tokens so an agent can do any relevant porting, but still.