Comments (6)
I was not able to reproduce this issue, although I don't doubt there could be an issue. #388 is a similar issue. In general our code is not battle-tested with exceptions enabled.
from abseil-cpp.
In general our code is not battle-tested with exceptions enabled.
We don't use a ton of exceptions either, so originally btree_allocator
was returning nullptr
instead of throwing, but none of the btree code checks for nullptr
either. Do the abseil containers generally not handle memory allocation failures?
In any case, I discovered the root cause. In btree::internal_emplace
, ASAN enables a second replace_leaf_root_node()
call. If we're inserting the first element into the btree and that allocation fails, we get into the broken state where size_ == 0
but root_ != EmptyNode()
. When this happens, btree::clear()
won't bother to free the dangling root_
and it leaks.
So there's a couple different solutions I see:
btree::clear()
should checkif (!empty() || root() != EmptyNode())
instead. This might help prevent some other similar issues, but it does add an additional branch. Gating the second check behind ASAN could work if the extra check is unbearable.btree::internal_emplace()
should catchstd::bad_alloc
and clean up the oldroot_
before re-throwing. This would have the benefit of being a much more targeted fix, but that means it might not catch any other similar issues.
We're probably going to go with the clear()
fix in the hope that it potentially saves us from other issues.
I was not able to reproduce this issue, although I don't doubt there could be an issue.
That's a bummer, although I don't see how it could be related to our specific build. I think the Godbolt link I posted will repro it, but I can't get the leak sanitizer to turn on...
from abseil-cpp.
Do the abseil containers generally not handle memory allocation failures?
Like I said, this isn't well tested. Our production memory allocator is TCMalloc, which actually just aborts the program on failure.
That's a bummer, although I don't see how it could be related to our specific build. I think the Godbolt link I posted will repro it, but I can't get the leak sanitizer to turn on...
I played with this a bit because from the information you provided, something was obviously wrong. I finally got it to reproduce. A fix should be coming.
from abseil-cpp.
Following up here. I sent a proposed fix and unit test to the maintainers of our btree and hash containers. The feedback was that they don't want to guarantee that these containers are exception safe because this is not thoroughly tested and because maintaining exception safety may complicate code that is performance critical. We decided to explicitly document that this is not supported in e181410.
I know this isn't the outcome you hoped for and I'm a bit disappointed as well, but I can sympathize with the logic here. Thanks for your interest in Abseil.
from abseil-cpp.
Bummer to hear, but i'm not entirely surprised. Luckily everything seems to work fine in non-ASAN builds, allocation failures there don't appear to have any side effects as far as I can tell. Could you post the proposed fix here? We might steal it for ourselves.
I did encounter another instance of this bug in the rebalance_or_split
path during insertion. I wasn't able to dig as deep or create a minimal repro, but I expect the root cause is mostly the same.
from abseil-cpp.
It is largely based on what you posted. The change to clear()
that you mentioned: if (!empty() || root() != EmptyNode())
.
Here is the test case:
#ifdef ABSL_HAVE_EXCEPTIONS
// An allocator that repeately throws `std::bad_alloc()` after the counter
// variable pointed to by `good_allocs_remaining` reaches 0. The state lives
// outside the allocator so that copies don't interfere with the state.
template <typename T>
class ThrowAfterAllocator {
public:
using Allocator = std::allocator<T>;
using AllocatorTraits = std::allocator_traits<Allocator>;
using value_type = typename AllocatorTraits::value_type;
using pointer = typename AllocatorTraits::pointer;
using const_pointer = typename AllocatorTraits::const_pointer;
using size_type = typename AllocatorTraits::size_type;
using difference_type = typename AllocatorTraits::difference_type;
explicit ThrowAfterAllocator(int64_t *good_allocs_remaining) noexcept
: good_allocs_remaining_(good_allocs_remaining) {}
template <typename U>
ThrowAfterAllocator(const ThrowAfterAllocator<U> &x) noexcept
: good_allocs_remaining_(x.good_allocs_remaining_) {}
pointer allocate(
size_type n,
typename AllocatorTraits::const_void_pointer hint = nullptr) {
if ((*good_allocs_remaining_)-- <= 0) {
throw std::bad_alloc();
}
Allocator allocator;
return AllocatorTraits::allocate(allocator, n, hint);
}
void deallocate(pointer p, size_type n) {
Allocator allocator;
AllocatorTraits::deallocate(allocator, p, n);
}
int64_t* good_allocs_remaining_;
};
TEST(Btree, ClearAfterAllocatorThrow) {
using Alloc = ThrowAfterAllocator<int>;
using Set = absl::btree_set<int, std::less<int>, Alloc>;
int64_t good_allocs_remaining = 1;
Alloc alloc(&good_allocs_remaining);
Set set(alloc);
try {
set.emplace_hint(set.cend(), 42);
} catch (const std::exception &) {
}
set.clear();
}
#endif // ABSL_HAVE_EXCEPTIONS
from abseil-cpp.
Related Issues (20)
- [Bug]: Can't link using riscv64 toolchain
- use vcpkg install absl dll lib link error HOT 1
- [Bug]: Sanitizer options may disagree between library and user leading to ODR violations
- Can't link to MSVC built abseil with clang/LLVM
- improvement : keep cmakelists clean, remove unneeded policy activations that are already active HOT 2
- [Bug]: container test_allocator should be skipped if testing is disabled HOT 4
- [Bug]: Build error during cmake quick start HOT 1
- [Bug]: 20240722.rc1 can not be built for android ndk r25b / r25c HOT 3
- [Bug]: Can't build C++20 project without .bazelrc HOT 2
- [Bug]: absl::Log Initialization in DLL Fails When Called Using ctypes on Windows
- [Bug]: Flag "-Wnon-virtual-dtor" is added to pkg-config files
- [Bug]: error while loading shared libraries: libabsl_cord_internal.so.2401.0.0: cannot open shared object file: No such file or directory
- [Bug]: asan failure in absl::Status::ToString() HOT 3
- [Bug]: Compilation regression in version update: "...incomplete type 'absl::container_internal::HashEq..." HOT 2
- Have Bazel rules for creating shared libraries HOT 2
- [Bug]: Undefined symbol referenced from absl::lts_20240722::log_internal::LogMessage::operator<<(unsigned long) HOT 4
- protobuf-3.25.3 with abseil-20240116.2 crash
- [Bug]: Abseil does not honor C++ standard from Bazel target HOT 1
- abseil doesn't build with clang on windows due to missing AVX types HOT 8
- [Bug]: combase.dll is loaded and unloaded in every call to `LocalTimeZone()` on Win10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from abseil-cpp.