6 ms·
The C++ Lifetime Profile: How It Plans to Make C++ Code Safer
- hwc 7y agoA common issue with C++ software is the need for a stable, standard ABI, which C provides but C++ does not (except through `extern "C"`). Because C++ name mangling is not standardized and the standard library headers have tons of implementation-specific details, something like `void foo(std::unique_ptr<Bar>);` does not have a stable ABI.
- jcelerier 7y agohow is that related in any way to the article ?
- Someone 7y agoAlso, I don’t think there is anything in the C, respectively C++ standards that forbids, respectively requires name mangling. Linking isn’t specified in either standard. C++ implementations have name mangling because they live on systems designed for C, which don’t allow symbols such as int max(int,int) C implementations (would) need name mangling if they ran on a system that used case-insensitive comparisons on symbol names, or on ones whose character set is smaller than that allowed in C symbols (I know examples of neither, but Wikipedia has an example that encodes the calling convention in the symbol name: https://en.wikipedia.org/wiki/Name_mangling#C https://en.wikipedia.org/wiki/Name_mangling#C)
- ape4 7y agoThe table in that article shows how different compilers mange very similarly but not the same! This is something that could be standardized for the benefit of all.
- Someone 7y agoNot quite. You would know what to call, but not how to call it. That would require full ABI compatibility, and C++’s is complex. https://isocpp.org/wiki/faq/compiler-dependencies#binary-compat https://isocpp.org/wiki/faq/compiler-dependencies#binary-com...: ”If name mangling was standardized, could I link code compiled with compilers from different compiler vendors? Short answer: Probably not.” (click link for more info)
- ape4 7y agoThanks for the info. I'm just a C/C++ user. There has to be way to workout a convention for all those options.
- jcelerier 7y ago> Probably not. yet the real life answer is that I can link C++ code built with three different compiler on linux (gcc / clang / icc) and three / two on windows (clang / cl.exe / icc or clang / gcc)
- BubRoss 7y agoWhat does that have to do with lifetime annotations?
- adev_ 7y ago> common issue with C++ software is the need for a stable, standard ABI. That's not entirely true. C++ has a stable ABI: compiling a library with GCC and reusing it with clang or ICC is doable and commonly done nowadays. The problem with the C++ ABI is that it is shamefully fragile. It is extremely easy to break an ABI without wanting it in C++, even for experts. https://community.kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B https://community.kde.org/Policies/Binary_Compatibility_Issu...
- zabzonk 7y ago> standard ABI, which C provides but C++ does not Neither the C Language Standard nor the C++ Language Standard specify an ABI.
- DoofusOfDeath 7y ago> Neither the C Language Standard nor the C++ Language Standard specify an ABI. Perhaps we should distinguish between a Langage-Standard ABI vs. a per-platform de facto ABI. I think the GP's comment holds for the latter definition.
- zabzonk 7y agoI don't believe that either the GCC or MSVC C ABIs have been the same (per platform) over the years.
- temac 7y agoFor C: both platform have been EXTREMELY compatible over the years. They probably both treat breakage as bug. This is a must given the fundamental APIs are defined in term of C headers, and must remain compatible. Even for C++: gcc has been mostly compatible, but not unreasonably so. MSVC was traditionally not at all, but has recently been for 2 or 3 major versions in a row. I think MSVC will eventually break it again though, but at least they break it way less often than before (and breaking can also have advantages).
- jcranmer 7y agoIn practice, there is a stable and standard ABI in C++. On Windows systems, you use the (undocumented, but mostly reverse-engineered) MSVC ABI. On everybody else, you use the confusingly-named Itanium ABI, except for exception handling on ARM, which uses a slightly different ABI. Standard library implementations do not necessarily share the same ABI, but it is possible to interoperate between libc++ and libstdc++, and libstdc++ maintains ABI compatibility across different versions. And, to be pedantic, C does not provide a stable, standard ABI. It is the platforms themselves that provide this ABI standard, and these platforms equally provide a C++ ABI standard. So C++ ABI compatibility is no more or less a problem than C ABI compatibility.
- pjmlp 7y agoJust a small correction macOS derived platforms do it a bit differently as well.
- paulddraper 7y agoThe legitimate difference between C and C++ ABIs is that the C ABI is easier to understand and reason about. It's analogous to Java and Scala; each has an ABI, but it's easier to tell when the former breaks ABI than the latter.
- likeliv 7y agoThat's right. C being more limited, it has a more simplistic ABI. But the parent comment was replying the "need for a stable, standard ABI, which C provide". Which is misleading as both C++ and C have the same level of stability and standardisation of their ABI. (I.e: none of them is standardized, but both of them are stable on practice)
- paulddraper 7y agoYep, you're right. There is a difference, but the difference is in complexity, not availability.
- youdontknowtho 7y agoI'm really interested in the Core guidelines. They are only a few years old, however. It will be super interesting to see some case studies of large codebase adoption in the next few years. The lifetime profile is the one that I 'm most interested in seeing some real studies on. Some of the changes seem pretty simple (of course, I don't have to make them in any codebases) so it makes me wonder if they are expressive enough to solve a large chunk of the use after free problems.
- xiphias2 7y agoI would prefer if C++ would be extended with a function level borrow checker with real safety guarantees that could optionally be turned on for functions. In the first look Lifetime profile looks like it just tries to find the low hanging fruit cases. Can it be further extended to guarantee safety guarantees in the future, or it's just a hack? I'm not sure about it.
- ryanianian 7y agoRAII solves lots of lifetime problems, but it's surprisingly easy to have refs outlive their targets (among other things). Static-analysis can spot a portion of these, but in general the lifetime of input and output values of functions/scopes needs to be explicitly stated by the user (part of the type) and can thus be verified by the compiler. I think Rust captures the core lifetime concepts pretty well, but the notation can be cumbersome, and I think C++'s rich type-system could make for a less-tedious solution. TFA mentions doing some implicit lifetime-analysis based on signatures (e.g. inferring ownership based on type-traits), but ultimately it is 'just' low-hanging fruit. It would be nice to capture in signatures/types that "the input must outlive the output" and have more meaningful error diagnostics and less implicit magic ("did I follow the iterator traits correctly?").
- xiphias2 7y agoI think the main problem for C++ programmers is not the notation of Rust, as Rust is a simpler language than C++. It's backwards compatibility: there are hundreds of millions, or probably billions of lines of C++ code out there. I found working with big code bases really hard, because the memory ownership situation is crazy, in fact impossible to keep track of. That's why my question was whether there's a future path for C++ Lifetime Profile to get programs to Rust like memory safety in the future, but I still don't know the answer.
- pjmlp 7y agoHowever that is also the reason why those billions of lines will stay C++, and no one will pay for porting them into Rust, or any other memory safe language. So any improvement to the status quo is pretty much welcomed, and here lies the big difference between the C++ (introducing lifetime profile) and C communities (making Annex K optional), regarding taking actions to fix memory corruption.
- thrower123 7y agoAt this point, why not write Rust instead? I know, C++ is a dirty snowball rolling downhill, gobbling up every feature it can put its tendrils on, and always has been, but it is getting silly.
- adev_ 7y ago> I know, C++ is a dirty snowball rolling downhill, gobbling up every feature it can put it's tendrils on, and always has been, but it is getting silly. That's named evolving and improving. And that's how a language stay alive.
- Dylan16807 7y agoIf you never remove features, but keep adding more steadily, 'evolving and improving' is the wrong way to describe it.
- yarrel 7y agoC++ treats backwards compatibility as important. New code doesn't have to use old features.
- Dylan16807 7y ago> New code doesn't have to use old features. Yeah but it's hard to manage in multiple ways. Is there good tooling to help? And it means a lot of references you could normally check are using features you can't use. Plus none of your libraries are going to agree on what parts to use.
- Analemma_ 7y agoRemoving features from a programming language is almost impossible; it breaks backwards compatibility and so is a hard barrier to upgrading from the old version. Note the 12-year-and-counting struggle to adopt Python 3, or the fact that Perl 6 developers gave up completely trying to get people to upgrade from Perl 5 and just rebranded it as a new language instead. C++ has opted to avoid those struggles by never removing features; you can disagree with that decision but it was done for good reasons.
- sproketboy 7y agoRUST
- jupp0r 7y agoHere’s my problem with the approach: it’s a compiler warning. Nobody looks at those so it’s configured to be treated as an error. Now you have to deal with false positives and differences between implementations across compilers. I’d rather have this be part of the language with string guarantees and an obligation for compilers to adhere to the spec. Of course that isn’t possible in C++ without breaking existing code, which is why this ended up where it is.
- vkaku 7y agoTL;DR - Ownership to C++
- axilmar 7y agoNot a long term approach, in my humble opinion (as a C++ veteran that has worked with it for 20 years). What the language needs is a new set of types that will ensure memory safety. Using the old types with the new ones should not allow the code to be compiled, thus ensuring memory safety. It would be nice to have such a tool in short-term, though. At least some old bugs could possibly be more easily revealed.