r/cpp • • Oct 24 '24

Why Safety Profiles Failed

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

347 comments sorted by

View all comments

Show parent comment

-12

u/germandiago Oct 25 '24 edited Oct 25 '24

Not really. Profiles are targetting 100% safety without disrupting the type system and the standard library and by making analysis feasible for already written code. Something that Safe C++ does not even try to do, ignoring the whole problem. 

Choosing analyzing regular C++ has some consequences. But claiming that profiles do not target 100% safety is incorrect, repeated constantly and even suggested by the paper by pretending that C++ must match exactly the Safe C++ subset in order to be safe, using its mold as the target subset because yes, but is not true you need the same subset: what is important is for an analysis to not leak unsafety even if that subset is differenr.

Which is different from "if you cannot catch this because my model can, thennyou will never be safe". I find that argument somewhat misleading because it is just factually incorrect to be honest. What is true from Safe C++ model is that with relocation you can get rid from null at compile-time, for example. That one is factually true. But that breaks the whole object model at this point the way it is proposed at the best of my knowledge.

22

u/Dalzhim C++Montréal UG Organizer Oct 25 '24

Profiles are targetting 100% safety

Can you provide a source for that affirmation? Last I heard from Herb Sutter's talks, he was aiming for 90-95% of spatial, temporal, type and bounds safety.

[…] making analysis feasible for already written code. Something that Safe C++ does not even try to do, ignoring the whole problem.

Safe-C++ has quoted security papers showing it's way more important to write new code in a memory-safe language than rewriting anything at all in existing code. Definitely not ignoring the problem, just focusing where the bang for the buck is.

Choosing analyzing regular C++ has some consequences. But claiming that profiles do not target 100% safety is incorrect, repeated constantly and even suggested by the paper by pretending that C++ must match exactly the Safe C++ subset in order to be safe, using its mold as the target subset because yes, but is not true you need the same subset: what is important is for an analysis to not leak unsafety even if that subset is differenr.

You keep mentioning these two different subsets in various comments as if they were partially overlapping. But anyone who's read Sean's papers in whole can surely see that is not the case. Any safety issue correctly detected by Profiles is correctly detected by the Safe-C++ proposal. Doesn't work the other way though, Profiles detect a subset of what Safe-C++ can do (i. e. data races).

1

u/germandiago Oct 25 '24 edited Oct 25 '24

I do not have time for a full reply. 

Pretending that everyone can do what Google can do migrating to another language with the training, resources, etc. that this takes and with how expensive is to migrate code is calling for a  companies go bankrupt strategy.  

That paper is assuming too much from a single report and from a single company and trying to make us believe that all companies will freeze their code and magically will have trained people or all toolchains available, etc. 

I just do not believe that. 

There are a ton of reasons to not be able to do that (licensing, policies, training, toolchain adoption, existing code integration...). 

That paper only demonstrates that if you have the luxury of being able to migrate, train people, freeze all code, avilability and the money to do it and move on then, yes, maybe. Otherwise? Ah, your problem, right?

13

u/Mysterious_Focus6144 Oct 25 '24

Isn't his proposal for lifetimes opt-in?

1

u/germandiago Oct 25 '24

The point is to have a switch and make it opt-out. Safety by default for a certain set of profiles.

7

u/bitzap_sr Oct 26 '24

Sure, that can just be a profile in Safe C++. :D

0

u/germandiago Oct 26 '24

You would still miss the capability of analyzing old C++ code.

7

u/bitzap_sr Oct 26 '24 edited Oct 27 '24

You can have profiles for the unsafe side, and Safe C++ as well. Doesn't have to be one or the other.

-5

u/germandiago Oct 25 '24 edited Oct 25 '24

Yes. The papers from Bjarne and Herb Sutter in the strategy and tactics section. 

You do not need to be gifted to conclude thay "it exists a subset of current C++ that is safe", from which it derives that this subset, even if it is not equally expressive to a full-blown Rust copy, it is provably safe. 

