24 ms·
Depressing and faintly terrifying days for the C standard [pdf]
- justboxing 8y agoDirect link to PDF: http://www.yodaiken.com/wp-content/uploads/2018/05/ub-1.pdf http://www.yodaiken.com/wp-content/uploads/2018/05/ub-1.pdf
- phkahler 8y agoI want C to not grow any more. The problem with having groups to oversee things is that they usually feel the need to change or grow the thing they're supposed to be watching. Sometimes this is good, but often it's not. C is the foundation of a lot of stuff, and it'd be better to rewrite that stuff in a better language than to try and turn C into something it's not - that's what C++ was for.
- zzzcpan 8y agoBut it doesn't grow anymore, C ecosystem is out of anyone's control. The committee is as irrelevant as it can be.
- pjmlp 8y agoNot when you need to use certified compilers.
- jandrese 8y agoI'm not too worried about C growing. It seems well and truly stuck in the 70s. All the growth is from people doing whatever they want on top of C.
- andrewmcwatters 8y agoWell, you're in luck. A lot of people don't care to use anything beyond C89.
- iforgotpassword 8y agoOh please. I've been using C99 for almost 5 years now.
- jschwartzi 8y agoI would never go back. And the amount of campaigning I did at my last job to use C++11 was because all the new language features to allow compile-time checking helped us not do dumb things at run-time. Except for the stupid TI compiler, which is still stuck on C++03.
- pjmlp 8y agoOr C++98, depending on the chip, if some of their docs are still up to date. :\
- augustk 8y agoI'm one of those. If maximum portability is desired C89 is still king.
- pera 8y agoStandard C grows at a rate of 1nm per decade... but in any case, I'm confident that your compiler will always let you select the version you like the most.
- nn3 8y agoMy understanding is that a lot of undefinedness in the original c89 standard came from a desire to support non 2 complement machines like Burroughs. Of course in hindsight that was a total mistake. Yes would be a great idea to come up with an modern update to the c standard that is specified on let's say the level of java.
- rootbear 8y agoAs someone who was on X3J11 at the time C89 was ratified, I can tell you that supporting 1s-compliment architectures was necessary. The standard would not have been approved if 2s-compliment had been mandated. Standards are all about compromise, even when you know that that will cause trouble later.
- ropeadopepope 8y ago> I can tell you that supporting 1s-compliment architectures was necessary Can you elaborate on that point? Why was it necessary then, but not still necessary now?
- rootbear 8y agoSorry, I didn't mean to imply that there are no 1s-compliment systems still around. The comment I was replying to suggested it was a mistake to support 1s-compliment architectures because that was the source of some of the Undefined Behavior (UB) that so troubles C. My point was that not supporting 1s-compliment wasn't an option then (and probably is not one now.) After all, without 1s-compliment support, how could I write new C apps for the Apollo Guidance Computer? I would like to give an example of how supporting both 1s- and 2s-compliment is the source of a specific UB, but I can't take the time to do that right now, regrettably. Similarly, supporting both Big Endian and Little Endian was necessary. As was supporting ASCII, EBCDIC, and probably Fieldata and five level Baudot (looking at you, trigraphs). All of this generality made it hard to say anything useful in some areas, so you end up in some cases just calling it Undefined, since there was no consensus on how it should be defined.
- esaym 8y agoI didn't know the C std was still being modified/enhanced. Interesting. Does sound scary.
- rootbear 8y agoAll ANSI standards are required to be maintained and updated periodically, or they lapse. That can be as simple as rubber stamping the last standard, but in practice, there are usually a few tweaks needed.
- andrewmcwatters 8y agoIt hasn't been updated in any meaningful sense beyond C89, though.
- hood_syntax 8y agointtypes was pretty big, right? I know it's just one header, but it's an important thing to have in the standard for a systems language.
- pjmlp 8y agoC'mon it even has some kind of primitive generics support nowadays, which other modern languages keep ignoring.
- platinumrad 8y agoC11 generic selection is basically not worth using and almost worse than not having "generics" at all. It exists so tgmath.h can be written without implementation magic but tgmath.h is kind of a garbage fire itself.
- bumblebritches5 8y agoCould you possibly be more out of the loop, and yet confident you're in the loop? C99 is a huge leap, then there's _Generic, _Atomic, etc in C11.
- antirez 8y agoI've the feeling that only a language fork from the GCC/clang teams could have any chance to success. It looks like the C standard committee is not interested in changing point of view.
- posterboy 8y agoIt's like the word "standard" has meaning, e.g. "not changing". Not even for good reasons? Well "good" is a subjective statement. Objectively, only changes to maintain the standard would qualify, which is a contradiction. I guess, adding to the standard library wouldn't be a problem, but I guess you are rather leaning on security concerns and all that jazz that comes up when rust is discussed. C is open as can be but you can't tack on a change of the semantics without changing the language. Then, what do you still need C for?
- pcwalton 8y agoGCC and Clang have already essentially forked the C language. Perhaps the most important C project—the Linux kernel—is written in the GNU dialect of C.
- pjmlp 8y agoAlthough Google has managed to compile it with clang, as they removed gcc from Android.
- pcwalton 8y agoRight, that's the point—a useful C compiler nowadays needs to be a GNU C compiler, not a standard C compiler.
- pjmlp 8y agoTo reinforce your point that is also how Microsoft decided to go on Azure Sphere. https://www.mediatek.com/products/azureSphere/mt3620 https://www.mediatek.com/products/azureSphere/mt3620
- deleted 8y ago
- backpropaganda 8y agohttps://github.com/ziglang/zig https://github.com/ziglang/zig and Jonathan Blow's JAI language seem to be contenders for C replacement.
- bokglobule 8y agoArguably the Rust language is a replacement candidate for 'C' (and C++).
- 1bent 8y agoI'm studying Rust, though wouldn't claim I know it yet. I believe Rust is an effort to create a language in which you can write code that's as fast and portable as C; and yet, for which, the compiler can help you coordinate a "single owner permitted to write" strategy for thread safety -- at the expense of increased bother and fuss coaxing your program into that pattern. I wouldn't call it a C replacement; while I'm glad to have Rust available, I prefer concurrency (when I need it at all) via processes, rather than threads, with no shared memory.
- kbenson 8y ago> while I'm glad to have Rust available, I prefer concurrency (when I need it at all) via processes, rather than threads, with no shared memory. Are you implying that Rust can't do multi-processing through processes?
- 1bent 8y agoI don't know yet. I've heard about at least two languages that adore threads, and have issues with multi-processing. Perl6 has threads in its runtime helping its GC --- and calling fork kills its programs. I don't know yet if rust has threads _required_ in a fashion that makes it crash if you try to fork(). perl6 cannot fork; and golang behaves badly if you fork in a program that uses goroutines. Ends up unhappy, since shared library manipulation is kinda dependent on the process loader. Oops. I'm tempted to hope that the Rust lang devs have kept use of threads out of its required runtime. To answer your question, I didn't mean to imply that rust cannot call fork(2), and I hope it isn't true. I meant to say that I wouldn't call Rust a C replacement; it's an effort to solve a problem (threads with shared heap or stack) that C flat doesn't attempt to consider. I love C, and feel obliged to learn Rust.
- copper_think 8y agoAt Microsoft the compiler team (Visual C++) and the Windows team are joined at the hip. I'm sure the same was true at Sun. This can lead to good decisions about undefined behavior that I hope would make Linus smile. I recently learned of one such good engineering decision (I hope I'm remembering it correctly). Let's say you have a struct with an int32 and a byte in it. That's 5 bytes, right? But the platform alignment is a multiple of 4 bytes, so there's 3 bytes of padding (sizeof the struct is 8 bytes). If we stack-allocate an array of 11 of these and zero-initialize with = { 0 }, what would you expect to see in memory after initialization? It turns out the answer was that the first element of the array would have its 5 bytes zeroed, but the 3 bytes of padding would be left uninitialized. Then, the remaining 10 elements of the array would be zeroed with a memset that actually zeroed all 80 remaining bytes. It sounds weird but this is a legal thing to do from the standard's perspective. All they're obligated to zero out are the non-padding bytes. This UB was leading to disclosure of little bits of kernel memory back into user mode because Windows engineers assumed that = { 0 } was the same as leaving the variable uninitialized and then memsetting the whole thing to zero. Nope! The compiler team fixed this by always zeroing out padding too. Problem solved. There are some cases where it's not quite as fast. But it's the right engineering decision by the compiler team for their customers, both internal and external.
- oblong 8y ago> This UB was leading to disclosure of little bits of kernel memory back into user mode because Windows engineers assumed that = { 0 } was the same as leaving the variable uninitialized and then memsetting the whole thing to zero But what on earth were they doing with the padding bits?
- kabdib 8y agoTotal guess, but they could have been including them in a hash function or struct equality check (equality with memcmp, hash by just grabbing bytes, etc.). That would not have gone well with uninitialized padding :-)
- buckminster 8y agoIt's not uncommon for a function to use a supplied output buffer as scratch space. So the padding could have contained pretty much anything.
- armitron 8y agoI can't be the only one that 99.9% of the time doesn't care _one iota_ about these mythical optimizations that compilers can introduce by exploiting undefined behavior. I just want to write straightforward, predictable code that tries its best to be safe but for one reason or another have to stick with C. Is there a guide/reference on how to disable these optimizations in modern compilers? A list of GCC/Clang arguments that disable as much of this as possible would be greatly appreciated. I've seen a lot of posts and articles discussing C undefined behavior but almost nothing describing how to counter it.
- pcwalton 8y agoSure. Compile at -O0. A huge number of seemingly-trivial optimizations depend on assuming that undefined behavior will never happen. The number of optimizations that don't depend on UB in any way is quite small. For example, if you want to get pedantic about it, even automatic promotion of local variables to registers is exploiting undefined behavior—who's to say you didn't have a pointer that just happened to point to one of them?
- kazinator 8y agoOf course you can have a pointer to a local variable, and that variable can still spend portions of its lifetime in a register. What won't necessarily work as expected is a shadily obtained pointer to a local variable, like by displacing &i to try to point to the same location as &j. If &j doesn't occur anywhere (or does occur, but in a way that is optimized away), j may not even have a location.
- jcranmer 8y agoHow do you reflect the fact that pulling a pointer to a value "out of thin air" may not make any sense without either banning it entirely or defining some step of the process to be undefined behavior?
- esrauch 8y agoMost languages have no concept of undefined behavior and they still have compilers and runtimes that can do a huge amount of optimizations.
- Paul-ish 8y agoHow much of the UB in C is carried over to C++? Does the C++ standards committee carry a lot of the UB over into C++, or try to fix the issue? If C++ doesn't have the same UB, maybe the solution is to compile C code with a C++ compiler.
- jcranmer 8y agoThe intent of C++ is that the subset that is valid C code ends up being largely semantically identical in C++ and C. There are some differences, particularly related to the type system (for example, in C, a ternary expression is an rvalue, where it can be an lvalue expression in C++). Most, perhaps all, of the undefined behavior is retained C++. There is potentially a case where C eliminated undefined behavior that C++ retained--the "union trick" for getting access to the bits of a float was made legal in C99 but the C++ language (modified to support unrestricted unions) suggests that it is undefined behavior in C++.
- mabynogy 8y agoWe need a -Osafe flag.
- notacoward 8y agoLike phkahler, I don't want C to grow any more. I've been a C programmer for a long time, the vast majority of my day-to-day work is still in a C codebase, and I expect to continue working in C for a while yet. Nonetheless, its time has passed. It will be around for a while, just like FORTRAN and COBOL are, but there's no good reason for new code to be written at that poor level of abstraction. Even for systems software - what I write - there are always better choices that provide higher-level data and control structures. They variously use garbage collection, reference counting, ownership rules, or whatever you call that hot mess C++ has. Writing safe, secure C code is certainly possible, but it's too much unnecessary work - especially in the concurrent and/or parallel world that any non-trivial code has to live in nowadays. That said, I really wish proponents of other languages would get their stuff together about creating libraries that can be used from other languages. A library written in C can be used by anyone else. Many other languages are avid consumers of this functionality, many advertise it as a key feature, but very few return the favor by producing reusable code. The industry doesn't need such Balkanization. If you're one of the very many people who look down their noses at C and want to get rid of it, do your part.
- blobjectivism 8y ago>A library written in C can be used by anyone else. This is because of how much of the unix clone ecosystem has been built around C workflows, and this wasn't true on Windows until linux compatibility was developed on it. >If you're one of the very many people who look down their noses at C and want to get rid of it, do your part. convincing linus and sysadmin greybeards to modernize linux and is no small task, until then we'll all still be just be scripting over archaic C apis "science progresses one funeral at a time"
- notacoward 8y ago> This is because of how much of the unix clone ecosystem has been built around C workflows Absolutely correct. > convincing linus and sysadmin greybeards to modernize linux That's not what I'm suggesting. Anything that's already written in C can and probably should continue to be so. What I'm suggesting is that people who prefer to work in other languages should have an easy way to make their work available beyond their own language community. I'm not talking about interpreted/scripting languages here. I'm talking about compiled/systems languages. Stuff that gets linked together, or that should be able to use some sort of dlopen/FFI back and forth fluidly despite multiple languages being involved. There's some work to be done there, but everyone seems to prefer hiding in their own language bunker instead of reaching out to others.
- raverbashing 8y agoIt's really annoying It seems the committee is more interested in adding optimization gotchas (that nobody cares about for the most part) while making it impossible to write correct code without relying on convoluted code (which denies the benefit of "extra optimization") and "UBing everything" C is broken, let's face it - Builtin strings are a joke. stdlib functions are even worse. - It was not developed with modern systems in mind. - DIY Memory management: malloc/free is like giving a kid a chainsaw to play with. Sure, every C programmer had to work out with this crap. But yeah, please complain how my static void * should really be a volatile char * . Idiot. (of course it's not wrong to complain, but that's like complaining about a faulty blinker in a car without brakes). And of course the compiler is going to ignore the 'volatile' part of the pointer because f. you and item 7c of paragraph 3 of the spec lets us do that, even though it is blatantly stupid to do so. Rust is a step in the right direction. Making string and (memory) slices a fundamental part of the language helps. You can have null-terminated memory pointers for interoperability, but having it as a fundamental construct eliminates several problems. Also greenlets/threads/multiprocesses.
- pjmlp 8y agoC was broken from the beginning if you go search what compiler researchers from Algol school of languages had to say about it during the early 80's. "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 However, C and UNIX are symbiotic, so there is no way we can get rid of C, while keeping POSIX based OSes around us. So, it will stay around for many decades until quantum computers or something else eventually takes off.
- jancsika 8y agoNaive question: Are there any compiler optimizations that -Ox makes possible which couldn't have been written originally as readable C code?
- BeeOnRope 8y agoFor sure. Consider idiom recognition, where a compiler takes a series of operations that implement some operation that isn't expressible as a language primitive and turns it into a single machine instruction that performs that operation. Rotate built up from a couple of shifts for example. Consider auto-vectorization for which the language has no direct support. Consider inlining and value-propagation and all the simplification can that occur when you combine them. Basically compile any non-trivial C program without optimization (or without some interesting optimization option) and with optimization, and look at the assembly. In many cases you wouldn't be able to write C code at all to reproduce the optimized version with the not-optimizing compiler. In other cases you could, but at the cost of code duplication or other things that would reduce the quality of your code.
- jpfr 8y agoYou are invited to bring forward concrete proposals for changes to the C standard in the relevant committee. http://www.open-std.org/jtc1/sc22/wg14/ http://www.open-std.org/jtc1/sc22/wg14/ Since UB allows the compiler to do anything in those situations, we can reduce the amount of UB without breaking existing “legal” code. By actually defining more behavior. If a proposal is reasonable needs to be discussed in the standardization committee. All other discussions are a nice hobby. But eventually moot.
- vyodaiken 8y agoI will.
- wolfgke 8y ago> You are invited to bring forward concrete proposals for changes to the C standard in the relevant committee. My proposal: make the future versions of the C standard freely available (not only drafts).
- xorblurb 8y agoThose kind of discussions have various effects, some of which I believe to be far from moot. First, they permit that some people even take notice about this situation. Few developers read the standard and even less write it or follow the discussions to change it (are they even open?) or write a compiler for it. The rationales are not even tracked [1]. It actually would be insanely hard to get a good understanding of those subjects by e.g. just reading the standard, without having those kind of discussions on forums typically used by more devs than just a few dozens of compiler writers... [1]: but while I'm thinking about it, an impressive independent book as been written by Derek Jones: The New C Standard: An Economic and Cultural Commentary http://www.knosof.co.uk/cbook/cbook.html http://www.knosof.co.uk/cbook/cbook.html
- vyodaiken 8y agoHere is a start. https://docs.google.com/document/d/1xouelPcphQ-o7DmdSwz5UcL42M6bdA3t93Nm_5Hbomc/edit?usp=sharing https://docs.google.com/document/d/1xouelPcphQ-o7DmdSwz5UcL4...
- buserror 8y agoI also think that the committee is out of touch. C99 was an awesome improvement on the language, and since then, it went downhill. We don't need the extra weird C11 syntax things, duplicating of existing libraries; we want tools that scope C better, or extensions that have been proven helpful and stable (one such example is the switch(x) { case A...B: } from gcc!). I want strict boundary checking, I want an array base type that can't be cast as a pointer. I want some sort of scoping mechanism (ie, blocks), I want a bit of standardisation of memory barrier and such. I want #pragma once FFS -- it was proven a good idea for 25 years. Basically there's tons of stuff that could help make the language better -- C99 did that; C99 is a masterpiece for example on how you can statically initialise extremely complex data into a single block, without having to use code. It's used all over the Linux kernel (amongst other thing; for example my own simavr is heavily based on that feature [0]). * Standardise the stupid bit order in bitfield declaration FFS. I've been wanting to use that feature for 30 years and I can't because they 'forgot' to make up their mind! * Coroutine standardisation would be awesome (stack swap primitives, with boundary checks etc).... * gcc 'sub functions' (or a derivative) would be awesome if improved to make them safe. * Reference counted allocator (basically, get libtalloc and roll it in [1]) There are so many things that could be improved, without diverging into weird stuff nobody needs (complex math anyone??!?!). * In fact I want SIMD. I don't need these complex types. [0]: https://github.com/buserror/simavr/blob/master/simavr/cores/sim_megax.h https://github.com/buserror/simavr/blob/master/simavr/cores/... [1]: https://talloc.samba.org/talloc/doc/html/index.html https://talloc.samba.org/talloc/doc/html/index.html
- bumblebritches5 8y agoSo you want C++, and need to leave that shit outta C. The only extensions I want from C2x is generic data (beyond void pointers) and type safe variadic functions. The rest you can build yourself, or use some other language. Edit: You HAVE SIMD without doing anything special, at least in Clang.
- maxlybbert 8y agoI can’t say much for most of your wish list, but I believe C11’s thread model/atomic operations include a standard memory barrier ( http://en.cppreference.com/w/c/atomic/atomic_thread_fence http://en.cppreference.com/w/c/atomic/atomic_thread_fence ).
- maxlybbert 8y agoI found the title a little misleading: there isn’t much about where the standard is heading or even where it’s been. I would vote for the title “a rant on undefined behavior in C.” —— Simple example: the article complains that unsigned integer overflow is defined in C while signed integer overflow is not. There is very little in the article about this except for the claim that the performance for incrementing a signed int should match the performance for incrementing an unsigned int. The writer refuses to believe otherwise, even though he accepts that undefined behavior “supposedly” allows the compiler to omit overflow checks. It’s the “supposedly” that makes this a rant. The article’s sources mention that Clang does omit overflow checks and that the Clang team believes this makes loops up to 20% faster (“up to” because the optimization can’t be applied to all loops, and the performance increase will depend on how tight the loop is, i.e., how much overhead there is in incrementing and testing the loop variable in comparison to the loop body).
- loup-vaillant 8y ago> the article complains that unsigned integer overflow is defined in C while signed integer overflow is not. The article complains that signed integer overflow is undefined, while unsigned integer overflow is not. There's a difference. > the Clang team believes this makes loops up to 20% faster I'd love to see the benchmarks they conducted. Anyway, undefined behaviour was the wrong way to solve the problem. The C language should have grown a `for` loop that's more than a thin veneer of syntax sugar over `while`. Seriously, how hard would the following be? for (int i = start; end)) { // body } No explicit comparison, evaluate `start` and `end` at the start of the loop, maybe cast them to the type of i, and increment implicitly. While loops can handle the weird cases. There you have your 20%.
- maxlybbert 8y ago> > the article complains that unsigned integer overflow is defined in C while signed integer overflow is not. > The article complains that signed integer overflow is undefined, while unsigned integer overflow is not. There's a difference. You are correct. > > the Clang team believes this makes loops up to 20% faster > I'd love to see the benchmarks they conducted. It wouldn’t surprise me if they have micro benchmarks. I convinced myself they were telling the truth based on crude instruction counting: a loop must be converted into something like: - loop body (assume it contains at least one machine instruction) - increment loop variable - (for unsigned): clear overflow flag - test loop variable, exit loop if appropriate - goto beginning of loop I believe they don’t have to actually check the overflow flag, it’s OK to let the overflow happen. But they do have to clear the flag to avoid a spurious error if the flag gets looked at later. I’m no expert, so it’s possible this oversimplifies things, but removing the instruction to clear the flag does remove a big chunk of this loop. But it’s a big chunk only because it’s a tight loop. For the record, I happily use the foreach loop constructs in C++, D, Java, C#, Python, Perl, etc. but I originally avoided them until I saw a comment by Walter Bright that there is no performance penalty in D (the compiler rewrites the loop appropriately; there may be a penalty in Java because the feature might be defined in terms of their relatively heavy iterators).
- cryptonector 8y agoFaintly?!
- cryptonector 8y agoStrict aliasing needs to go out the window. That's for starters.
- burfog 8y agoThat may be. It sure is broken. What they did: Things of similar base type (so ignoring "signed", "const", etc.) may alias. Everything may alias with "char". What would make more sense: Any unsigned integer type may alias with any type of the same size; this is not transitive. (so a floating-point type may not alias with a pointer type, but there likely are unsigned integer types that may alias with both) Structs may alias other structs from the beginning until the point at which the types of their content diverge. Even with that better default, you'd still want easy ways to override it in both directions.
- pm24601 8y agoWow... I remember a simple, elegant language. The C, I knew had close to a 1-1 mapping to assembly. The compiler didn't overthink things. How can you muck this up! I thought it was C++'s job to turn something simple/elegant into an indecipherable mess!