8217994: os::print_hex_dump should be more resilient against unreadable memory

Reviewed-by: zgu, stuefe, lucy
This commit is contained in:
Aleksey Shipilev 2019-01-30 19:45:10 +01:00
parent 6e04e7659a
commit 9f533b678e
2 changed files with 33 additions and 5 deletions
src/hotspot/share/runtime
test/hotspot/gtest/runtime

@ -885,11 +885,15 @@ void os::print_hex_dump(outputStream* st, address start, address end, int unitsi
address p = start;
st->print(PTR_FORMAT ": ", p2i(start));
while (p < end) {
switch (unitsize) {
case 1: st->print("%02x", *(u1*)p); break;
case 2: st->print("%04x", *(u2*)p); break;
case 4: st->print("%08x", *(u4*)p); break;
case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break;
if (is_readable_pointer(p)) {
switch (unitsize) {
case 1: st->print("%02x", *(u1*)p); break;
case 2: st->print("%04x", *(u2*)p); break;
case 4: st->print("%08x", *(u4*)p); break;
case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break;
}
} else {
st->print("%*.*s", 2*unitsize, 2*unitsize, "????????????????");
}
p += unitsize;
cols++;

@ -153,6 +153,30 @@ TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
}
#endif
TEST(os, test_print_hex_dump) {
ResourceMark rm;
stringStream ss;
outputStream* out = &ss;
// outputStream* out = tty; // enable for printout
// Test dumping unreadable memory does not fail
os::print_hex_dump(out, (address)0, (address)100, 1);
os::print_hex_dump(out, (address)0, (address)100, 2);
os::print_hex_dump(out, (address)0, (address)100, 4);
os::print_hex_dump(out, (address)0, (address)100, 8);
// Test dumping readable memory does not fail
char arr[100];
for (int c = 0; c < 100; c++) {
arr[c] = c;
}
address addr = (address)&arr;
os::print_hex_dump(out, addr, addr + 100, 1);
os::print_hex_dump(out, addr, addr + 100, 2);
os::print_hex_dump(out, addr, addr + 100, 4);
os::print_hex_dump(out, addr, addr + 100, 8);
}
//////////////////////////////////////////////////////////////////////////////
// Test os::vsnprintf and friends.