< prev index next >

test/native/runtime/test_os.cpp

Print this page
rev 13549 : 8185712: [windows] Improve native symbol decoder
Reviewed-by: goetz, zgu


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "runtime/os.hpp"
  26 #include "unittest.hpp"

  27 
  28 static size_t small_page_size() {
  29   return os::vm_page_size();
  30 }
  31 
  32 static size_t large_page_size() {
  33   const size_t large_page_size_example = 4 * M;
  34   return os::page_size_for_region_aligned(large_page_size_example, 1);
  35 }
  36 
  37 TEST_VM(os, page_size_for_region) {
  38   size_t large_page_example = 4 * M;
  39   size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);
  40 
  41   size_t small_page = os::vm_page_size();
  42   if (large_page > small_page) {
  43     size_t num_small_in_large = large_page / small_page;
  44     size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);
  45     ASSERT_EQ(page, small_page) << "Did not get a small page";
  46   }


 126     mean += u;
 127     variance += (u*u);
 128   }
 129   mean /= reps;
 130   variance /= (reps - 1);
 131 
 132   ASSERT_EQ(num, 1043618065) << "bad seed";
 133   // tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
 134   int intmean = mean*100;
 135   ASSERT_EQ(intmean, 50);
 136   // tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
 137   int intvariance = variance*100;
 138   ASSERT_EQ(intvariance, 33);
 139   const double eps = 0.0001;
 140   t = fabsd(mean - 0.5018);
 141   ASSERT_LT(t, eps) << "bad mean";
 142   t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
 143   ASSERT_LT(t, eps) << "bad variance";
 144 }
 145 




















































































































 146 
 147 #ifdef ASSERT
 148 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
 149   size_t region_size = 16 * os::vm_page_size();
 150   os::page_size_for_region_aligned(region_size, 0); // should assert
 151 }
 152 #endif


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "runtime/os.hpp"
  26 #include "unittest.hpp"
  27 #include "utilities/decoder.hpp"
  28 
  29 static size_t small_page_size() {
  30   return os::vm_page_size();
  31 }
  32 
  33 static size_t large_page_size() {
  34   const size_t large_page_size_example = 4 * M;
  35   return os::page_size_for_region_aligned(large_page_size_example, 1);
  36 }
  37 
  38 TEST_VM(os, page_size_for_region) {
  39   size_t large_page_example = 4 * M;
  40   size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);
  41 
  42   size_t small_page = os::vm_page_size();
  43   if (large_page > small_page) {
  44     size_t num_small_in_large = large_page / small_page;
  45     size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);
  46     ASSERT_EQ(page, small_page) << "Did not get a small page";
  47   }


 127     mean += u;
 128     variance += (u*u);
 129   }
 130   mean /= reps;
 131   variance /= (reps - 1);
 132 
 133   ASSERT_EQ(num, 1043618065) << "bad seed";
 134   // tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
 135   int intmean = mean*100;
 136   ASSERT_EQ(intmean, 50);
 137   // tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
 138   int intvariance = variance*100;
 139   ASSERT_EQ(intvariance, 33);
 140   const double eps = 0.0001;
 141   t = fabsd(mean - 0.5018);
 142   ASSERT_LT(t, eps) << "bad mean";
 143   t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
 144   ASSERT_LT(t, eps) << "bad variance";
 145 }
 146 
 147 #ifdef _WIN32
 148 TEST(os, dll_addr_to_function_valid) {
 149   char buf[128] = "";
 150   int offset = -1;
 151   address valid_function_pointer = (address) JNI_CreateJavaVM;
 152   ASSERT_TRUE(os::dll_address_to_function_name(valid_function_pointer,
 153                                           buf, sizeof(buf), &offset, true) == true);
 154   ASSERT_TRUE(strstr(buf, "JNI_CreateJavaVM") != NULL);
 155   ASSERT_TRUE(offset >= 0);
 156 }
 157 
 158 // Test that handing down a too-small output buffer will truncate the output
 159 // string correctly but cause no harm otherwise.
 160 TEST(os, dll_addr_to_function_valid_truncated) {
 161   char buf[128] = "";
 162   int offset = -1;
 163   memset(buf, 'X', sizeof(buf));
 164   address valid_function_pointer = (address) JNI_CreateJavaVM;
 165   ASSERT_TRUE(os::dll_address_to_function_name(valid_function_pointer,
 166                                           buf, 10, &offset, true) == true);
 167   ASSERT_TRUE(buf[10 - 1] == '\0');
 168   ASSERT_TRUE(buf[10] == 'X');
 169   // Note: compare the first (sizeof buf - 2) bytes only because Windows decoder
 170   // (actually UndecorateSymbolName()) seems to have a bug where it uses one byte less than
 171   // the buffer would offer.
 172   ASSERT_TRUE(strncmp(buf, "JNI_Crea", 8) == 0);
 173   ASSERT_TRUE(offset >= 0);
 174 }
 175 
 176 // Test that handing down invalid addresses will cause no harm and output buffer
 177 // and offset will contain "" and -1, respectively.
 178 TEST(os, dll_addr_to_function_invalid) {
 179   char buf[128];
 180   int offset;
 181   address invalid_function_pointers[] = { NULL, (address)1, (address)&offset };
 182 
 183   for (int i = 0;
 184        i < sizeof(invalid_function_pointers) / sizeof(address);
 185        i ++)
 186   {
 187     address addr = invalid_function_pointers[i];
 188     strcpy(buf, "blabla");
 189     offset = 12;
 190     ASSERT_TRUE(os::dll_address_to_function_name(addr, buf, sizeof(buf),
 191                                             &offset, true) == false);
 192     ASSERT_TRUE(buf[0] == '\0');
 193     ASSERT_TRUE(offset == -1);
 194   }
 195 }
 196 
 197 TEST(os, decoder_get_source_info_valid) {
 198   char buf[128] = "";
 199   int line = -1;
 200   address valid_function_pointer = (address) JNI_CreateJavaVM;
 201   ASSERT_TRUE(Decoder::get_source_info(valid_function_pointer, buf, sizeof(buf), &line) == true);
 202   ASSERT_TRUE(strcmp(buf, "jni.cpp") == 0);
 203   ASSERT_TRUE(line >= 0);
 204 }
 205 
 206 // Test that handing down a too-small output buffer will truncate the output
 207 // string correctly but cause no harm otherwise.
 208 TEST(os, decoder_get_source_info_valid_truncated) {
 209   char buf[128] = "";
 210   int line = -1;
 211   memset(buf, 'X', sizeof(buf));
 212   address valid_function_pointer = (address) JNI_CreateJavaVM;
 213   ASSERT_TRUE(Decoder::get_source_info(valid_function_pointer, buf, 7, &line) == true);
 214   ASSERT_TRUE(buf[7 - 1] == '\0');
 215   ASSERT_TRUE(buf[7] == 'X');
 216   ASSERT_TRUE(strcmp(buf, "jni.cp") == 0);
 217   ASSERT_TRUE(line > 0);
 218 }
 219 
 220 // Test that handing down invalid addresses will cause no harm and output buffer
 221 // and line will contain "" and -1, respectively.
 222 TEST(os, decoder_get_source_info_invalid) {
 223   char buf[128] = "";
 224   int line = -1;
 225   address invalid_function_pointers[] = { NULL, (address)1, (address)&line };
 226 
 227   for (int i = 0;
 228        i < sizeof(invalid_function_pointers) / sizeof(address);
 229        i ++)
 230   {
 231     address addr = invalid_function_pointers[i];
 232     // We should fail but not crash
 233     strcpy(buf, "blabla");
 234     line = 12;
 235     ASSERT_TRUE(Decoder::get_source_info(addr, buf, sizeof(buf), &line) == false);
 236     // buffer should contain "", offset should contain -1
 237     ASSERT_TRUE(buf[0] == '\0');
 238     ASSERT_TRUE(line == -1);
 239   }
 240 }
 241 
 242 #ifdef PLATFORM_PRINT_NATIVE_STACK
 243 TEST(os, platform_print_native_stack) {
 244   bufferedStream bs;
 245   // Note: scratch buffer argument to os::platform_print_native_stack is not
 246   // optional!
 247   char scratch_buffer [255];
 248   for (int i = 0; i < 3; i ++) {
 249     ASSERT_TRUE(os::platform_print_native_stack(&bs, NULL, scratch_buffer,
 250                                                 sizeof(scratch_buffer)));
 251     ASSERT_TRUE(bs.size() > 0);
 252     // This may depend on debug information files being generated and available
 253     // (e.g. not zipped).
 254     ASSERT_TRUE(::strstr(bs.base(), "platform_print_native_stack") != NULL);
 255 #ifdef _WIN32
 256     // We have source information on Windows.
 257     ASSERT_TRUE(::strstr(bs.base(), "os_windows_x86.cpp") != NULL);
 258 #endif
 259   }
 260 }
 261 #endif
 262 #endif
 263 
 264 #ifdef ASSERT
 265 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
 266   size_t region_size = 16 * os::vm_page_size();
 267   os::page_size_for_region_aligned(region_size, 0); // should assert
 268 }
 269 #endif
< prev index next >