157 karma · joined Sep 26, 2016 (10y ago) · 164 submissions
comment 9y ago · link · parent
Recursive mutexes are analogous to having multiple pointers (or iterators), some of which are "non-const", to an object in the same thread at the same time. Some suggest that this too is a bad idea. For example, the Rust language does not allow a "mutable" (i.e. "non-const") reference to an object to co-exist with any other reference to that object, even in the same thread.
SaferCPlusPlus does not necessarily disagree with this position, but it also does not require adherence to it, like Rust does. So if SaferCPlusPlus is going to allow multiple pointers (or iterators) to a shared object in the same thread, then it's going to need to lock the mutex protecting the object multiple times from the same thread. Giving each pointer/iterator its own separate lock, as opposed to having one lock encompass the all the pointer/iterators, allows the locks to be managed automatically, which ensures against data races, and that resource locks will be released as soon as it is safe to do so.
Again, there's rarely any reason you'd need to interact with the mutex directly. It's primarily there to support "access requester" functionality.
Also, note that this is a recursive shared mutex, not just a recursive mutex, which means that, for example, it provides a kind of "upgrade mutex" functionality. So if a thread has a "read" lock, it can obtain a "write" lock (blocking if necessary), without having to give up its read lock. Then when it's done with the write lock, it can release it without fear of losing its read lock (or blocking).
comment 9y ago · link · parent
Anyway, the point of access requesters are that they are much safer than manually protecting resources with mutexes. That is, using access requesters eliminates the possibility of data races[1]. Which is important because data races can be particularly insidious bugs.
Just like how smart pointers can improve safety by automatically managing object lifetimes, access requesters can improve safety by automatically managing object lifetimes and asynchronous access.
[1] As long as you adhere to the rule that shared objects not have "unprotected" mutable or indirect (i.e. pointers/references) members.
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
> How's this different from taking a pair of iterators?
Safety and (a little) convenience. If your function takes a pair of iterators, there's no way to ensure (at compile-time anyway) that both iterators are even pointing into the same array/vector. Also, spans can ensure, with bounds-checking, that only elements within a specified sub-range can be accessed.
Such bounds-enforcement is particularly important in cases like, for example, SaferCPlusPlus' "TRASectionSplitter", which is a data type that allows you to partition an array/vector/whatever into subsections that can each be safely accessed/modified concurrently from different threads. ("TRASectionSplitter" is not yet documented, but for those interested, example code can be found here[1].)
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus/blob/master...
comment 9y ago · link · parent
From their "Devguide":
Clients of Mutex must obey these rules:
1. Each time a thread acquires a Mutex it must later release it.
2. A thread may not attempt to release a Mutex unless it holds it.
3. A thread may not attempt to acquire an exclusive lock on a Mutex it already holds.
For basic sharing of resources between threads, using "access requesters"[1] can be safer and more convenient as they automatically take care of these rules for you.
And if you need to use the mutex directly, the SaferCPlusPlus library provides a "recursive_shared_timed_mutex"[2] (the one missing from the standard library), which allows a thread to hold multiple ("read" and/or "write") locks at the same time (relieving the "self-deadlock" issue). The mutex isn't documented, but it functions just as its name suggests.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus#asynchronou...
[2] https://github.com/duneroadrunner/SaferCPlusPlus/blob/master...
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
C++ compile times are a problem. Apparently there is a C++ interpreter[2]. Anyone have any experience with it?
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
So when for example, encountering a pointer in the original C source, you have to determine from context whether the author is using it as an iterator to a fixed-sized array, an iterator to a dynamically-sized array, an "owning" reference to a dynamically allocated object, or just a weak/observer reference to an object, and translate to the appropriate "higher-level" element.
So in a sense it's a "decompiler" to a (higher-level) language the code was never compiled from. As a source-to-source transformer, presumably it would qualify as a "transpiler". But does that term have an connotation that the output is just an intermediate translation not intended to be maintained or used directly?
[1] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus#asynchronou...
comment 9y ago · link · parent
"Mut" (i.e. retargetable) references, on the other hand, do require a borrow checker to ensure, at compile-time, that their target will outlive them. Since SaferCPlusPlus does not have a borrow checker (yet), you'd have to use "registered" pointers, which do have run-time overhead. But in practice, retargetable references are generally much less common than non-retargetable ones (particularly in inner loops), so there tends not to be much effect on performance.
SaferCPlusPlus does not yet have an exact analogy to Send/Sync. But as I understand it, those are basically just indicators that say "Trust me, this object is safe to send to / share with other threads." without any verification of the proclaimed safety. At the moment, SaferCPlusPlus does something functionally similar by providing the "TAsyncSharedObjectThatYouAreSureHasNoUnprotectedMutablesReadWriteAccessRequester" which basically allows you to make the same claim about the safe shareability of the object. (And has an unwieldy name to remind you of the seriousness of the claim.) Basically the rule of thumb is "Don't share any object that contains pointers/references/iterators (or is declared "mutable" in C++) or has any (non-static) member functions that return pointers/references/iterators."
Rust and SaferCPlusPlus maybe take different positions on whether sharing objects between threads should be encouraged with abandon, or done prudently and only as necessary.
comment 9y ago · link · parent
The implementation of SaferCPlusPlus as a language/dialect is not yet complete. For example, there is not yet a proper compile-time "enforcement" tool to verify that your code is actually free from unsafe elements. But as a programmer, you basically know what those potentially unsafe elements are, and the SaferCPlusPlus library already makes it practical to write C++ code that avoids the vast majority of them.
That said, I would say that, at the moment, SaferCPlusPlus is not a direct alternative to Rust. If you have the resources to do a rewrite of your project in Rust and memory safety is paramount, then that's probably the way to go. On the other hand, if you need a more expedient way to address code safety or have some other issue with using Rust, SaferCPlusPlus may be a more attractive choice.
https://github.com/duneroadrunner/SaferCPlusPlus#safercplusp...
comment 9y ago · link · parent
[1] Shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
[2] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
comment 9y ago · link · parent
SaferCPlusPlus[1] provides compatible, memory-safe versions of the most commonly used STL containers. (The compiler switches mentioned by others add range checking, but do not, for example, catch use-after-free bugs.)
> > It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases.
Using SaferCPlusPlus, it now is simple to write memory-safe code in C++. And it's not much different from writing traditional (unsafe) C++ code.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
In part, it is simply a practical, relatively low cost solution to address the problem of invalid memory access in existing C/C++ code (and thus, for example, remote code execution vulnerabilities in internet facing code). Clearly we've still not reached the point where critical internet infrastructure vulnerabilities are perceived as costly enough to justify actually solving the problem (as you say in point 5). And collectively, we've not yet concluded that cheaper mitigation techniques aren't a "good enough" solution. At least for now.
But the costs (of critical vulnerabilities) are still going up, and there may come a time when some conclude that they have no choice but to properly address the issue. And some may opt for the least expensive option (i.e. something like SaferCPlusPlus rather than a full rewrite in another memory-safe language).
Of course, more appealing solutions might emerge before that time comes.
But there is also a longer term goal. I see a safe dialect of C++ (like SaferCPlusPlus) perhaps relevant in a future where code is liberated from the "language silo" it's trapped in, via auto-translators. For example, I don't expect C programmers to switch to SaferCPlusPlus en masse (although I'll probably continue to encourage it :). The idea is that legacy C code would be auto-translated to SaferCPlusPlus (with or without the support/consent of the original author). Kind of like how the tor guys used to build their ("safer") version of firefox with the sanitizers enabled[1], and didn't need the permission of the firefox developers to do it.
But it doesn't have to be restricted to just legacy C/C++ code. Modern C++ is so powerful that lots of languages could be translated to (a safe dialect of) it. I think. Presumably C++ would be a more appealing language if it had easy access to, say, all the existing Java code via auto-translation. This of course would apply to any language, but I think its power and performance makes C++ a good target for auto-translation. I'm sure other languages would make good targets too.
Basically, in the future I expect auto-translation quality to be a bigger factor wrt language relevance than marketing or mind-share.
[1] https://blog.torproject.org/blog/tor-browser-55a4-hardened-r...
comment 9y ago · link · parent
If performance is not a concern, just compiling with the appropriate sanitizers[5] is an easy option (though not a complete solution).
But note that all of these solutions take the position that not only is the undefined behavior itself a problem, but also the operation which instigated the undefined behavior. And there is good reason for that. If you just want the undefined behavior to be defined, then the question is, what exactly are you trying to achieve by doing so? Are you hoping to guarantee deterministic behavior while maintaining portability?
[1] https://github.com/robertramey/safe_numerics
[2] https://github.com/duneroadrunner/SaferCPlusPlus#safercplusp...
[3] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
[4] https://github.com/duneroadrunner/SaferCPlusPlus/issues/3
[5] http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
comment 9y ago · link · parent
#include "msemstdvector.h"
// ...
auto v = mse::mstd::vector<int> { 1, 2, 3 };
auto e = v.begin() + 0; // 'e' is a safe iterator here
// ...
v.push_back(v.size());
// ...
cout << *e << "\n"; // no problem
Not much different from your original code.Rust's policy of static enforcement of "exclusive mutable references" still might be preferable in many cases, but there is a practical option of using C++ in a safe(r) way.
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
comment 9y ago · link · parent
It's not ready for use in the wild, but I'll just mention that some good progress is being made on a "C to SaferCPlusPlus[1]" auto-translation assistant. (SaferCPlusPlus is essentially a memory-safe subset of C++.) At this point it can recognize arrays and "pointers used as array iterators" in C code and replace them with memory-safe substitutes. Hopefully an (incomplete but) usable version will be available before long and we can gauge the interest of developers/maintainers.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
I'm glad to see somebody articulate this observation. SaferCPlusPlus[1] is meant to, in part, bring this benefit to existing C++ code bases. The question is, would a borrow checker for C++ make sense?
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
Automatic translation (assistance) from native C to SaferCPlusPlus is being worked on. And if you'll permit me to gripe a little here - you might think it's a reasonable undertaking, until you start contemplating code like this:
int **var1 = (int **)malloc(20);
var1[2] = (int *)malloc(10);
int *var2 = var1[2] + 4;
var2--;
free(var2 - 3);
free(var1);
:)[1] https://github.com/google/sanitizers
[2] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
The standard library provides std::recursive_mutex which supports being locked multiple times by the same thread. Unfortunately, it doesn't provide an std::recursive_shared_timed_mutex for the general case where you might want to support multiple simultaneous read operations. The SaferCPlusPlus library does implement such a mutex, and, like I mentioned in another comment, provides data types that automatically control access to shared objects. I think for most cases, it's the safest and easiest way to share objects asynchronously (in C++). (The data types are kind of analogous to Arc<> and Arc< Mutex<> > for those familiar with Rust.)
Btw, the implementation is self-contained, so you can use the safe sharing data types without having to include the rest of the library. Or you can just copy the source code and make your own safe sharing data types.
comment 9y ago · link · parent
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus#asynchronou...
comment 9y ago · link · parent
Fyi, when safety is important and C++ is the only viable alternative, there's SaferCPlusPlus[1].
And wrt new C++17 features, I'll just point out that the SaferCPlusPlus library provides a version of std::any (and std::variant) geared specifically for pointer/iterators. That is, you can, for example, dereference the contained pointer/iterator without needing to know its actual type. And for those that need it, it also provides the "recursive_shared_timed_mutex" that's notably missing from the standard library.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
[1] https://github.com/duneroadrunner/SaferCPlusPlus#cint-csize_...
comment 9y ago · link · parent
If memory safety is actually important to you, there's now an actual practical solution: SaferCPlusPlus[1].
A tool to automatically retrofit memory safety to existing C/C++ code is in early development (but resources are limited so it might be a while).
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
Just out of curiosity, what kind of performance penalty are people be willing to accept in exchange for memory safety?
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
comment 9y ago · link · parent
It's been perfectly usable for a while now, but it's not yet complete. Static tools for automatically identifying uses of potentially unsafe C/C++ elements in existing code, and (at least mostly) automated translation are still being worked on. And there are still elements that are missing safe substitutes (like std::string). So, if anyone's looking to kill some free time... :)
[1] https://github.com/duneroadrunner/SaferCPlusPlus
[2] https://github.com/duneroadrunner/SaferCPlusPlus-BenchmarksG...