I read ALL the papers including Sean Baxter's papers. What we find here is a try to shift the conversation to a false claim: that the profiles cannot be 100% safe by definition to push for the other alternative, of course obviating all the problems: a split type system, a new std lib and the fact that the analysis does not work for current code. I am sorry to be so harsh, but I find many people either misunderstanding what profiles want to offer (because they believe through Safe C++ papers that profiles must necessarily be unsafe) or... a not too honest assessment otherwise. 

I will take the former. Also, it seems that a lot of people who use Rust want this to be pushed to C++ and they do not seem to understand the profiles proposal completely and tag it as unsafe. 

No matter how many times it is repeated: the profiles proposals do not, in any case, propose a 90% solution that leaks safety.  

That is false. It can be discussed what every proposal can and cannot do, which is different, but tagging one proposal erroneously as "90% safe" is not the best one can do, more so when this is just not true. 

It should be discussed, IMHO, how expressive those subsets are, which is the real problem, and if the subset of profiles is expressive enough and how. 

Also, please, do not forget the costs of Safe C++: it is plain useless for alreafy written code.

16

u/hihig_ Oct 25 '24 edited Oct 25 '24

Profiles can only serve as a standardized set of compiler warnings, static analyzers, and sanitizers by definition. They are envisioned to achieve perfection someday. But what is the real benefit of standardizing this? Why have previous tools—compiler warnings, static analyzers, and sanitizers—that have existed for decades still not resolved all safety issues? Do you believe the reason is that they weren’t developed by a committee?

It seems clear that C++ code alone lacks the information necessary to resolve all memory safety issues. Profiles are likely to end up being either too strict, resulting in excessive false positives that discourage use, or too permissive, leading people to overlook their importance, as with previous tools. While I recognize there are aspects of Profiles that could be beneficial, even if they become standardized, when will they truly surpass the effectiveness of existing sanitizers and static analyzers that are already available?

-1

u/germandiago Oct 25 '24

Profiles can only serve as a standardized set of compiler warnings, static analyzers, and sanitizers by definition.

Who said that?

Why have previous tools—compiler warnings, static analyzers, and sanitizers—that have existed for decades still not resolved all safety issues?

Good question: it is a lack of push or it is impossible?

It seems clear that C++ code alone lacks the information necessary to resolve all memory safety issues

By that definition, Rust also: otherwise the "unsafe" keyword would not exist. There are perfectly safe patterns in Rust that cannot be proved also. See the problem? I think it is much more honest and productive to say: is there a sane subset that works?

Example: assume raw pointers only point to memory by default. Is this true of every project? No. It is a bad practice to do otherwise? Yes in almost all code I can imagine of.

Another example: what happens when I call a non-const function and there are iterators pointing to it? Conservative approach: invalidate or annotate (without redoing the whole std lib).

While I recognize there are aspects of Profiles that could be beneficial, even if they become standardized, when will they truly surpass the effectiveness of existing sanitizers and static analyzers that are already available?

Open question. :)

13

u/Nickitolas Oct 25 '24

> By that definition, Rust also: otherwise the "unsafe" keyword would not exist. There are perfectly safe patterns in Rust that cannot be proved also. See the problem? I think it is much more honest and productive to say: is there a sane subset that works?

Unsafety is very clearly delineated in rust, both syntactically and also in function interfaces. It's fairly encapsulated. Sean's poposal proposed adding similar explicit annotations to C++ but got a lot of pushback for wanting to "split" the language into safe and unsafe subsets. How else would you ever be able to partition these? You need clear, explicit annotations on functions for it.

Rust unsafe also tends to end up encapsulated and reused. e.g the voladdress crate for MMIO, or things like std map, vec and iterators being mostly safe (With optional unsafe operations for whoever needs them, usually for performance).

> Conservative approach: invalidate or annotate (without redoing the whole std lib).

Wouldn't this generate a LOT of false positives, leading to people not adopting the tooling? Or just adding annotaitons willy nilly.

24

u/foonathan Oct 25 '24

