Show HN: Xr0 – Vanilla C Made Safe with Annotations
xr0.dev
We've been working on Xr0 for the past couple of months and are excited to share an early prototype.
@betz47 and I are here to answer any questions.
xr0.dev
We've been working on Xr0 for the past couple of months and are excited to share an early prototype.
@betz47 and I are here to answer any questions.
#include <stdlib.h>
foo(int **pp) [
if (@pp) {
.alloc *pp;
} else {
.undefined;
}
] {
*pp = malloc(4);
// or: int *qq = malloc(4); *pp = qq;
}
It seems that `*pp` doesn't even work in the abstraction. (I originally tried structs, but realized that Xr0 currently doesn't support them.) This is crucial because you can't just do pattern matching against abstractions and axioms, there should be some sort of tactics that massage abstractions so that appropriate axioms can be used if any. Even keeping track of non-variable places (here `*qq`) is not trivial, please consider how to handle the commented code. #include <stdlib.h>
foo(void **pp) [ .alloc pp[0]; ] {
void *q;
q = malloc(4);
pp[0] = q;
}Does code using the annotations compile as is?
What's most distinctive about Xr0 is it's built to be reliant on the programmer to structure the code in a way that is amenable to verification of safety. Another unique thing is that the annotation language is C-like and should be easy to pick up for programmers.
The code will compile if the annotations are stripped, and we'll have a command to do this soon.
void alloc1() //[ .alloc result; ]
{
return malloc(1);
}Then it will be unnecessary to strip annotations.
The advantage of putting the annotations in comments is C compilers can compile the code right away, which may make it easier for existing projects to adopt it.
The disadvantage is that the annotations become second-class citizens, and will never feel native. Placing them in comments also means that they cannot have their own comments unless we introduce another comment sequence for the annotation environment. Again, it means that we can't use the preprocessor, so conditional compilation is impossible.
So basically, we're trading some compatibility for nicer, native-like source. For us, "beauty is the first test", and we believe that there's no way for the source to be beautiful if we put the annotations in comments.
void alloc1() //[ .alloc result; ] // it allocates!
If the annotation opening block is always `//[` you have intuitive C comments within that line (either another line comment or a /* / section inside that line. Multiline /*/ comments starting on that line wouldn't be allowed but that would be following the established C preprocessor rules and thus won't surprise any user nor editor/syntax-highlighter.That said, I do agree with your concerns around first-class-ness of syntax annexes for language extensions.
The main advantage of using some form of hack exploiting comments is that the existing ecosystem (syntax-highligters, code formatters, auto completion, ...) would keep working. But how code auto formatters would work depend on the details. E.g. I didn't try how would popular auto formatters handle the example above.
I can make even languages such as Java leak memory even though it shouldn't be possible, nominally, so just knowing that something is returning heap-allocated memory somehow doesn't seem to help much.
But I have only skimmed the page, so hopefully I am wrong!