4 ms·
Interesting. The important development is the porting tool which automatically infers the size of objects. Many people, including me [1], have proposed safer va
by Animats 8y ago
Interesting. The important development is the porting tool which automatically infers the size of objects. Many people, including me [1], have proposed safer variants of C. The problem is converting legacy code. They've made some progress, but not enough to use yet.
The important result is in Table 1 of the actual paper.[2] The converter looks at pointers, and classifies them as "pointer", "array of known size", or "unknown". "Unknown" ranges from 49% to 76%. You can't convert an existing code base automatically with that low a success rate.
The big design mistake in C was "pointer" and "array" being the same thing syntactically. Trying to undo that has taken decades.
Their converter is on Github.[3] This is a Microsoft project.
The syntax they use is rather clunky:
void copy(char ∗dst : bytecount(n),
const char ∗src : bytecount(n),
size_t n);
That's halfway between C and Pascal/Ada/Rust.
I proposed more C-like syntax like this:
void copy(size_t n, char (&a)[n], const char (&b)[n])
It's annoying that you need the parentheses, but that's C/C++ syntax.
I wanted to put C++ style references into C so you could pass arrays as arrays, rather than degrading them to pointers. I also wanted to add slices of arrays, so you could pass a slice of an array as an array. Those are standard features of most later languages, and handle most of the cases for which pointer arithmetic is used. C doesn't have the expressive power to even talk about those things unambiguously. So they can't be checked either at compile time or run time.
The hard job is converting legacy code by automatically inferring the information that C source code lacks. It's good that they're trying. But a 50% success rate indicates they're just getting the easy hits. If anybody ever gets a good array size inference system, it would be a huge win. Then existing C could be converted to something better. Not necessarily "Checked C". Rust or Go would be options.
[1] http://www.animats.com/papers/languages/safearraysforc43.pdf http://www.animats.com/papers/languages/safearraysforc43.pdf
[2] http://www.cs.umd.edu/~mwh/papers/checkedc-incr.pdf http://www.cs.umd.edu/~mwh/papers/checkedc-incr.pdf
[3] https://github.com/Microsoft/checkedc-clang https://github.com/Microsoft/checkedc-clang
- mpweiher 8y ago> The big design mistake in C was "pointer" and "array" being the same thing syntactically. Couldn't agree more. I actually tend to think that C is totally fine for me, but "forget" that this is really the C that is embedded in Objective-C. Meaning we have basic collection classes like NSData (bytes), NSArray (objects), NSString (well, strings, not the same as bytes) that take care of these things, yet are close enough to the metal that you can still largely treat them as C. Well, maybe not all of them, but particularly NSData.
- pjmlp 8y agoI never understood why it would be so hard to write ptr = &var[0] like in any sane systems programming language. Is saving to type those extra 4 characters worthwile the CVE mess we got into?
- mpweiher 8y agoNot sure it's quite that easy. Wouldn't you have to have a size field so the size of the array is known? What integer is the size field? And now the array is a struct, and I dimly recall that passing structures by value came later. Or do we do it all in the type-system? Except we don't really have much of one. And Pascal just showed what a dumb idea having the size of the array encoded in the type was, with functions that take arrays of different sizes incompatible with each other. Anyway, I think it's really kind of the other way around: it's not that C has arrays and pointers and wanted there to be compatibility. C doesn't actually have arrays, it only has pointers. What passes as arrays is a minimal bit of syntactic sugar that doesn't affect the semantics, which are just plain pointer semantics.
- pjmlp 8y agoIt is so hard that a large majority of system programming languages do it. That Pascal example is getting tired by now. It only applies to the first edition of ISO Pascal (superceded by ISO Extended Pascal), and improved by any systems language that was born afterwards.
- 8y ago
- duneroadrunner 8y ago> "Unknown" ranges from 49% to 76%. Yeah, this is interesting. They're saying they can't determine whether a pointer targets an array buffer or not? Perhaps they might want to take a look at the (long neglected) "C to SaferCPlusPlus" translator[1] which can do this. (It was an unexpectedly taxing undertaking though.) It converts C arrays and allocated buffers used as arrays into memory safe implementations of std::array<>s and std::vector<>s, so failure to properly identify them would generally result in output code that wouldn't compile. The examples they give of problematic code in the paper: void f(int* a) { *(int**)a = a; } and f1(((int*) 0x8f8000)); don't strike me as the kind you would often encounter in real-world code. > The syntax they use is rather clunky The output code of the "C to SaferCPlusPlus" translator replaces the types and declarations with macros[2] that can be redefined with a compile-time directive to either use the safe C++ implementation, or revert to the original unsafe native C implementation. The argument being that using macros instead of custom syntax makes the source code more versatile. And existing C programmers already "get" macros. [1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus-AutoTranslation https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla... [2] https://github.com/duneroadrunner/SaferCPlusPlus/blob/master/mselegacyhelpers.h https://github.com/duneroadrunner/SaferCPlusPlus/blob/master...
- Animats 8y agoSaw this in the translated "SaferCPlusPlus" output examples. static void string_set(char** out, const char* in) What happened there? Where are the array types? Wrong place to look? If inference can't make a definitely good decision, maybe translators should guess, conservatively. That is, if it looks like something needs an array type parameter, make it an array type parameter with subscript checking. Then run tests on the translated program and see if that works. That's what humans do on such code. Machine learning has potential here. For any array in a working program, there must be some expression of some variables that expresses the size of the array. If humans can't find that expression, the program is unmaintainable and probably has a bug. There are really 3 cases. 1. this is a pointer, and it's never subscripted or offset. That's a pointer to a single instance of something. 2. this is a pointer which is subscripted or offset, and we can tell from context how big the array is. 3. This is a pointer which is subscripted or offset, but auto-translation fails to figure out how big the array is supposed to be. The problem is to convert (3) into (2). I tend to think that a good metric for C code quality is how hard that is. If it's not obvious by looking how big something is supposed to be, there's probably a potential bug. [1] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTranslation/tree/master/examples/lodepng/lodepng_translated/src/lodepng.cpp https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...