8217994: os::print_hex_dump should be more resilient against unreadable memory
authorshade
Wed, 30 Jan 2019 19:45:10 +0100
changeset 53573 556122252316
parent 53572 f5480f924571
child 53574 f38270ce02cc
8217994: os::print_hex_dump should be more resilient against unreadable memory Reviewed-by: zgu, stuefe, lucy
src/hotspot/share/runtime/os.cpp
test/hotspot/gtest/runtime/test_os.cpp
--- a/src/hotspot/share/runtime/os.cpp	Wed Jan 30 18:21:06 2019 +0000
+++ b/src/hotspot/share/runtime/os.cpp	Wed Jan 30 19:45:10 2019 +0100
@@ -885,11 +885,15 @@
   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++;
--- a/test/hotspot/gtest/runtime/test_os.cpp	Wed Jan 30 18:21:06 2019 +0000
+++ b/test/hotspot/gtest/runtime/test_os.cpp	Wed Jan 30 19:45:10 2019 +0100
@@ -153,6 +153,30 @@
 }
 #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.