summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Alex Light <allight@google.com> 2020-09-17 14:01:55 -0700
committer Alex Light <allight@google.com> 2020-09-18 21:43:11 +0000
commit46c2a23dbbe48c8ba1dd0238e844f9b5fda47ec7 (patch)
treed905233d37e6af9f4476b9d25b63baa7f8c4f5b3
parentb76cb89736d889929939223a22bc7fb44c6117b2 (diff)
Don't assume allocators clear memory
BitVector, when constructed with an explicit size, would assume that the allocator would zero out memory. This is not always true and could lead to unexpected outcomes. Test: ./test.py --host Change-Id: Ibe556ebf07b5081f110e76efa927b7fa677a607e
-rw-r--r--libartbase/base/bit_vector.cc3
-rw-r--r--libartbase/base/bit_vector_test.cc26
2 files changed, 28 insertions, 1 deletions
diff --git a/libartbase/base/bit_vector.cc b/libartbase/base/bit_vector.cc
index 2ef14d7074..b32b4117dd 100644
--- a/libartbase/base/bit_vector.cc
+++ b/libartbase/base/bit_vector.cc
@@ -45,9 +45,10 @@ BitVector::BitVector(uint32_t start_bits,
allocator,
BitsToWords(start_bits),
static_cast<uint32_t*>(allocator->Alloc(BitsToWords(start_bits) * kWordBytes))) {
+ // We don't know if the allocator cleared things.
+ ClearAllBits();
}
-
BitVector::BitVector(const BitVector& src,
bool expandable,
Allocator* allocator)
diff --git a/libartbase/base/bit_vector_test.cc b/libartbase/base/bit_vector_test.cc
index e5ba7b4f08..bdba22a203 100644
--- a/libartbase/base/bit_vector_test.cc
+++ b/libartbase/base/bit_vector_test.cc
@@ -66,6 +66,32 @@ TEST(BitVector, Test) {
EXPECT_TRUE(iterator == bv.Indexes().end());
}
+struct MessyAllocator : public Allocator {
+ public:
+ MessyAllocator() : malloc_(Allocator::GetMallocAllocator()) {}
+ ~MessyAllocator() {}
+
+ void* Alloc(size_t s) override {
+ void* res = malloc_->Alloc(s);
+ memset(res, 0xfe, s);
+ return res;
+ }
+
+ void Free(void* v) override {
+ malloc_->Free(v);
+ }
+
+ private:
+ Allocator* malloc_;
+};
+
+TEST(BitVector, MessyAllocator) {
+ MessyAllocator alloc;
+ BitVector bv(32, false, &alloc);
+ EXPECT_EQ(bv.NumSetBits(), 0u);
+ EXPECT_EQ(bv.GetHighestBitSet(), -1);
+}
+
TEST(BitVector, NoopAllocator) {
const uint32_t kWords = 2;