Live data from Hacker News

How (memory) safe is Zig? (2021)

scattered-thoughts.net

1–10 of 88 posts

Re: How (memory) safe is Zig? (2021)

#2
> But it does not nearly approach the level of systematic prevention of memory unsafety that rust achieves.

Unless I gravely misunderstood Zig when I learned it, the Zig approach to memory safety is to just write a ton of tests fully exercising your functions and let the test allocators find and log all your bugs for you. Not my favorite approach, but your article doesn't seem to take into account this entirely different mechanism.

Re: How (memory) safe is Zig? (2021)

#3
There is a third category of memory and other software safety mechanisms: model checking. While it does involve compiling software to a different target -- typically an SMT solver -- it is not a compile-time mechanism like in Rust.

Kani is a model checker for Rust, and CBMC is a model checker for C. I'm not aware of one (yet!) for Zig, but it would not be difficult to build a port. Both Kani and CBMC compile down to goto-c, which is then converted to formulas in an SMT solver.

Re: How (memory) safe is Zig? (2021)

#4
post #2

> But it does not nearly approach the level of systematic prevention of memory unsafety that rust achieves. Unless I gravely misunderstood Zig when I learned it, the Zig approach to memory safety is to just write a ton of tests fully exercising your functions and let the test allocators find and log all your bugs for you. Not my favorite approach, but your article doesn't seem to take into account this entirely diffe…

I suppose you can even ship the test/logging allocator with your production build, and instruct your users to run your program with some option / env var set to activate it. This would allow to repro a problem right where it happens, hopefully with some info helpful for debugging attached.

Not a great approach for critical software, but may be much better than what C++ normally offers for e.g. game software, where the development speed definitely trumps correctness.

Re: How (memory) safe is Zig? (2021)

#5
> it seems impossible to secure c or c++

False. Fil-C secures C and C++. It’s more comprehensively safe than Rust (Fil-C has no escape hatches). And it’s compatible enough with C/C++ that you can think of it as an alternate clang target.

Re: How (memory) safe is Zig? (2021)

#6
post #2

> But it does not nearly approach the level of systematic prevention of memory unsafety that rust achieves. Unless I gravely misunderstood Zig when I learned it, the Zig approach to memory safety is to just write a ton of tests fully exercising your functions and let the test allocators find and log all your bugs for you. Not my favorite approach, but your article doesn't seem to take into account this entirely diffe…

What that means, though, is that you have a choice between defining memory unsafely away completely with Rust or Swift, or trying to catch memory problems by a writing a bunch of additional code in Zig.

Re: How (memory) safe is Zig? (2021)

#7

> it seems impossible to secure c or c++ False. Fil-C secures C and C++. It’s more comprehensively safe than Rust (Fil-C has no escape hatches). And it’s compatible enough with C/C++ that you can think of it as an alternate clang target.

I love this shameless self-promotion. ;)

Fil-C is in the cards for my next project.

Re: How (memory) safe is Zig? (2021)

#8
post #7

> it seems impossible to secure c or c++ False. Fil-C secures C and C++. It’s more comprehensively safe than Rust (Fil-C has no escape hatches). And it’s compatible enough with C/C++ that you can think of it as an alternate clang target.

I love this shameless self-promotion. ;) Fil-C is in the cards for my next project.

Thank you for considering it :-)

Hit me up if you have questions or issues. I’m easy to find

Re: How (memory) safe is Zig? (2021)

#9
post #7

Earlier quoted context omitted.

I love this shameless self-promotion. ;) Fil-C is in the cards for my next project.

Thank you for considering it :-) Hit me up if you have questions or issues. I’m easy to find

One of these days, a project will catch on that's vastly simpler than any memory solution today, yet solves all the same problems, and more robustly too, just like how it took humanity thousands of years to realize how to use levers to build complex machines. The solution is probably sitting right under our noses. I'm not sure it's your project (maybe it is) but I bet this will happen.

Re: How (memory) safe is Zig? (2021)

#10
post #3

There is a third category of memory and other software safety mechanisms: model checking. While it does involve compiling software to a different target -- typically an SMT solver -- it is not a compile-time mechanism like in Rust. Kani is a model checker for Rust, and CBMC is a model checker for C. I'm not aware of one (yet!) for Zig, but it would not be difficult to build a port. Both Kani and CBMC compile down to…

Any good primers on SMT solvers?
Post reply on HN