4 ms·
This is similar to an approach I proposed in 2012.[1] Microsoft: "Rather than changing the run-time representation of a pointer in order to support bounds chec
by Animats 6y ago
This is similar to an approach I proposed in 2012.[1]
Microsoft: "Rather than changing the run-time representation of a pointer in order to support bounds checking, in Checked C the programmer associates abounds expression with each_Array_ptr<T>-typed variable and member to indicate where the bounds are stored. The compiler inserts a run-time check that ensures that deferencing an_Array_ptr<T>is safe (the compiler may optimize away the runtime check if it can prove it always passes). Bounds expressions consist of non-modifying C expressions and can involve variables, parameters, and struct field members. For bounds on members, the bounds can refer only to other members declared in the same structure."
Me: No array descriptors are generated by the compiler. The programmer tells the compiler the size of the array as an expression, and that expression is evaluated at the point in the program where the relevant declaration appears.
The Microsoft syntax is very Microsoft. It involves many leading underscores and long keywords. What I proposed involved adding C++ "&" references, and making lengths explicit in the places in C where you can write "[]". This is more concise. The basic idea is the same, though. Arrays have a length, which is defined by some expression on nearby variables, such as other fields of the same struct or other parameters in the same function call. C programs that work usually have a length around somewhere, but the compiler doesn't know where it is. Make that explicit, and you can check.
If you add ranges and references to C syntax, and provide length information, you can deal with one of C's three big questions - "How big is it?" (The other two big questions are "who owns it?" and "what locks it?")
This area is 10% technology and 90% politics, so you need sizable organizational support to make much happen.
[1] http://www.animats.com/papers/languages/safearraysforc43.pdf http://www.animats.com/papers/languages/safearraysforc43.pdf
- unwind 6y agoCool, thanks for working in this area of course. I had a quick look at your paper out of general interest (I'm probably a very conservative C programmer, I would rather move to Rust than modify C to make it safer). I didn't understand this part: int arr100[100]; // an array of 100 ints arr100& arr100r; // ref to array of 100 ints > This is valid C++ today. Is it really? That doesn't look valid to me, but I'm well and truly much more of a C programmer than a C++ one. I did try it on Godbolt, and got an error: <source>:2:1: error: 'arr100' does not name a type 2 | arr100& arr100r; // ref to array of 100 ints | ^~~~~~ Compiler returned: 1 Apologies if this is already mentioned somewhere in errata or something, or if I'm simply being too dense.
- pjmlp 6y ago> The Microsoft syntax is very Microsoft. It involves many leading underscores and long keywords. Not at all, Microsoft follows the ISO C guidelines to add new keywords to the language. Which is why you have stuff like _Generic, _Atomic,....
- sai_c 6y agoHonest question with no bad intent: Do you really think the "C community" as a whole will accept ANY syntax changes to the language? From my experience I would say no. I think it would be "easier" to force tools on them than any syntax change (even if really really minor).
- pjmlp 6y agoWhat matters is WG14.
- jstarks 6y agoTell that to Annex K... Getting something into the spec isn't enough.
- pjmlp 6y agoIndeed, but getting into the spec is the first step to matter at all. And you can get Annex K, on Windows. On the other hand, exactly because of the way some C developers are against anything that helps security, is why Microsoft, Google, Oracle, ARM, Apple are all pursuing projects to have hardware memory tagging. On Solaris SPARC, it already doesn't matter what those more religious developers think about bounds checking and similar issues.
- Animats 6y agoDo you really think the "C community" as a whole will accept ANY syntax changes to the language? No. That's why I didn't push on this. I have some disagreements with the design of Rust, but it's a big step forward.
- johnisgood 6y agoWhy not just Ada/SPARK? Rust and its syntax I believe is troublesome (heavy use of symbols being the major reason, e.g. in Rust it is completely normal to have 6 symbols next to each other). I often look at some code written in Rust and I cannot tell what it is supposed to do, it is way too hidden from me. It is almost akin to reading Perl. Reading someone else's code written in Ada/SPARK is not a problem, even without knowing much about the language. Mind you, Ada/SPARK has been out there for quite some time now, and it has been a "big step forward" of what people were unaware, unfortunately. Only if Ada/SPARK got the hype of Rust... there would not be much of a need for Rust. Ada/SPARK would have an ideal ecosystem, perhaps even another compiler, and so on.
- bialpio 6y agoPrior art from Microsoft which leverages similar idea, but using macros: https://docs.microsoft.com/en-us/cpp/code-quality/best-practices-and-examples-sal?view=msvc-160 https://docs.microsoft.com/en-us/cpp/code-quality/best-pract... They also have SAL annotations for "what locks it?".
- Animats 6y agoThat's been a problem with verification systems. If the annotations are not well integrated into the language, they're viewed as a nuisance. Especially if the compiler neither enforces them or uses them to help optimization.
- bialpio 6y agoThe enforcement does not necessarily need to come from the compiler - it's not uncommon to introduce additional required processes / checks that new code needs to pass (ex. "tests need to pass", "at least one reviewer must approve a change"). The compiler probably won't be able to leverage those annotations since there's always a potential for mismatch between annotations and code (but the verification system should be able to catch it) - I agree that integration into the language would be needed here, but that's not likely going to happen with C/C++ so I'm open to using a solution that's not perfect. :-)