Merge "Fix heap bitmap rounding down size error" into ics-mr1-plus-art
diff --git a/src/heap_bitmap.cc b/src/heap_bitmap.cc
index 769ee9d..fc722f5 100644
--- a/src/heap_bitmap.cc
+++ b/src/heap_bitmap.cc
@@ -24,7 +24,8 @@
 
 HeapBitmap* HeapBitmap::Create(const char* name, byte* heap_begin, size_t heap_capacity) {
   CHECK(heap_begin != NULL);
-  size_t bitmap_size = HB_OFFSET_TO_INDEX(heap_capacity) * kWordSize;
+  // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
+  size_t bitmap_size = HB_OFFSET_TO_INDEX(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
   UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name, NULL, bitmap_size, PROT_READ | PROT_WRITE));
   if (mem_map.get() == NULL) {
     LOG(ERROR) << "Failed to allocate bitmap " << name;
diff --git a/src/heap_test.cc b/src/heap_test.cc
index 0077a77..d5c07da 100644
--- a/src/heap_test.cc
+++ b/src/heap_test.cc
@@ -45,4 +45,11 @@
   Runtime::Current()->GetHeap()->CollectGarbage(false);
 }
 
+TEST_F(HeapTest, HeapBitmapCapacityTest) {
+  byte* heap_begin = reinterpret_cast<byte*>(0x1000);
+  const size_t heap_capacity = HeapBitmap::kAlignment * (sizeof(intptr_t) * 8 + 1);
+  UniquePtr<HeapBitmap> bitmap(HeapBitmap::Create("test-bitmap", heap_begin, heap_capacity));
+  bitmap->Set(reinterpret_cast<const Object*>(&heap_begin[heap_capacity - HeapBitmap::kAlignment]));
+}
+
 }  // namespace art