r/cpp • • Oct 24 '24 • Take My Silver 1

Why Safety Profiles Failed

https://www.circle-lang.org/draft-profiles.html
180 Upvotes

347 comments sorted by

View all comments

Show parent comment

-2

u/germandiago Oct 26 '24 edited Oct 26 '24

Stop focusing on replacements for sort. Without annotating functions like std::sort as unsafe, how would the profiles know to actually reject code using unsafe functions like std::sort?

Seems like a valid point, I was here thinking of solutions and could not find an immediate one at least.

With fat iterators that know its container (run-time) in debug mode would be a solution.

I admit I did not find a solution to this, since beg, end form a range but those params have nothing to do with each other. That function should be, then, banned as unsafe in this case.

Only in safe-cpp. With profiles, there's literally no safe mode. To have that, you need coloring and we go back to section 2.3.

Ok, let's talk about color intensity: it would not be better to "color-annotate" and reuse safety analysis to the maximum amount existing code? Or just forget about, I do not know, easily billions of lines of code? I think I would have a preference for the former solution if it is possible: you say it is not, because some annotations bother you, banning unsafe functions bothers you, but it does not bother you that people have to rewrite code in another dialect for just getting an analysis...? I think here the cost/benefit should heavily favor the former, even with some annotations, as long as old code stays analyzable (read: analyzable, not safety-compliant). You say it is impossible. Ok, I get your point. You have an informed decision, definitely. But I think that then banning the most problematic areas is a second strategy that it is still valid. That cuts off the analyzable as safe surface, true. But it is too much to win to let it go...

A lot of code is too expensive to analyze. That is why stroustroup recommends just giving up if its too hard and reject the code as too complex.

I am aware of that. The strategy would be to look for alternatives, not to try to analyze pairs of iterators, right? Ban those. It is an unsafe abstraction.

C++ type system doesn't have lifetime information. So, analyzers must make conservative judgements. Too much valid code is rejected by default due to false positives.

If you add annotations to help, then, it will reduce false positives. But, now you need to annotate a lot of code, which defeats the purpose.

These two depend a lot on how much annotation you need because going full split-world directly will make you pay the price of non-analyzable older code...

As for resources on analysis/safety, You can try reading https://gavinhoward.com/2024/05/what-rust-got-wrong-on-formal-verification/ (particularly the Local Reasoning section) which explains why you must analyze one function at a time. There's also a short article https://manishearth.github.io/blog/2017/12/24/undefined-vs-unsafe-in-rust/ explaining why UB is global, but safety is local.

Thanks, the links are very informative. Learning some more now.