r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
11
u/[deleted] 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?
There is no safe mode in profiles. This is what I am getting at with using std::sort as an example. section 2.3 (inferring safeness) of this paper explicitly points out that profiles don't want a safe/unsafe mode. To quote Herb's paper
We should not require a safe function annotation that has the semantics that a safe function can only call other safe functions.
– (Re)affirm design principles for future C++ evolution[P3446R0]
I hope you read this paper properly, because it explicitly calls that out too in section 3 (lifetimes are static typing) that how static typing and virality come as a package. And appropriate aliasing rules will mean a new "object model" (breaking stdlib) and we will need new kinds of references [with lifetimes] where profiles/compiler can enforce aliasing rules as pointers/old references won't enforce aliasing (unless you break backwards compatibility). we are back at safecpp.
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.
You can already do that. Profiles are just static analyzers + hardening. hardening will sacrifice performance, so mostly nobody uses it (except for debugging). static analyzers already enforce the easy parts of profiles. Just look at this huge list that clang-tidy enforces: https://clang.llvm.org/extra/clang-tidy/checks/list.html and compare that with profile "rules list" in section 2.3 of https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3081r0.pdf .
But the harder parts of profiles like lifetime/invalidation are just impossible to analyze.
You also seem to have some misconceptions about how safety, analysis and lifetimes work together. This is why you keep missing the point in so many threads. If profiles were as awesome as you think, the authors of profiles had almost 10 years to actually implement them and demonstrate their value. If someone could just make a static analyzer that can make analyze cpp with just a few annotations, then it would have already been done. Google/MS will easily pay tens of millions of dollars for that analyzer. In fact, the world will offer billions to find out the secret sauce that made whole program analysis feasible.
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.