4 ms·
> How about doing what every other language of the last couple of decades does and just not allow them? I fully agree. But this is not without tradeoffs: elimi
by pcwalton 11y ago
> How about doing what every other language of the last couple of decades does and just not allow them?
I fully agree. But this is not without tradeoffs: eliminating undefined behavior pretty much means (a) the performance and runtime overhead of a GC (most languages); (b) forbidding malloc (verified "mission-critical" variants of Ada, C, etc.); (c) requiring programmers to learn a lot of new concepts (Rust†). I do think that there is rapidly becoming little reason to use C except for throwaway programs, and as an industry we need to be more open to (c) if we are ever going to move beyond making the same memory management mistakes we've been continuously making since the 1960s. But I also understand the reasons why programmers continue to choose C and C++.
† It's interesting to me that the biggest reason C++ programmers give for bouncing off Rust is fundamentally "I want my undefined behavior back", though very few actually word it like that.
- nickpsecurity 11y agoNo it doesn't: just means we know what it will do. It might still fail. It might still use a risky approach that allows high optimization. One can run old Pascal or Modula-2 with unsafe constructs to do anything C can do at high performance. Disallowing undefined behavior simply means the result is defined behavior. Might be safe, unsafe, any number of things. Just clearly that to developer and tools.
- pcwalton 11y agoThe point of the article is that precisely what you suggest will have extremely bad consequences for optimization. In your proposal, how do you propose to solve the load-load forwarding issue described in the article?
- pjmlp 11y agoBy taking the approach that in proper engineering quality should precede "anything goes" mentality.
- pcwalton 11y agoThat's very philosophical. It also fails to describe a technical solution to the problem. What is the specific solution to the specific load-load forwarding problem described in the article? This is a technical problem that needs a specific technical solution, not philosophy.
- pjmlp 11y agoI replied to it, indirectly. My position is the same as Tony Hoare, optimizations that exploit undefined behavior or contribute to possible malfunctioning programs are not to be done at all.
- pcwalton 11y agoThen you are making programs at least 2x slower, probably much more. That is untenable for many of us who are using C and C++. Customers will simply not accept e.g. browser engines that are 2x slower.
- twic 11y ago> Then you are making programs at least 2x slower, probably much more. Then how come programs written in languages which don't have undefined behaviour, like Java, are not 2x slower than those written in C?
- jerf 11y agoBecause they aren't C. I think it's not just that "undefined behavior can be optimized" in some abstract sense but that in particular C has the problem that its undefined behavior is where a lot of optimizations live. This is due to a combination of the language being very old now, and the hardware world having moved on a lot since it was (half-)specified. I really don't consider this a criticism of C per se or its designers. It is unreasonable to expect that a language specified in the 1970s would be tuned for either modern processors or have the benefit of an additional 40 years of collective experience. But it is, nevertheless, true. (It's part of why my position is very much that this entire approach just won't work. C is pretty much what it is; it can not be changed. It is literally easier to use another language than to try to specify a "friendly C" in 2016. And I don't say that because I don't know how large a task it is to start writing code in another language; I say that because people are really underestimating how much work Friendly C will take. I suggest that everyone observe that all who have now seriously tried to sketch what this would look like have immediately given up and totally surrendered. This means something.)
- nickpsecurity 11y agoI don't know enough about C language and compiler technology to give a specific recommendation on that. However, your other comment on Java hints that its a hold-over from bad language design. Also, I recall that Fortran compilers could optimize the heck out of code that C compilers held back on specifically because Fortran was more deterministic. The compiler knew what the code was doing and could guess where usage was going. C's properties made the same code a giant unknown due to effects of pointers. My overall point is that you can do an optimization from a set of statements with specific meaning at least as easy as you can from one with unknown meaning. So, a similarly low-level language with well-defined behavior could be optimized at least as much as C. However, since compiler faces uncertainty, the argument doesn't work the other way around. That's why C and its undefined behavior are bad by design. And hence all the discussions on workarounds like the OP.
- rbehrends 11y agoThis is really overkill. When I'm writing C, it's generally for one of two reasons: 1. Interoperability code with some library or the OS. 2. C as the closest thing to a portable assembler there is (e.g. to implement something like the Ruby interpreter). What I need for this isn't perfect safety; it's the ability to reason with some confidence about the code I'm writing (I may want to be able to rely on code review by merely mortal programmers or create my own tools to enforce this). If I wanted a safe, expressive language, I wouldn't use C in the first place. But right now even something as simple as a malloc implementation is riddled with traps even for reasonably skilled programmers. The Linux kernel uses -fno-strict-overflow and the FreeBSD kernel uses -fwrapv because the price to be paid by enabling the undefined behavior of -fstrict-overflow was too high.
- pcwalton 11y ago> 1. Interoperability code with some library or the OS. If by "interoperability" you mean "binding a more modern language to a C library", no argument there. Binding to C is a legitimate reason to write C. > 2. C as the closest thing to a portable assembler there is (e.g. to implement something like the Ruby interpreter). My question is: why do you want a portable assembler? Why not write Ruby in, for example, Go? It could certainly be done; look at JRuby, for instance. The vast majority of the time, the answer to this question is "performance". Which brings us back to the point of the article. Without compiler optimizations enabled by undefined behavior, you can easily lose 2x performance or more. A 2x performance loss for MRI is unacceptable. Malloc implementations embody this even more. In fact, it's kind of hard to think of any piece of code that's more performance critical than malloc. Many large applications (apps, games) spend 10%-20% of their time in the malloc and free functions. Allocator performance is so important that Facebook and Google have invested a huge number of man-hours into these two routines (jemalloc and tcmalloc respectively). It all comes down to performance again: under these extreme contraints, malloc simply can't afford a 2x performance loss. A Friendly C that produced slower code would have little chance of being adopted by allocator writers.
- rbehrends 11y ago> My question is: why do you want a portable assembler? Why not write Ruby in, for example, Go? It could certainly be done; look at JRuby, for instance. I would in principle like to do this in another language. Go isn't that, because Go's runtime makes some very specific assumptions (about things like stack layout and how it interoperates with syscalls). I may not want to be weighed down by these assumptions. For similar reasons, you may want to eschew JRuby, as it locks you into the JVM ecosystem (which is great if that's what you need, not so great if you don't want to suffer from the poor interop with non-JVM libraries). > The vast majority of the time, the answer to this question is "performance". The answer for me is generally that C is ecosystem-agnostic (what I'd expect from a portable assembly language; practically any other language that isn't called BCPL makes more ambitious assertions about its environment). Note that when I talk about interoperability above, I don't just mean interoperability between one language and C, but also to build bridges between two non-C languages (which is actually fairly important for some of my current work). Performance is a nice additional benefit. > A 2x performance loss for MRI is unacceptable. We have a few points to chew through here. First, I didn't say that undefined behavior is unacceptable. My problem is with undefined behavior that is difficult to reason about or to investigate (as an extreme case, when an infinite loop is turned into a no-op). Second, I don't buy the 2x performance loss. What makes most bytecode interpreters slow is how C compilers optimize the dispatch loop (poorly), not the exploitation of undefined behavior or the lack thereof. For example, -fwrapv has virtually no effect on Ruby's performance (even though clang/gcc unnecessarily disable strength reduction in some cases where they don't have to). Ruby would likely benefit a lot more from having its bytecode dispatcher rewritten in assembler LuaJIT-style (I'm talking about the LuaJIT interpreter, not compiler) than it would lose from not exploiting undefined behavior on a large scale. Finally: code that crashes really fast (or may suddenly start crashing for inexplicable reasons because the compiler was upgraded) is not going to help anyone. This is not a hypothetical concern; you'd be surprised how much C code doesn't survive an encounter with UBSAN.