5 ms·
That’s a good goal! A 100% correct program which cannot guarantee a lack of memory safety bugs is not a 100% correct program, though, right?
by chriskrycho 4y ago
That’s a good goal! A 100% correct program which cannot guarantee a lack of memory safety bugs is not a 100% correct program, though, right?
- suremarc 4y agoThe absence of a proof of correctness is not a proof of incorrectness, strictly speaking.
- Schroedingersat 4y agoRust still needs unsafe blocks for full generality. Additionally there are many sources of panics that are unavoidable. The memory safety guarantees it makes are also not all bugs. It is equally possible to write a memory safe program in both (although far easier in rust), but impossible to make a panic-safe program in rust without avoiding a large portion of the language. A guarantee of the absence of certain classes of memory bugs is of no use to creating a 100% correct program if it also comes with a guarantee of panic on allocation failure and standard libraries that are full of panics. They are for different things. Rust is for keeping you completely safe from the worst things while you build something big like a web browser where the program doing something you didn't want is the worst failure state, and crashing is a mere inconvenience. It is there to replace C++ Zig is for making it possible to make yourself safe from all bugs if you try hard enough. It is for making small things that you understand fully where crashing is as bad or worse than memory errors like the firmware for a space probe. With the addition of some static analysis tools it could maybe replace or be useful in addition to something like Ada one day, and definitely replace C and some hand coded assembly.
- pcwalton 4y agoHow does Zig protect against stack overflow? To make a "100% panic-safe program" you must disallow recursion.
- csande17 4y agoZig basically does plan to disallow recursion: https://github.com/ziglang/zig/issues/1006#issuecomment-534660820 https://github.com/ziglang/zig/issues/1006#issuecomment-5346... More specifically, it'll force you to allocate memory for your recursive functions on the heap (and thus handle the possible resulting OOM condition).
- pcwalton 4y agoDo you disallow function pointers/closures/virtual methods too, since these can be used to smuggle recursion into the semantics without the compiler being able to statically detect it? You'd have to heap allocate every call to a function pointer, which would be very slow (see how much benefit apps get from PGO which for a long time mostly amounted to optimizing virtual calls).
- csande17 4y ago> Do you disallow function pointers/closures/virtual methods too [...] Maybe? Of these features, AFAIK Zig only has function pointers, and they already don't seem to interact very well with other parts of the language. I wouldn't be surprised if they restricted them to comptime-only usage or made "how much stack space does the function need" part of the pointer type or something.
- Schroedingersat 4y agoDisallowing recursion is one possibility (IIRC misra guidelines forbid it). In some circumstances, static proofs are also possible. IIRC it's still an open problem in zig as to what the possible language features will be while keeping this guarantee, but the goal is to be able to have recursion in some circumstances and still provide the possibility of stack-overflow-safety with a performance tradeoff, and to know when you've stepped out of the bounds where that is possible. Here's a thread I'm at least a year out of date on https://github.com/ziglang/zig/issues/1006 https://github.com/ziglang/zig/issues/1006 The general philosophy is to give the programmer tools to know what the consequence of a choice will be as far as that doesn't hamstring them overly. Ie. the compiler can tell you when a stack overflow is possible, and it's up to you to decide what to do about it (or whether it is wrong because you know something it doesn't).
- throwawaymaths 4y agoI'm pretty confident that zig will eventually get a static lifetime analysis tool. Such a thing should be writeable, because a human can reason about code. We also know that for example SEL4 does "sort-of" static analysis on c and gives memory safety guarantees.