Don't use fixed addresses in mem map test
We obtain a valid address instead of assuming there won't be a map
there.
These checks used to be OK when we didn't start the runtime.
Change-Id: Ie636ed3773d2c0c62d87e57623ee4ce380c64db0
diff --git a/runtime/mem_map_test.cc b/runtime/mem_map_test.cc
index 3790e53..edcbcf2 100644
--- a/runtime/mem_map_test.cc
+++ b/runtime/mem_map_test.cc
@@ -29,10 +29,25 @@
static uint8_t* BaseBegin(MemMap* mem_map) {
return reinterpret_cast<uint8_t*>(mem_map->base_begin_);
}
+
static size_t BaseSize(MemMap* mem_map) {
return mem_map->base_size_;
}
+ static uint8_t* GetValidMapAddress(size_t size, bool low_4gb) {
+ // Find a valid map address and unmap it before returning.
+ std::string error_msg;
+ std::unique_ptr<MemMap> map(MemMap::MapAnonymous("temp",
+ nullptr,
+ size,
+ PROT_READ,
+ low_4gb,
+ false,
+ &error_msg));
+ CHECK(map != nullptr);
+ return map->Begin();
+ }
+
static void RemapAtEndTest(bool low_4gb) {
std::string error_msg;
// Cast the page size to size_t.
@@ -189,9 +204,11 @@
TEST_F(MemMapTest, MapAnonymousExactAddr) {
CommonInit();
std::string error_msg;
+ // Find a valid address.
+ uint8_t* valid_address = GetValidMapAddress(kPageSize, /*low_4gb*/false);
// Map at an address that should work, which should succeed.
std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0",
- reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
+ valid_address,
kPageSize,
PROT_READ | PROT_WRITE,
false,
@@ -199,7 +216,7 @@
&error_msg));
ASSERT_TRUE(map0.get() != nullptr) << error_msg;
ASSERT_TRUE(error_msg.empty());
- ASSERT_TRUE(map0->BaseBegin() == reinterpret_cast<void*>(ART_BASE_ADDRESS));
+ ASSERT_TRUE(map0->BaseBegin() == valid_address);
// Map at an unspecified address, which should succeed.
std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1",
nullptr,
@@ -237,18 +254,27 @@
CommonInit();
// This test may not work under valgrind.
if (RUNNING_ON_MEMORY_TOOL == 0) {
- uintptr_t start_addr = ART_BASE_ADDRESS + 0x1000000;
+ constexpr size_t size = 0x100000;
+ // Try all addresses starting from 2GB to 4GB.
+ size_t start_addr = 2 * GB;
std::string error_msg;
- std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr",
- reinterpret_cast<uint8_t*>(start_addr),
- 0x21000000,
- PROT_READ | PROT_WRITE,
- true,
- false,
- &error_msg));
+ std::unique_ptr<MemMap> map;
+ for (; start_addr <= std::numeric_limits<uint32_t>::max() - size; start_addr += size) {
+ map.reset(MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr",
+ reinterpret_cast<uint8_t*>(start_addr),
+ size,
+ PROT_READ | PROT_WRITE,
+ /*low_4gb*/true,
+ false,
+ &error_msg));
+ if (map != nullptr) {
+ break;
+ }
+ }
+ ASSERT_GE(reinterpret_cast<uintptr_t>(map->End()), 2u * GB);
ASSERT_TRUE(map.get() != nullptr) << error_msg;
ASSERT_TRUE(error_msg.empty());
- ASSERT_EQ(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), start_addr);
+ ASSERT_EQ(BaseBegin(map.get()), reinterpret_cast<void*>(start_addr));
}
}