You do not need to be gifted to conclude thay "it exists a subset of current C++ that is safe", from which it derives that this subset, even if it is not equally expressive to a full-blown Rust copy, it is provably safe. 

Yeah, and that subset does not include common usage of map::operator[] without lifetime annotations/inference by looking at function body, as shown in OP's paper. This makes it a pretty useless subset.

2

u/germandiago Oct 25 '24

That would be a potentially valid point if no alternative solutions are found.  

But for example, by making reference escaping more restricted it can be solved as far as I understand (this is what subscripts do in Swift/Hylo I thinkbut take that with a grain of salt bc I did not do a full, super accurate analysis about it). 

Or by adding an annotation. Are annotations bad? If they are pervasive, yes. If they are not... compared to a new type system that is disjoint and all analysis useless in all existing code? Come on...

22

u/foonathan Oct 25 '24

But for example, by making reference escaping more restricted it can be solved as far as I understand

Yes, but that is a massive change in the way C++ works. I thought profiles meant to avoid this sort of thing?

Or by adding an annotation. Are annotations bad? If they are pervasive, yes. If they are not... compared to a new type system that is disjoint and all analysis useless in all existing code? Come on.

Oh, don't get me wrong, I'm not in favor of adding Rust style references to C++ either. My opinion is to embrace checking the low-hanging fruit with false negatives but as little false positives as possible. This does not make C++ guaranteed safe, but it catches common bugs. If you want guaranteed safety, use Rust.

But this hand-wavy "profiles can make C++ code guaranteed safe, and it's gonna be great, and we all get a pony" stuff by Herb and Bjarne is disingenuous. Show me an implementation, I'd be glad to be proven wrong. Right now it's at the "draw the rest of the owl" stage.

18

u/andwass Oct 25 '24

But this hand-wavy "profiles can make C++ code guaranteed safe, and it's gonna be great, and we all get a pony" stuff by Herb and Bjarne is disingenuous.

It is also dangerous. Imagine the bad PR that C++ would get if a feature was sold as "guaranteeing safety", while it not at all guarantee safety. I think that would be a death-blow to any safety and security claims made by the committee.

0

u/germandiago Oct 25 '24

Noone is going to do or approve that AFAIK. What all solutions, with their pros and their cons, are looking for is guaranteed safety and saying otherwise about any of those proposals is just misrepresenting them.

-1

u/germandiago Oct 25 '24

Yes, but that is a massive change in the way C++ works.

Compared to the Safe C++ proposal as it stands now, nothing is a massive change :)

-1

u/germandiago Oct 25 '24

That is in process, an implementation. And yes, not everything is going to be unicorns and happiness.

The thing is: how far it can be taken? It works?

Rust-like proponents say that the other model works today. But it is at the expense of splitting the language and making analysis useless out of their island.

That is such a high cost for a language with billions of lines of code that I doubt that it would justify the addition compared to the profiles solution.

But that is just my opinion and as you say,  an implementation is needed to verify the design.

What for sure it is true is that whatever solution we end up with it must not leak unsafety and noone is proposing that anywhere yet I keep reading arguments like "it will be 90% safe but not guaranteed". This is a misrepresentation.

A more accurate representation is: whatever the subset is it is 100% safe: is that subset reasonable and expressive enough?

That is the real question.

9

u/[deleted] Oct 25 '24

Don't take this the wrong way but I've seen you posting like 20+ comments in multiple threads all across reddit about safe C++.

Maybe you should stop posting on reddit and hackernews for a bit and kind of find some inner peace, man.

-3

u/germandiago Oct 25 '24

I aknowledge Safe C++ is a superset. I do not aknowledge that profiles leaks unsafety.

→ More replies (2)

12

u/throw_cpp_account Oct 25 '24

Profiles are targetting 100% safety without disrupting the type system and the standard library and by making analysis feasible for already written code.

They can claim whatever they want to claim. It's about time they demonstrated feasibility. Which, as Sean's excellent paper points out, seems pretty unlikely.

7

u/[deleted] Oct 25 '24

