4 ms·
C safety addons like this (there have been many) is that they don't prevent extracting raw pointers from controlled pointers. Optional memory safety isn't. > I
by Animats 3y ago
C safety addons like this (there have been many) is that they don't prevent extracting raw pointers from controlled pointers. Optional memory safety isn't.
> If this can be reasonably retrofitted to existing libraries and projects
That's the problem.
If you want to fool around in this space, consider revisiting C++ to Rust conversion.
There's something called Corrode, which compiles C to a weird subset of Rust full of objects that implement C raw pointers. The output is verbose and unmaintainable.
What's needed is something that can figure out how big things are and who owns what, possibly guessing, and generate appropriate ideomatic Rust. Now that LLMs are sort of working, that might be possible.
Can you ask Github Co-pilot to look at C code and answer the question "What is the length of the array 'buf' passed to this function"? That tells you how to express the array in a language where arrays have enforced lengths, whicn includes both C++ and Rust. With hints like that, ideomatic translation becomes possible. Bad guesses will result in programs that subscript out of range, which is caught at run time. But guesses should be correct most of the time, because C programmers tend to use the same idioms for arrays with lengths. Forms such as "int read(int fd, char* buf, size_t buf_l)" show up often.
Using LLMs to help with tightening up existing code might work.
- thradams 3y agothe cake implementation cannot be mapped to rust. I am not rust specialist but one concept for instance is that a owner pointer owns two resources at same time, the memory and object. In rust it is one concept. Owner pointers take on the responsibility of owning the pointed object and its associated memory, treating them as distinct entities. A common practice is to implement a delete function to release both resources, as illustrated in Listing 7: Listing 7 - Implementing the delete function #include <ownership.h> #include <stdlib.h> struct X { char *owner text; }; void x_delete(struct X *owner p) { if (p) { /*releasing the object*/ free(p->text); /*releasing the memory*/ free(p); } } int main() { struct X \* owner pX = calloc( 1, sizeof \* pX); if (pX) { /*...*/; x_delete( pX); } }
- pitaj 3y agoI don't see why that couldn't be represented like this in Rust: struct X { text: Option<Box<str>>, } fn main() { let pX = Box::new(X { text: None }); // automatically dropped (freed) at end of scope }
- thradams 3y agoIn cake object and memory are two resources. We can for instance, delete the object and reuse the same memory. For instance, this code is correct. #include <ownership.h> #include <stdlib.h> struct X { char * owner text; }; void x_delete(struct X * owner p) { if (p) { free(p->text); free(p); } } int main() { struct X * owner p = malloc(sizeof(struct X)); p->text = malloc(10); free(p->text); //object text destroyed struct X x2 = {0}; *p = x2; //x2 MOVED TO *p x_delete(p); //no need to destroy x2 }
- Arnavion 3y agolet p: Box<X> = Box::new(X { ... }); let x2 = X { ... }; // Moves x2 into the same memory as the first X. // The first X is automatically dropped as part of this assignment. // Also consumes x2 so x2 is not available any more. *p = x2; // Drops the X that was originally assigned to x2 and then moved into p. drop(p); // No need, nor is it possible, to destroy x2.
- masklinn 3y agoAnd as bonus there is no temporal hole where you could access a null or dangling text. Here it is as a runnable snippet: https://godbolt.org/z/fc4Gfxrfd https://godbolt.org/z/fc4Gfxrfd
- thradams 3y agoIn cake there is no temporal hole, we cannot reuse the deleted object. This prevents double free and use after free. int main() { struct X * owner p = malloc(sizeof(struct X)); p->text = malloc(10); free(p->text); //object text destroyed //p->text is on uninitialized state. //cannot be used (except assignment) struct X x2 = {0}; *p = x2; //x2 MOVED TO *p x_delete(p);
- thradams 3y ago>Can you ask Github Co-pilot to look at C code and answer the question "What is >the length of the array 'buf' passed to this function"? That tells you how to >express the array in a language where arrays have enforced lengths, whicn >includes both C++ and Rust this is the way you tell C what is the size of array. void f(int n, int a[n]) { }
- Animats 3y agoYou can write that in C, but it doesn't really do anything. It's equivalent to void f(int n, int a[]) { } Why? So that you can write void f(int n, int m, int a[n][m]) { } which declares a 2-dimensional array parameter. In that case, the "m" is used to compute the position in the array for a 2D array. The "m" doesn't do anything. This is equivalent to writing void f(int n, int m, int a[][m]) { } This is C's minimal multidimensional array support, known by few and used by fewer. Over a decade ago, I proposed that sizes in parameters should be checkable and readable I worked out how to make it work.[1] But I didn't have time for the politics of C standards. [1] http://animats.com/papers/languages/safearraysforc43.pdf http://animats.com/papers/languages/safearraysforc43.pdf
- JonChesterfield 3y agoyou're missing the word "static" to have that work as intended. Option (2) at https://en.cppreference.com/w/c/language/array https://en.cppreference.com/w/c/language/array Parameters like `const double b[static restrict 10]` for at least 10 long and doesn't alias other parameters. Syntactically this is pretty weird.
- a_t48 3y agoDo you have source on this syntax? Does the `[n]` actually do anything here? Fooling around in godbolt, `void f(int n, int a[n]) {` is the same as `void f(int n, int a[]) {` and doesn't appear to change assembly or generate any warnings/errors with improper usage.
- unnah 3y agoIt looks like standard C99 variable-length array (VLA) syntax: https://en.cppreference.com/w/c/language/array#Variable-length_arrays https://en.cppreference.com/w/c/language/array#Variable-leng... The major difference is when the array is multi-dimensional. If you don't have VLAs then you can only set the inner dimensions at compile time, or alternatively use pointer-based work-arounds. Even in the case of one-dimensional arrays, a compiler or a static analyzer can take advantage of the VLA size information to insert run-time checks in debug mode, or to perform compile-time checks.
- quatrefoil 3y ago> Optional memory safety isn't. Optional memory safety is, when you can opt an entire project into a "strict" mode, and this becomes trivially verifiable by others. I imagine that's the goal here. Optional security is a problem only when you need to remember a million different rules and gotchas, because you will inevitably miss a spot. But if it's a global toggle, it's pretty good. "Use '-fmemsafe' for C/C++" is as tractable as "don't use 'unsafe' in Rust". Yeah, as you note, library compatibility is an issue. But it's an even bigger issue when bootstrapping a new, safe language: you gotta implement the libraries from scratch, and you never really get to full parity with C/C++. Getting it done for the top 10 most-used libraries would make a spectacular difference in itself. I should note that I'm not a huge believer in "saving" C/C++ as the memory-safe language of the future - I think there are lingering cultural problems around the standards that we had no luck overcoming for decades - but I also don't think the duo is going away any time soon, so might as well expend some effort on making it a safer tool.
- nanolith 3y agoThese are tools. How well they work largely depends on the discipline and processes followed by the development team using them. If the concern is that raw pointers can be extracted from controlled pointers, then the development team needs to check for this. No tool is perfect, but tools like these can be used effectively to reduce attack surfaces and improve safety. Even languages like Rust make memory safety optional. One can drop into an unsafe block and perform all sorts of abominable things. Such escape hatches are necessary to color outside of the lines when one must do system software development or optimize software beyond what the compiler can do on its own. At some point, the developer must be trusted to learn the tool or to use discretion when considering something like unsafe. In both cases, a development team can peer review these choices. What makes me interested in tools like Cake and similar tools is that these bring us closer to being able to use proof assistants to build up reasoning about the times when we must color outside of the lines. Whether C, C++, or Rust, being able to import code into a proof assistant or extract efficient code from a proof assistant can further assist us when our use cases exceed what is possible with the safety features in our language or tooling.
- naitgacem 3y agoBad guesses will result in programs that subscript out of range, which is caught at run time. That is sadly not always the case.