Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit c37eabe

Browse files
committedDec 15, 2020
8252148: vmError::controlled_crash should be #ifdef ASSERT and move tests to gtest
Reviewed-by: iklam, stuefe
1 parent 2273f95 commit c37eabe

18 files changed

+190
-216
lines changed
 

‎src/hotspot/share/gc/shared/gc_globals.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@
290290
develop(uintx, MetadataAllocationFailALotInterval, 1000, \
291291
"Metadata allocation failure a lot interval") \
292292
\
293-
product(bool, ExecutingUnitTests, false, \
294-
"Whether the JVM is running unit tests or not") \
295-
\
296293
product(bool, UseTLAB, true, \
297294
"Use thread-local object allocation") \
298295
\

‎src/hotspot/share/prims/jni.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -3800,10 +3800,14 @@ static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
38003800

38013801
#ifndef PRODUCT
38023802
if (ReplayCompiles) ciReplay::replay(thread);
3803+
#endif
38033804

3805+
#ifdef ASSERT
38043806
// Some platforms (like Win*) need a wrapper around these test
38053807
// functions in order to properly handle error conditions.
3806-
VMError::test_error_handler();
3808+
if (ErrorHandlerTest != 0) {
3809+
VMError::controlled_crash(ErrorHandlerTest);
3810+
}
38073811
#endif
38083812

38093813
// Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.

