3 ms·
At least for heap allocations that's taken care of by the 'General Purpose Allocator'. AFAIK it's still possible to return a dangling reference to stack memory
by flohofwoe 5y ago
At least for heap allocations that's taken care of by the 'General Purpose Allocator'. AFAIK it's still possible to return a dangling reference to stack memory though, but this should be fixable without going full Rust, even C compilers have a warning for this nowadays.
- pcwalton 5y agoZig's "general purpose allocator" achieves use-after-free protection by quarantining memory pages forever: i.e. it never reuses heap addresses. This is unsuitable for production because keeping even a single 16 byte allocation alive on a 4kB page will leak the whole page. If it were possible to solve use-after-free this way, Microsoft/Apple/Google would have done it long ago for C++.
- flohofwoe 5y agoThis allocation strategy encourages to keep allocation frequency low, which is always a good thing ;) But more seriously, I guess the general-purpose-allocator is mainly useful for catching memory corruption issues during development, despite the name.
- pcwalton 5y agoIf it's designed to be a development tool, then Address Sanitizer for C/C++/Rust does everything that Zig's general purpose allocator does and more. If you follow the strategy behind Zig's "general purpose allocator" to its logical conclusion, you get Google's HWASAN for C++ [1]. It is much more advanced than an allocator that simply quarantines memory forever and uses probabilistic checking to mitigate the problems of quarantining, trading soundness for performance. [1]: https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html https://clang.llvm.org/docs/HardwareAssistedAddressSanitizer...
- smaddox 5y agoI think that's the debug allocator, but I don't know if that's even implemented yet.
- pjmlp 5y agoC compilers have had a warning for that since the late 1990's, when I was actually writing C into production. Ever heard of Purify and MemCheck? Most people keep ignoring such tooling to this day, if it isn't enforced by the language, or CI/CD pipeline, it just gets ignored. Just like the strings/arrays, naturally one can make use of something like SDS in spirit, most don't.
- xxpor 5y agoPurify is proprietary and memcheck is dynamic, so you need to be able to run the software on the host machine.
- pjmlp 5y agoSo what if it is proprietary, there are other alternatives if you don't like to pay for tooling. As for the rest, I really don't get the point, given the plethora of such tooling for C and C++, both as free beer and commercial for the last 25 years.
- xxpor 5y agoYet we have basic vulnerabilities in C and C++ code ALL THE TIME. Empirically, even if perfect tooling exists, you still have to go out of your way to use it. The right place to enforce these things are in the language and compiler (which I think you agree with). Make it impossible to make mistakes. Which is also why I'm not that excited about Zig. But the point is the tooling story for C isn't a land of puppies and roses, even in 2021.
- pjmlp 5y agoIn fact, because most people don't use them, yet another reason for Zig to have been designed better in regards to use-after-free. Right now, in regards to security in systems programming, Zig is no better than using Modula-2 compiler.
- marcodiego 5y ago