by yosefk · 9y ago · 0 comments
How about we use 24 bits of data pointers to keep the array size, or 1 bit to indicate "this is a pointer with a size" and 23 bits for the size, and then our load/store with index instructions, as well as freshly added pointer arithmetic instructions, trap when the index exceeds the size? Instead of using bits in instruction pointers to not let one of many kinds of buffer overflow create valid instruction pointers? No good?
How about we use 24 bits of data pointers to keep the array size, or 1 bit to indicate "this is a pointer with a size" and 23 bits for the size
That would imply a pretty big granularity for the sizes - if the maximum size is 4GB, the minimum is 512 bytes. Packing more efficiently might help (for instance, the way segmentation limits on x86 are) but introduces hardware complexity. then our load/store with index instructions, as well as freshly added pointer arithmetic instructions, trap when the index exceeds the size?
You've described x86 segmentation pretty clearly here. It's been around since 1978, but most of the mechanism has been disabled in x86-64. Two of the registers involved are still used for things like per-CPU data and stack-smashing protection:http://www.software-architect.net/blog/article/date/2015/03/...
Pointer auth and control flow integrity techniques cover most (all?) memory corruption flaws, including memory lifecycle errors (which are probably the most common modern source of vulnerabilities). Built-in buffer bounds checks do not.
You don't need any special instruction support to do bound checked memory access. Write in Rust or Swift or whatever, and you're already making buffer overflows "impossible". The buffer overflows are already out there, in billions of lines of C and C++ code, and since we can't rewrite all the code, we should mitigate it as best we can.
It'd require somewhat more ISA & compiler changes but it'd solve more problems that just the one problem they solve, and I think the security of this would be easier to demonstrate, too.
If you store the bounds separately (full runtime bounds checking) you lose efficiency on code which inherently can not overflow the bounds, and code where you have a large number of small objects (let's say you have 400GiB of 64-byte objects) with a known size. If you switch to a new language, great! But you obviously lose access to your existing code, which is a non-starter.
How about we only use languages that (as you propose) work with memory slices not naked pointers and where the concept of a null pointer does not exist
How about we only operate on memory slices after checking boundaries
You can think yourself superiour and turn up your nose, but the fact remains that many people have perfectly good reasons to write and maintain C. Your haughty commentary has no impact on that.
You may think that bounds checking is the answer to all situations, but if you're writing a realtime system, there's often no point in running the program if it can fail from an out of bounds read or write anyway. In a flight control system, or an ECU, there is often nothing productive about crashing. You need to verify your pointer logic, instead of hoping your program will crash.
I love how most of the comments are, in essence "you're too dumb to use C". Just check the pointers right? Too bad people who think they are too smart do get bitten by those issues
I guess that's why people won't use other technologies beyond C in embedded systems (they do use)
There's really only one replacement for C right now and it's Rust. If you don't like or can't use Rust (for whatever reason) you're gonna stick with C, and when you consider how much work has gone into making Rust a viable C alternative, it's clear we're a long way away from having a healthy ecosystem of C replacements.
Rust, Go, Oberon, Common Lisp, Scheme, D, Java
- Lack of library and platform support
- Lack of tooling
- Lack of standardization
Go:
- Lack of tooling
- Lack of standardization
- GC
- Slower
- Higher memory usage
Oberon (Ada, RTS, etc.):
- Lack of library and platform support
- Lack of tooling
- Lack of standardization
- Obscure
Common Lisp/Scheme:
- Lack of library and platform support
- Lack of tooling
- Lack of standardization
- GC
- Slower
- Higher memory usage
- Obscure
- Can't access arbitrary memory locations (?)
Java:
- GC
- Slower
- Higher memory usage
- Can't access arbitrary memory locations
I'm not really a C apologist, but I am pretty irritated with the near-constant calls for C deprecation. It's a lot easier to say, "C sucks!" than it is to do something about it, and I think we should at least internalize how difficult replacing C will be before we go around castigating people for continuing to use it.
How that would benefit stability? I'd argue that having an equivalent of NaN for integer types would benefit array index computations more.
Any time where you have an important distinction between the address of a valid object, and a non-address (next address at the end of a linked list, leaf node of a tree, failed initialization of a pointer). Zero is also used to terminate strings, for similar convenience/efficiency reasons.
C compilers and static analyzers together tend to catch possible null dereference bugs with near certainty these days, so people don't tend to ship them these days, if they make any effort at all. I have not encountered a null pointer dereference which wasn't typo-related in... I don't remember the last time it happened.
If you really want to be certain downstream users of your API won't struggle with it, put the null check in your sample code with a fat comment which says "This is NULL 0.001% of the time, and it really hurts when you don't handle that".
ALGOL, of course, is the sort of language which doesn't have pointer arithmetic. I would agree that a language without pointer arithmetic should not have NULL pointers, if only because it doesn't make any sense for there to be an abstract reference to an object which can not be used with functions designed for it.
In a language with integer pointers, like C, you check for null at allocation time. I've also seen people consider functions which could return NULL pointers to return something like an option type, where null is considered None, and everything else considered Some.
Don't burn the trees down, just because you don't want anyone else to have wood. Some of us will craft furniture, not smog.