11 ms·
Checked C: int a[5] = { 0, 1, 2, 3, 4}; _Array_ptr<int> p : count(5) = a; // p points to 5 elements. My proposal for C: int a[5] = { 0, 1, 2, 3,
by WalterBright 4y ago
Checked C:
int a[5] = { 0, 1, 2, 3, 4};
_Array_ptr<int> p : count(5) = a; // p points to 5 elements.
My proposal for C:
int a[5] = { 0, 1, 2, 3, 4};
int p[..] = a; // p points to 5 elements.
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
https://github.com/Microsoft/checkedc/wiki/New-pointer-and-array-types#checked-single-dimensional-arrays https://github.com/Microsoft/checkedc/wiki/New-pointer-and-a...
- KingLancelot 4y agoFat pointers are a good idea, but I think it’d be a better idea to store the start and evd pointers instead of a pointer to the first element and a size_t.
- 1980phipsi 4y agoWhy?
- masklinn 4y agoI'd guess it's because that's what C++ does. Though it's unclear to me that C++-style iteration (where you increment a running pointer until you reach an EOI pointer) is much used in C.
- WalterBright 4y agoStoring two pointers into the same array is a memory safety problem. It's hard to implement a borrow checker when there are two live mutable pointers into the same memory object.
- int_19h 4y agoI would expect the borrow checker to treat a slice as a logical pointer to all elements that are addressable through it, regardless of how it's implemented under the hood.
- int_19h 4y agoThis pattern originated in C; C++ iterators were specifically designed to fit it when it was already established.
- andrewmcwatters 4y agoI've never heard of a good argument for this as compared to a capacity, length, and data struct. If you have a start and end pointer and you move either one, you don't know what the original size of the allocation was.
- KingLancelot 4y ago[dead]
- ntrel 4y agoD doesn't store capacity in each slice. This allows a slice to fit in registers more often. Instead for GC allocated slices, the GC will store metadata before the first element of the allocation. That does make it slower to access, particularly when the slice points past the first element, but it is cached to speed up repeated accesses to the same allocation. Repeated appending can be further speeded up by using a dedicated Appender type.
- sirwhinesalot 4y agoIt's a shame this will never get through the insane bureaucracy that is the C standards committee. Had they listened to you all the way back in 2009, C11 might have been a game changer the same way C++11 was. Alas. At least there's BetterC ;)
- WalterBright 4y agoThis proposal could very well save C, as array bounds overflows are consistently the #1 cause of corruption bugs in shipped C software.
- ryao 4y agoIn theory, sound static analyzers could be another method by which this is achieved in existing projects. They claim to be able to catch all instances of this. Some examples of sound static analyzers include: https://www.absint.com/astree/index.htm https://www.absint.com/astree/index.htm https://github.com/NASA-SW-VnV/ikos https://github.com/NASA-SW-VnV/ikos https://github.com/static-analysis-engineering/CodeHawk-C https://github.com/static-analysis-engineering/CodeHawk-C I have planned to try using them on OpenZFS for a while, but I am still busy reviewing and fixing reports made by conventional static analyzers. I plan to look into these next. That said, at least one of them claims to be able to prove the absence of issues in C that checked C’s documentation explicitly says it cannot prevent. The obvious one is use-after-free.
- WalterBright 4y agoI don't know how a static analyzer could do this: size_t i = some_function(); // gets return value from environment array[i] = 3;
- deleted 4y ago[deleted]
- ryao 4y agoA sound static analyzer in theory can check the safety of that by examining all code paths and complain whenever any exist where it is possible to go out of bounds. It is called sound because the absence of a complaint means it has proven the absence of the issues it is meant to catch. If it has a limitation that prevents it from finding a proof, it will complain about the lines that could not be proven safe.
- elcritch 4y agoI like your version, but how do you annotate the size from existing things like "main(int *argv, argc)" to know that the size is argc?
- kelnos 4y agoYou just... annotate it? Like, I don't see what the issue is. You just write int main(int argc, char **argv : count(argc)) {} If you're already creating an extension for C, then it's fine to "change" its signature in this way. Or am I misunderstanding your objection? Sure, the compiler can't verify that argv does indeed have argc elements, but hopefully we can rely on the kernel and C runtime to populate things properly. If not, I would say it doesn't matter that the compiler can't help you here; your system is screwed beyond repair already. At the very least, the compiler can emit bounds checking to ensure that you don't try to access past argc elements in that array, which is still valuable. Edit: it occurs to me that you were talking about WalterBright's version, not the version in Checked C. But I think my comment still applies: if you're changing how things work, then you just keep changing how things work to cover cases like this.
- WalterBright 4y agoCreate an adapter: int main(int argc, char **argv) { return myMain(argv[0 .. argc]); } Strings can be done like this: char [..] s = p[0 .. strlen(p)];
- elcritch 4y agoHmmm, the "[..]" is a fat pointer right? Couldn't you just say: int main(char [..] args) Maybe with some way to tell the compiler the correct parameter order if needed? Perhaps: int main(char [..argc] argv) I'm curious if ELF and other formats have enough info to figure that mapping out?
- WalterBright 4y ago> the "[..]" is a fat pointer right? yes > if ELF and other formats have enough info to figure that mapping out? No, as there is no semantic connection between `argc` and `argv`.
- gavinray 4y agoYou might show an example of how to use D's templates (if it's possible) to emulate the more complex example of computing the bounds from an input argument char *strncpy(char * restrict dest : count(n), const char * restrict src : count(n), size_t n) : bounds(dest, (_Array_ptr<char>)dest + n);
- WalterBright 4y agohttps://news.ycombinator.com/item?id=34085835 https://news.ycombinator.com/item?id=34085835
- gavinray 4y agoI meant something more like this (although this doesn't QUITE compile, but it's close I think): static ubyte* [n*2] requires_ptr_twice_as_large_as_input(n : uint) ( ubyte*[n] input, ubyte*[n*2] output, ) { output[0..n] = input[0..n]; output[n..2*n] = input[0..n]; return output; } void main() { ubyte[10] input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; ubyte[20] output = new ubyte[20]; requires_ptr_twice_as_large_as_input!(10)(&input, &output); }
- ntrel 4y agoThis compiles: ref ubyte[n*2] requires_ptr_twice_as_large_as_input(uint n) ( const ref ubyte[n] input, ref ubyte[n*2] output, ) { output[0..n] = input[0..n]; output[n..2*n] = input[0..n]; return output; } void main() { ubyte[10] input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; ubyte[20] output; requires_ptr_twice_as_large_as_input!(10)(input, output); import std.stdio; writeln(output); assert(output == input ~ input); }
- gavinray 4y agoThank you, neat to see it could be done (I had a hunch!)
- Xeoncross 4y agoYes, needing a fixed-sized array seems like a fairly common need to me. a := [5]int32{0, 1, 2, 3, 4} // Go let mut array: [i32; 5] = [0, 1, 2, 3, 4]; // Rust
- masklinn 4y agoC has fixed-size arrays. The problem is that it doesn't use them much, and gets rid of them as soon as possible: int a[5] = {1, 2, 3, 4, 5, 6}; printf("%d\n", a[8]); in clang, this warns, by default, on both misuse sites "excess elements in array initializer" and "array index is past the end of the array". Which is good. However things go downhill as soon as you pass the arrays downstack: #include <stdio.h> static void foo(int *a) { printf("%d\n", a[8]); } static void bar(int a[]){ printf("%d\n", a[8]); } static void qux(int a[6]){ printf("%d\n", a[8]); } int main() { int a[5] = {1, 2, 3, 4, 5}; foo(a); bar(a); qux(a); } no warning anywhere, even under "-Wall -Weverything".
- klodolph 4y agoYeah, so the syntax is a bit funny but you can actually get the bounds checking you want here: #include <stdio.h> static void qux(int a[static 6]){ printf("%d\n", a[8]); } int main() { int a[5] = {1, 2, 3, 4, 5}; qux(a); } I get, test.c:9:5: warning: array argument is too small; contains 5 elements, callee requires at least 6 [-Warray-bounds] I did not enable any warning flags at all, this is just default Clang on a Mac. If you want to catch the a[8] inside the function you need something else.
- Jasper_ 4y agoUnfortunately, `int a[static 3][static 4]` does not work.
- klodolph 4y agoThat’s because you’d write it as: int a[static 3][4] You only need to put static on the outermost type, the one that decays into a pointer. Same thing applies if you need restrict: int a[static restrict 3][4] which is ugly but gets the job done. Think of int a[3][4], when used as a function parameter, as “pointer to int[4]” (the 3 disappears completely), and think of int a[static 3][4] as “pointer to first of at least 3 int[4]”. (Not trying to defend C here, just explaining how C works.)
- tomcam 4y agoElegant, but of course it would be. It’s like you’re a trained professional or something ;)
- WalterBright 4y agonot my first rodeo! ABEL was the first language I designed (for Data I/O) back in the 80s. ABEL = "Advanced Boolean Expression Language", a language for programming PLDs (Programmable Logic Devices) which was a core business for Data I/O.
- bachmeier 4y agoThere's no substitute for decades of having programmers giving you (and shouting at you) blunt feedback on your programming language proposals.
- deleted 4y ago[deleted]
- WalterBright 4y agoPeople have never been shy with criticism!
- ryao 4y agoI did not know who you were at first. Would you have any thoughts on why another group made Golang when D already existed that you would be willing to share?
- acehreli 4y agoBecause Google saves us all.
- ntrel 4y agoBecause Go designers don't like OOP and didn't want low-level system programming as core features. They also were not keen on generic programming. Basically they wanted a simpler language. (Then they realized generics are useful, ironically their constraints add complexity). Also they wanted a segmented stack and coroutines + channels as language features.
- Animats 4y agoMany people have been down this road, including me.[1] Back in my 2012 paper, I listed prior art: • Microsoft source code annotation language (SAL) • Cyclone (AT&T research language) • SCC, the Safe C compiler. • Ccured • MemSafe The breakthrough will come when someone can create an automated system to convert unsafe C into safe something. AI may be far enough along to do that now. Every array in a valid C program has a size, defined by some expression known to the programmer. All you have to do is find that expression and tell the language about it. Most of the time this is easy. Sometimes it's hard. Sometimes it's impossible, in which case the program is probably a buffer overflow waiting to happen. [1] http://animats.com/papers/languages/safearraysforc43.pdf http://animats.com/papers/languages/safearraysforc43.pdf
- 0xbadcafebee 4y ago> an automated system to convert unsafe C into safe something Can't we just make macros to redefine types as some 'safe' type variant system, and #include "safetypes.h" or something? It's ugly, but searching your source code for 'int a[5]' and replacing it with 'INT(a,5)' might be dumb enough to work.
- friendzis 4y agoStatically allocated/sized arrays are relatively easy. The problem is dynamically allocated/sized arrays coupled with pointer "interface", which supports type casting.
- Animats 4y agoSomething smart enough to convert pointer arithmetic to slice expressions is needed. Most C pointer arithmetic is really just slice access. (Who invented array slices? It was a concept that came along much later than one might have expected.)
- kinggencha 4y agoI can already see it: > This C code is guaranteed to be safe. We had an AI go through it and make it safe. No, thanks. That sounds more like a problem than a solution.
- camel-cdr 4y agobtw C can already do this with VMTs: #define ARR_LEN(a) (sizeof (a) / sizeof *(a)) int a[5] = { 0, 1, 2, 3, 4}; int (*p)[ARR_LEN(a)] = &a; ARR_LEN(*p) // 5
- ryao 4y agoWhat does VMT mean? Virtual Method Table? When was this introduced to C? It looks like it was either in C89 or earlier according to both GCC and Clang. #define ARR_LEN(a) (sizeof (a) / sizeof (a)) int main(void) { int a[5] = {0, 1, 2, 3, 4}; int (*p)[ARR_LEN(a)] = &a; (*p)[6] = 1; return ARR_LEN(*p); // 5 } I had no idea this was possible in C. It is unfortunate that GCC seems to ignore this information. Clang on the other hand uses it.
- camel-cdr 4y ago> What does VMT mean? Variable modified types, they were introduces with VLAs (variable length arrays) in c99. C11 made both optional, and C2x mandates only VMTs.
- camel-cdr 4y agoAbout gcc ignoring it: gcc doesn't check array bounds at all, even `int x[2]; x[9] = 1;` doesn't produce a warning.
- ryao 4y agoOddly, it does catch it for the static example posted by someone else, but that is the only place where it catches it.
- uecker 4y agoYou need to use UBSan, then you get the bounds checking in GCC. I added this in 2015. https://godbolt.org/z/8ncEK3K5b https://godbolt.org/z/8ncEK3K5b
- uecker 4y ago