test/hotspot/jtreg/vmTestbase/nsk/stress/except/except002.java
author jcbeyler
Wed, 21 Nov 2018 09:38:27 -0800
changeset 52640 3a7d49718852
parent 52496 13f0aac77d60
permissions -rw-r--r--
8213721: [Graal] Tests vmTestbase/nsk/stress/except/except* may be encountering SEGV during out-of-memory conditions Summary: Add the exception handler only for the current thread Reviewed-by: dholmes, sspitsyn, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50169
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     1
/*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     4
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     8
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    13
 * accompanied this code).
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    14
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    18
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    21
 * questions.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    22
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    23
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    24
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    25
/*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    26
 * @test
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    27
 * @key stress
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    28
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    29
 * @summary converted from VM testbase nsk/stress/except/except002.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    30
 * VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    31
 * VM testbase readme:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    32
 * DESCRIPTION
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    33
 *     This checks if various exceptions are thrown (and caught) correctly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    34
 *     when there apparently are no free space in the heap to allocate new
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    35
 *     Throwable instance.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    36
 *     The test tries to occupy all of memory available in the heap by allocating
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    37
 *     lots of new Object() instances. Instances of the type Object are the smallest
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    38
 *     objects, so they apparently should occupy most fine-grained fragments in the
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    39
 *     heap and leave no free space for new Throwable instance. After that, the test
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    40
 *     provokes various exceptions (e.g.: by executing integer division by 0 and so
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    41
 *     on), and checks if appropriate exceptions are thrown.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    42
 * COMMENTS
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    43
 *     The test needs a lot of memory to start up, so it should not run under older
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    44
 *     JDK 1.1.x release due to its poorer heap utilization. Also, some checks are
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    45
 *     skipped when testing classic VM, because OutOfMemoryError is correctly thrown
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    46
 *     instead of target exception.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    47
 *     When the test is being self-initiating (i.e.: eating heap), memory occupation
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    48
 *     is terminated if memory allocation slows down crucially. This is a workaround
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    49
 *     intended to avoid the HotSpot bug:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    50
 *         #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    51
 *     There is also a workaround involved to avoid the following bugs known
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    52
 *     for HotSpot and for classic VM:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    53
 *         #4239841 (P1/S5) 1.1: poor garbage collector performance  (HotSpot bug)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    54
 *         #4245060 (P4/S5) poor garbage collector performance       (Classic VM bug)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    55
 *     However, printing of the test's error messages, warnings, and of execution
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    56
 *     trace fails under JDK 1.2 for Win32 even so. If the test fails due to this
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    57
 *     problem, exit status 96 is returned instead of 97.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    58
 *     JDK 1.3 classic VM for Sparc may crash (core dump) due to the known bug:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    59
 *         #4245057 (P2/S3) VM crashes when heap is exhausted
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    60
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    61
 * @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except002
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    62
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    63
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    64
package nsk.stress.except;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    65
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    66
import java.io.PrintStream;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    67
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    68
/**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    69
 * This checks if various exceptions are thrown (and caught) correctly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    70
 * when there apparently are no free space in the heap to allocate new
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    71
 * <code>Throwable</code> instance.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    72
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    73
 * <p>The test tries to occupy all of memory available in the heap by
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    74
 * allocating lots of new <code>Object()</code> instances. Instances of the
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    75
 * type <code>Object</code> are the smallest objects, so they apparently should
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    76
 * occupy most fine-grained fragments in the heap and leave no free space for
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    77
 * new <code>Throwable</code> instance. After that, the test provokes various
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    78
 * exceptions (e.g.: by executing integer division by 0 and so on), and checks
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    79
 * if appropriate exceptions are thrown.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    80
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    81
 * <p>Note, that memory occupation is terminated if memory allocation slows
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    82
 * down crucially. This is a workaround intended to avoid the HotSpot bug:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    83
 * <br>&nbsp;&nbsp;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    84
 * #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    85
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    86
 * <p>There is also a workaround involved to avoid the following bugs known
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    87
 * for HotSpot and for classic VM:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    88
 * <br>&nbsp;&nbsp;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    89
 * #4239841 (P1/S5) 1.1: poor garbage collector performance
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    90
 * <br>&nbsp;&nbsp;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    91
 * #4245060 (P4/S5) poor garbage collector performance
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    92
 * <br>However, printing of the test's error messages, warnings, and of
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    93
 * execution trace may fail even so. If the test fails due to poor GC
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    94
 * performance, exit status 96 is returned instead of 97.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    95
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    96
 * <p>Also note, that the test needs a lot of memory to start up, so it should
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    97
 * not run under older JDK 1.1.x release due to its poor heap utilization.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    98
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    99
public class except002 {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   100
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   101
     * Either allow or supress printing of execution trace.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   102
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   103
    private static boolean TRACE_ON = false;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   104
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   105
     * Either allow or supress printing of warning messages.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   106
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   107
    private static final boolean WARN_ON = true;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   108
    /*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   109
     * Storage for a lot of tiny objects
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   110
     * "static volatile" keywords are for preventing heap optimization
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   111
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   112
    public static volatile Object pool[] = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   113
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   114
     * Temporary <code>log</code> for error messages, warnings and/or execution trace.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   115
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   116
     * @see #messages
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   117
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   118
    private static String log[] = new String[1000]; // up to 1000 messages
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   119
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   120
     * How many <code>messages</code> were submitted to the <code>log</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   121
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   122
     * @see #log
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   123
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   124
    private static int messages = 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   125
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   126
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   127
     * Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   128
     * and print the test summary - either test passed of failed.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   129
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   130
    public static int run(String args[], PrintStream out) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   131
        if (args.length > 0) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   132
            if (args[0].toLowerCase().startsWith("-v"))
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   133
                TRACE_ON = true;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   134
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   135
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   136
        int exitCode = run(out);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   137
        pool = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   138
        System.gc();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   139
        // Print the log[] and the test summary:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   140
        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   141
            for (int i = 0; i < messages; i++)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   142
                out.println(log[i]);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   143
            if (exitCode == 0) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   144
                if (TRACE_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   145
                    out.println("Test passed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   146
            } else
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   147
                out.println("Test failed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   148
        } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   149
            // Poor performance of garbage collector:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   150
            exitCode = 1;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   151
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   152
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   153
        return exitCode;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   154
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   155
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   156
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   157
     * Allocate as much <code>Object</code> instances as possible to bring JVM
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   158
     * into stress, and then check if exceptions are correctly thrown accordingly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   159
     * to various situations like integer division by 0, etc.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   160
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   161
    private static int run(PrintStream out) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   162
        out.println("# While printing this message, JVM seems to initiate the output");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   163
        out.println("# stream, so that it will not need more memory to print later,");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   164
        out.println("# when the heap would fail to provide more memory.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   165
        out.println("# ");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   166
        out.println("# Note, that the test maintains especial static log[] field in");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   167
        out.println("# order to avoid printing when the heap seems exhausted.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   168
        out.println("# Nevertheless, printing could arise OutOfMemoryError even");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   169
        out.println("# after all the memory allocated by the test is released.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   170
        out.println("# ");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   171
        out.println("# That problem is caused by the known JDK/HotSpot bugs:");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   172
        out.println("#     4239841 (P1/S5) 1.1: poor garbage collector performance");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   173
        out.println("#     4245060 (P4/S5) poor garbage collector performance");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   174
        out.println("# ");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   175
        out.println("# This message is just intended to work-around that problem.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   176
        out.println("# If printing should fail even so, the test will return the");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   177
        out.println("# exit status 96 instead of 97 to indicate the problem.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   178
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   179
        // Prepare some items, which will be used by the test:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   180
        Object trash = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   181
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   182
        // Sum up exit code:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   183
        int exitCode = 0; // apparently PASSED
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   184
        int skipped = 0;  // some checks may correctly suffer OutOfMemoryError
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   185
        // Allocate repository for a lots of tiny objects:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   186
        for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   187
            try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   188
                pool = new Object[size];
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   189
            } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   190
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   191
        if (pool == null)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   192
            throw new Error("HS bug: cannot allocate new Object[1]");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   193
        int poolSize = pool.length;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   194
        int index = 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   195
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   196
        // Sum up time spent, when it was hard to JVM to allocate next object
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   197
        // (i.e.: when JVM has spent more than 1 second to allocate new object):
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   198
        double totalDelay = 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   199
        long timeMark = System.currentTimeMillis();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   200
        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   201
            for (; index < poolSize; index++) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   202
                //-------------------------
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   203
                pool[index] = new Object();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   204
                long nextTimeMark = System.currentTimeMillis();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   205
                long elapsed = nextTimeMark - timeMark;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   206
                timeMark = nextTimeMark;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   207
                //----------------------
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   208
                if (elapsed > 1000) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   209
                    double seconds = elapsed / 1000.0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   210
                    if (TRACE_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   211
                        out.println(
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   212
                                "pool[" + index + "]=new Object(); // elapsed " + seconds + "s");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   213
                    totalDelay += seconds;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   214
                    if (totalDelay > 60) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   215
                        if (TRACE_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   216
                            out.println(
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   217
                                    "Memory allocation became slow; so, heap seems exhausted.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   218
                        break;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   219
                    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   220
                }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   221
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   222
        } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   223
            if (TRACE_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   224
                log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   225
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   226
            // Do not release any byte once allocated:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   227
            pool[index++] = oome;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   228
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   229
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   230
        if (index > poolSize - 1000) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   231
            if (WARN_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   232
                log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   233
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   234
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   235
        // Check ClassNotFoundException (positive):
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   236
        try {
52496
13f0aac77d60 8213718: [TEST] Wrong classname in vmTestbase/nsk/stress/except/except002 and except003
dholmes
parents: 50169
diff changeset
   237
            trash = Class.forName("nsk.stress.except.except002$Abra$Cadabra"); //   correct - should pass
13f0aac77d60 8213718: [TEST] Wrong classname in vmTestbase/nsk/stress/except/except002 and except003
dholmes
parents: 50169
diff changeset
   238
//          trash = Class.forName("nsk.stress.except.except002.Abra.Cadabra"); // incorrect - should fail
50169
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   239
            if (TRACE_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   240
                log[messages++] = "Success: ClassNotFoundException (positive)";
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   241
        } catch (ClassNotFoundException cnfe) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   242
            log[messages++] = "Failure: ClassNotFoundException (positive)";
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   243
            exitCode = 2;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   244
        } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   245
            if (WARN_ON)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   246
                log[messages++] = "Skipped: ClassNotFoundException (positive)";
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   247
            skipped++;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   248
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   249
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   250
        return exitCode;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   251
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   252
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   253
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   254
     * Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   255
     * (The stream <code>out</code> is assigned to <code>System.out</code> here.)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   256
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   257
     * @see #run(String[], PrintStream)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   258
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   259
    public static void main(String args[]) {
52640
3a7d49718852 8213721: [Graal] Tests vmTestbase/nsk/stress/except/except* may be encountering SEGV during out-of-memory conditions
jcbeyler
parents: 52496
diff changeset
   260
        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
50169
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   261
            // Last try. If there is some exception outside the code, test should end correctly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   262
            @Override
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   263
            public void uncaughtException(Thread t, Throwable e) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   264
                try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   265
                    pool = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   266
                    log = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   267
                    System.gc();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   268
                    if (e instanceof OutOfMemoryError) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   269
                        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   270
                            System.out.println("OOME : Test Skipped");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   271
                            System.exit(0);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   272
                        } catch (Throwable ignore) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   273
                        } // No code in the handler can provoke correct exceptions.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   274
                    } else {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   275
                        e.printStackTrace();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   276
                        throw (RuntimeException) e;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   277
                    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   278
                } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   279
                }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   280
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   281
        });
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   282
        int exitCode = run(args, System.out);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   283
        System.exit(exitCode + 95);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   284
        // JCK-like exit status.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   285
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   286
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   287
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   288
     * This class should be used to check <code>ClassNotFoundException</code>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   289
     * and <code>IllegalAccessException</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   290
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   291
    private static class Abra {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   292
        /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   293
         * Will try to incorrectly find this class as <code>Cadabra</code>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   294
         * instead of <code>Abra$Cadabra</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   295
         */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   296
        public static class Cadabra {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   297
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   298
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   299
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   300
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   301
}