Profiles are targetting 100% safety without disrupting the type system and the standard library and by making analysis feasible for already written code. Something that Safe C++ does not even try to do, ignoring the whole problem.

  • Profiles aren't disrupting type-system/stdlib, because they haven't actually tackled any of the harder problems yet (lifetimes/aliasing/coloring).
  • "targeting 100%" is pointless without having a plausible idea to solve the problem (that can compete with borrow-ck / MVS).
  • As I already acknowledged, safe-cpp only works for newly written code. hardening will work for immutable legacy code. they can complement each other.

what is important is for an analysis to not leak unsafety even if that subset is differenr.

True, but profiles haven't done that yet even after so many years. As the paper shows in section 5, lots of the iterator based functions (eg: sort) are inherently unsafe. But there is no way to color functions as safe/unsafe.

-1

u/germandiago Oct 25 '24

"targeting 100%" is pointless without having a plausible idea to solve the problem (that can compete with borrow-ck / MVS)

Who says we have to target 100% with the same patterns and paradigms used in Rust? This could be a design mistake altogether.

as I already acknowledged, safe-cpp only works for newly written code.

Yes, and that is not good news. It is packing a lot of new functionality copied from Rust: drop, relocation, lifetimes...

immutable legacy code...

That is not even realistic, it will make a split between two worlds where all old code is basically "trusted" without any real analysis benefit.

True, but profiles haven't done that yet even after so many years

Ok, this is a valid point but I think there is still room for some innovation.

As the paper shows in section 5, lots of the iterator based functions (eg: sort) are inherently unsafe

std::ranges::sort(v)

But there is no way to color functions as safe/unsafe.

// This has been analyzed and compiled already, if guarantees violated,
// reject compilation of my code
import mymodule [[safety_guarantees]];

that composes transitively as far as I understand. You do not need safe/unsafe: you need to know what safety your modules provide. When you compile your code, then the code will detect unsafety according to your profiles (should be the default ones, lifetime, type, bounds, ...).

I also do not know how far the code can be correctly analysed, but if the operator[] is a problem for things like map or std::min it would be a good idea to solve the reference escaping problem for functions and not go fully viral in all the type system. Probably with some annotation and good defaults. Of course I do not know how well this works but I do know the cost of Safe C++ solution: another type system and another object model besides giving up analysis.

6

u/[deleted] Oct 25 '24

Who says we have to target 100% with the same patterns and paradigms used in Rust? This could be a design mistake altogether.

I literally said "plausible idea that can compete with borrowck/MVS (hylo)". your reply makes no sense. there has to be any idea that is almost as good, no?

immutable legacy code...

That is not even realistic, it will make a split between two worlds where all old code is basically "trusted" without any real analysis benefit.

I am confused. some legacy unsafe code must not be touched (or rewritten) and this is why we just recompile it in hardened mode (turn UB into runtime crashes). Why will it be "trusted"? It is still unsafe, just with less UB.

I think there is still room for some innovation.

Then, we should stop claiming that profiles solve safety until that innovation happens.

std::ranges::sort(v)

I wasn't asking for an alternative sort function :). I meant that there are unsafe functions, but how do you plan to "color" unsafe functions. Without coloring std::sort as unsafe, how would the compiler know to reject its usage?

You do not need safe/unsafe

There will always be unsafe functions. After all, someone has to build the safe abstractions by carefully writing unsafe code. eg: unique_ptr probably uses new for make_unique and delete in the destructor.

This paper's entire argument is that c++'s typesystem doesn't contain enough information about lifetimes/aliasing/coloring. You can fix lifetimes/coloring by adding information with annotations, but that's just hiding the new syntax inside square brackets. Aliasing is just something that profiles has no answer for yet.

solve the reference escaping problem for functions and not go fully viral in all the type system.

Profiles, with their conservative analysis, will reject a lot of valid code or force users to add a bunch of verbose annotations. This will be really frustrating for cpp devs. And who will tell them that they will still not have full safety after all that effort? Adopting rust for new code feels more feasible than this, and as a bonus.

→ More replies (3)