‎src/hotspot/share/runtime/globals.hpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -493,19 +493,24 @@ const intx ObjectAlignmentInBytes = 8;
493493
develop(bool, ZapFillerObjects, trueInDebug, \
494494
"Zap filler objects with 0xDEAFBABE") \
495495
\
496-
notproduct(uintx, ErrorHandlerTest, 0, \
496+
product(bool, ExecutingUnitTests, false, \
497+
"Whether the JVM is running unit tests or not") \
498+
\
499+
develop(uintx, ErrorHandlerTest, 0, \
497500
"If > 0, provokes an error after VM initialization; the value " \
498-
"determines which error to provoke. See test_error_handler() " \
501+
"determines which error to provoke. See controlled_crash() " \
499502
"in vmError.cpp.") \
503+
range(0, 17) \
500504
\
501-
notproduct(uintx, TestCrashInErrorHandler, 0, \
505+
develop(uintx, TestCrashInErrorHandler, 0, \
502506
"If > 0, provokes an error inside VM error handler (a secondary " \
503-
"crash). see test_error_handler() in vmError.cpp") \
507+
"crash). see controlled_crash() in vmError.cpp") \
508+
range(0, 17) \
504509
\
505-
notproduct(bool, TestSafeFetchInErrorHandler, false, \
510+
develop(bool, TestSafeFetchInErrorHandler, false , \
506511
"If true, tests SafeFetch inside error handler.") \
507512
\
508-
notproduct(bool, TestUnresponsiveErrorHandler, false, \
513+
develop(bool, TestUnresponsiveErrorHandler, false, \
509514
"If true, simulates an unresponsive error handler.") \
510515
\
511516
develop(bool, Verbose, false, \

‎src/hotspot/share/utilities/debug.cpp

+37-14
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,36 @@ void report_vm_error(const char* file, int line, const char* error_msg)
238238
report_vm_error(file, line, error_msg, "%s", "");
239239
}
240240

241+
242+
static void print_error_for_unit_test(const char* message, const char* detail_fmt, va_list detail_args) {
243+
#ifdef ASSERT
244+
if (ExecutingUnitTests) {
245+
char detail_msg[256];
246+
if (detail_fmt != NULL) {
247+
// Special handling for the sake of gtest death tests which expect the assert
248+
// message to be printed in one short line to stderr (see TEST_VM_ASSERT_MSG) and
249+
// cannot be tweaked to accept our normal assert message.
250+
va_list detail_args_copy;
251+
va_copy(detail_args_copy, detail_args);
252+
jio_vsnprintf(detail_msg, sizeof(detail_msg), detail_fmt, detail_args_copy);
253+
254+
// the VM assert tests look for "assert failed: "
255+
if (message == NULL) {
256+
fprintf(stderr, "assert failed: %s", detail_msg);
257+
} else {
258+
if (strlen(detail_msg) > 0) {
259+
fprintf(stderr, "assert failed: %s: %s", message, detail_msg);
260+
} else {
261+
fprintf(stderr, "assert failed: Error: %s", message);
262+
}
263+
}
264+
::fflush(stderr);
265+
va_end(detail_args_copy);
266+
}
267+
}
268+
#endif // ASSERT
269+
}
270+
241271
void report_vm_error(const char* file, int line, const char* error_msg, const char* detail_fmt, ...)
242272
{
243273
if (Debugging || error_is_suppressed(file, line)) return;
@@ -250,20 +280,7 @@ void report_vm_error(const char* file, int line, const char* error_msg, const ch
250280
}
251281
#endif // CAN_SHOW_REGISTERS_ON_ASSERT
252282

253-
#ifdef ASSERT
254-
if (detail_fmt != NULL && ExecutingUnitTests) {
255-
// Special handling for the sake of gtest death tests which expect the assert
256-
// message to be printed in one short line to stderr (see TEST_VM_ASSERT_MSG) and
257-
// cannot be tweaked to accept our normal assert message.
258-
va_list detail_args_copy;
259-
va_copy(detail_args_copy, detail_args);
260-
::fputs("assert failed: ", stderr);
261-
::vfprintf(stderr, detail_fmt, detail_args_copy);
262-
::fputs("\n", stderr);
263-
::fflush(stderr);
264-
va_end(detail_args_copy);
265-
}
266-
#endif
283+
print_error_for_unit_test(error_msg, detail_fmt, detail_args);
267284

268285
VMError::report_and_die(Thread::current_or_null(), context, file, line, error_msg, detail_fmt, detail_args);
269286
va_end(detail_args);
@@ -285,6 +302,9 @@ void report_fatal(const char* file, int line, const char* detail_fmt, ...)
285302
context = g_assertion_context;
286303
}
287304
#endif // CAN_SHOW_REGISTERS_ON_ASSERT
305+
306+
print_error_for_unit_test("fatal error", detail_fmt, detail_args);
307+
288308
VMError::report_and_die(Thread::current_or_null(), context, file, line, "fatal error", detail_fmt, detail_args);
289309
va_end(detail_args);
290310
}
@@ -294,6 +314,9 @@ void report_vm_out_of_memory(const char* file, int line, size_t size,
294314
if (Debugging) return;
295315
va_list detail_args;
296316
va_start(detail_args, detail_fmt);
317+
318+
print_error_for_unit_test(NULL, detail_fmt, detail_args);
319+
297320
VMError::report_and_die(Thread::current_or_null(), file, line, size, vm_err_type, detail_fmt, detail_args);
298321
va_end(detail_args);
299322

‎src/hotspot/share/utilities/vmError.cpp

+14-76
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ void VMError::report(outputStream* st, bool _verbose) {
444444
"Runtime Environment to continue.");
445445
}
446446

447-
#ifndef PRODUCT
447+
#ifdef ASSERT
448448
// Error handler self tests
449449

450450
// test secondary error handling. Test it twice, to test that resetting
@@ -503,7 +503,7 @@ void VMError::report(outputStream* st, bool _verbose) {
503503
st->print_cr("not possible; skipped.");
504504
}
505505
}
506-
#endif // PRODUCT
506+
#endif // ASSERT
507507

508508
STEP("printing type of error")
509509

@@ -1737,7 +1737,7 @@ bool VMError::check_timeout() {
17371737

17381738
}
17391739

1740-
#ifndef PRODUCT
1740+
#ifdef ASSERT
17411741
typedef void (*voidfun_t)();
17421742

17431743
// Crash with an authentic sigfpe
@@ -1765,47 +1765,13 @@ static void crash_with_segfault() {
17651765

17661766
} // end: crash_with_segfault
17671767

1768-
void VMError::test_error_handler() {
1769-
controlled_crash(ErrorHandlerTest);
1770-
}
1771-
17721768
// crash in a controlled way:
1773-
// how can be one of:
1774-
// 1,2 - asserts
1775-
// 3,4 - guarantee
1776-
// 5-7 - fatal
1777-
// 8 - vm_exit_out_of_memory
1778-
// 9 - ShouldNotCallThis
1779-
// 10 - ShouldNotReachHere
1780-
// 11 - Unimplemented
1781-
// 12,13 - (not guaranteed) crashes
1769+
// 1 - assert
1770+
// 2 - guarantee
17821771
// 14 - SIGSEGV
17831772
// 15 - SIGFPE
17841773
void VMError::controlled_crash(int how) {
1785-
if (how == 0) return;
1786-
1787-
// If asserts are disabled, use the corresponding guarantee instead.
1788-
NOT_DEBUG(if (how <= 2) how += 2);
1789-
1790-
const char* const str = "hello";
1791-
const size_t num = (size_t)os::vm_page_size();
1792-
1793-
const char* const eol = os::line_separator();
1794-
const char* const msg = "this message should be truncated during formatting";
1795-
char * const dataPtr = NULL; // bad data pointer
1796-
const void (*funcPtr)(void); // bad function pointer
17971774

1798-
#if defined(PPC64) && !defined(ABI_ELFv2)
1799-
struct FunctionDescriptor functionDescriptor;
1800-
1801-
functionDescriptor.set_entry((address) 0xF);
1802-
funcPtr = (const void(*)()) &functionDescriptor;
1803-
#else
1804-
funcPtr = (const void(*)()) 0xF;
1805-
#endif
1806-
1807-
// Keep this in sync with test/hotspot/jtreg/runtime/ErrorHandling/ErrorHandler.java
1808-
// which tests cases 1 thru 13.
18091775
// Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java.
18101776
// Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java.
18111777
// Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java.
@@ -1821,30 +1787,10 @@ void VMError::controlled_crash(int how) {
18211787
}
18221788

18231789
switch (how) {
1824-
case 1: vmassert(str == NULL, "expected null"); break;
1825-
case 2: vmassert(num == 1023 && *str == 'X',
1826-
"num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1827-
case 3: guarantee(str == NULL, "expected null"); break;
1828-
case 4: guarantee(num == 1023 && *str == 'X',
1829-
"num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1830-
case 5: fatal("expected null"); break;
1831-
case 6: fatal("num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1832-
case 7: fatal("%s%s# %s%s# %s%s# %s%s# %s%s# "
1833-
"%s%s# %s%s# %s%s# %s%s# %s%s# "
1834-
"%s%s# %s%s# %s%s# %s%s# %s",
1835-
msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1836-
msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1837-
msg, eol, msg, eol, msg, eol, msg, eol, msg); break;
1838-
case 8: vm_exit_out_of_memory(num, OOM_MALLOC_ERROR, "ChunkPool::allocate"); break;
1839-
case 9: ShouldNotCallThis(); break;
1840-
case 10: ShouldNotReachHere(); break;
1841-
case 11: Unimplemented(); break;
1842-
// There's no guarantee the bad data pointer will crash us
1843-
// so "break" out to the ShouldNotReachHere().
1844-
case 12: *dataPtr = '\0'; break;
1845-
// There's no guarantee the bad function pointer will crash us
1846-
// so "break" out to the ShouldNotReachHere().
1847-
case 13: (*funcPtr)(); break;
1790+
case 1: assert(how == 0, "test assert"); break;
1791+
case 2: guarantee(how == 0, "test guarantee"); break;
1792+
1793+
// The other cases are unused.
18481794
case 14: crash_with_segfault(); break;
18491795
case 15: crash_with_sigfpe(); break;
18501796
case 16: {
@@ -1858,19 +1804,11 @@ void VMError::controlled_crash(int how) {
18581804
fatal("Force crash with a nested ThreadsListHandle.");
18591805
}
18601806
}
1861-
case 18: {
1862-
// Check for assert when allocating from resource area without a
1863-
// ResourceMark. There must not be a ResourceMark on the
1864-
// current stack when invoking this test case.
1865-
ResourceArea* area = Thread::current()->resource_area();
1866-
assert(area->nesting() == 0, "unexpected ResourceMark");
1867-
area->allocate_bytes(100);
1868-
break;
1869-
}
1870-
1871-
default: tty->print_cr("ERROR: %d: unexpected test_num value.", how);
1807+
default:
1808+
// If another number is given, give a generic crash.
1809+
fatal("Crashing with number %d", how);
18721810
}
1873-
tty->print_cr("VMError::controlled_crash: survived intentional crash. Did you suppress the assert?");
1811+
tty->print_cr("controlled_crash: survived intentional crash. Did you suppress the assert?");
18741812
ShouldNotReachHere();
18751813
}
1876-
#endif // !PRODUCT
1814+
#endif // !ASSERT

‎src/hotspot/share/utilities/vmError.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -183,9 +183,7 @@ class VMError : public AllStatic {
183183
// Support for avoiding multiple asserts
184184
static bool is_error_reported();
185185

186-
// Test vmassert(), fatal(), guarantee(), etc.
187-
NOT_PRODUCT(static void test_error_handler();)
188-
NOT_PRODUCT(static void controlled_crash(int how);)
186+
DEBUG_ONLY(static void controlled_crash(int how);)
189187

190188
// returns an address which is guaranteed to generate a SIGSEGV on read,
191189
// for test purposes, which is not NULL and contains bits in every word

‎test/hotspot/gtest/gc/g1/test_g1ServiceThread.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,19 @@ TEST_VM(G1ServiceTaskQueue, add_ordered) {
160160

161161
#ifdef ASSERT
162162
TEST_VM_ASSERT_MSG(G1ServiceTaskQueue, pop_empty,
163-
"Should never try to verify empty queue") {
163+
".*Should never try to verify empty queue") {
164164
G1ServiceTaskQueue queue;
165165
queue.pop();
166166
}
167167

168168
TEST_VM_ASSERT_MSG(G1ServiceTaskQueue, peek_empty,
169-
"Should never try to verify empty queue") {
169+
".*Should never try to verify empty queue") {
170170
G1ServiceTaskQueue queue;
171171
queue.peek();
172172
}
173173

174174
TEST_VM_ASSERT_MSG(G1ServiceTaskQueue, set_time_in_queue,
175-
"Not allowed to update time while in queue") {
175+
".*Not allowed to update time while in queue") {
176176
G1ServiceTaskQueue queue;
177177
TestTask a(100);
178178
queue.add_ordered(&a);

‎test/hotspot/gtest/metaspace/test_allocationGuard.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ using metaspace::Settings;
4444
// Note: We use TEST_VM_ASSERT_MSG. However, an assert is only triggered if allocation
4545
// guards are enabled; if guards are disabled for the gtests, this test would fail.
4646
// So for that case, we trigger a fake assert.
47-
TEST_VM_ASSERT_MSG(metaspace, test_overwriter, "Corrupt block") {
47+
TEST_VM_ASSERT_MSG(metaspace, test_overwriter, ".*failed: Corrupt block") {
4848

4949
if (Settings::use_allocation_guard()) {
5050
MetaspaceGtestContext context;

‎test/hotspot/gtest/metaspace/test_blocktree.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ TEST_VM(metaspace, BlockTree_print_test) {
218218
}
219219

220220
// Test that an overwritten node would result in an assert and a printed tree
221-
TEST_VM_ASSERT_MSG(metaspace, BlockTree_overwriter_test, "Invalid node") {
221+
TEST_VM_ASSERT_MSG(metaspace, BlockTree_overwriter_test, ".*failed: Invalid node") {
222222
static const size_t sizes1[] = { 30, 17, 0 };
223223
static const size_t sizes2[] = { 12, 12, 0 };
224224

‎test/hotspot/gtest/runtime/test_mutex_rank.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ TEST_OTHER_VM(MutexRank, mutex_lock_rank_in_order) {
4545
}
4646

4747
TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_rank_out_of_orderA,
48-
"Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {
48+
".* Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {
4949
JavaThread* THREAD = JavaThread::current();
5050
ThreadInVMfromNative invm(THREAD);
5151

@@ -59,7 +59,7 @@ TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_rank_out_of_orderA,
5959
}
6060

6161
TEST_VM_ASSERT_MSG(MutexRank, mutex_lock_rank_out_of_orderB,
62-
"Attempting to acquire lock mutex_rankB/50 out of order with lock mutex_rankA/50 -- possible deadlock") {
62+
".* Attempting to acquire lock mutex_rankB/50 out of order with lock mutex_rankA/50 -- possible deadlock") {
6363
JavaThread* THREAD = JavaThread::current();
6464
ThreadInVMfromNative invm(THREAD);
6565

@@ -89,7 +89,7 @@ TEST_OTHER_VM(MutexRank, mutex_trylock_rank_out_of_orderA) {
8989
}
9090

9191
TEST_VM_ASSERT_MSG(MutexRank, mutex_trylock_rank_out_of_orderB,
92-
"Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {
92+
".* Attempting to acquire lock mutex_rankA_plus_one/51 out of order with lock mutex_rankA/50 -- possible deadlock") {
9393
JavaThread* THREAD = JavaThread::current();
9494
ThreadInVMfromNative invm(THREAD);
9595

@@ -119,7 +119,7 @@ TEST_OTHER_VM(MutexRank, monitor_wait_rank_in_order) {
119119
}
120120

121121
TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order,
122-
"Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "
122+
".* Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "
123123
"-- possible deadlock. Should wait on the least ranked monitor from all owned locks.") {
124124
JavaThread* THREAD = JavaThread::current();
125125
ThreadInVMfromNative invm(THREAD);
@@ -135,7 +135,7 @@ TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order,
135135
}
136136

137137
TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order_trylock,
138-
"Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "
138+
".* Attempting to wait on monitor monitor_rankA_plus_one/51 while holding lock monitor_rankA/50 "
139139
"-- possible deadlock. Should wait on the least ranked monitor from all owned locks.") {
140140
JavaThread* THREAD = JavaThread::current();
141141
ThreadInVMfromNative invm(THREAD);
@@ -151,7 +151,7 @@ TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_out_of_order_trylock,
151151
}
152152

153153
TEST_VM_ASSERT_MSG(MutexRank, monitor_wait_rank_special,
154-
"Attempting to wait on monitor monitor_rank_special_minus_one/5 while holding lock monitor_rank_special/6 "
154+
".* Attempting to wait on monitor monitor_rank_special_minus_one/5 while holding lock monitor_rank_special/6 "
155155
"-- possible deadlock. Should not block\\(wait\\) while holding a lock of rank special.") {
156156
JavaThread* THREAD = JavaThread::current();
157157
ThreadInVMfromNative invm(THREAD);

0 commit comments

Comments
 (0)
This repository has been archived.