4 ms·
I feel like the most user friendly solution for Use After Free is to just use Reference Counting. Basically, just copy Objective-C's "weak_ptr" design. For eve
by randyrand 4y ago
I feel like the most user friendly solution for Use After Free is to just use Reference Counting. Basically, just copy Objective-C's "weak_ptr" design.
For every allocation, also store on the heap a "reference object", that keeps track of this new reference.
struct reference_t {
// the ptr returned by malloc
void* ptr;
// the stack/heap location that ptr was written to.
// i.e the reference location.
void* referenceAddr;
}
Reference track: every time ptr is copied or overwritten, create and delete reference objects as needed.
If ptr is freed, visit every reference and call *referenceAddr = NULL, turning all of these references into NULL pointers.
- jhgb 4y agoWouldn't this, if applied by default, complicate Zig's easy C interoperability? I don't quite see how plain C code could play well with this.
- kaba0 4y agoReference counting is an incomplete solution it itself - it leaks circular references, and on multithreaded programs it is quite slow. Though some optimizations could be done statically to improve on that. All in all, a mark-and-sweep will likely be faster (in throughput at least) let alone a good GC.
- randyrand 4y agoCircular references wouldn’t matter. The programmer would still decide when to free the object, not some automatic system. Manual memory management, with Ref Counting just to add some additional Use After Free checks.