hotspot/test/runtime/Unsafe/Reallocate.java
author ctornqvi
Fri, 09 Jan 2015 08:34:32 -0800
changeset 28473 cff666a4057d
parent 28470 bf674dd8644e
child 29678 dd2f3932c21e
permissions -rw-r--r--
8068733: [TESTBUG] runtime/Unsafe/Reallocate.java sometimes fails when running with -Xcomp Reviewed-by: coleenp, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28470
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     1
/*
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     4
 *
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     8
 *
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    14
 *
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    18
 *
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    21
 * questions.
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    22
 */
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    23
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    24
/*
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    25
 * @test
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    26
 * @bug 8058897
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    27
 * @library /testlibrary
28473
cff666a4057d 8068733: [TESTBUG] runtime/Unsafe/Reallocate.java sometimes fails when running with -Xcomp
ctornqvi
parents: 28470
diff changeset
    28
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:MallocMaxTestWords=100m Reallocate
28470
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    29
 */
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    30
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    31
import com.oracle.java.testlibrary.*;
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    32
import sun.misc.Unsafe;
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    33
import static com.oracle.java.testlibrary.Asserts.*;
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    34
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    35
public class Reallocate {
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    36
    public static void main(String args[]) throws Exception {
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    37
        Unsafe unsafe = Utils.getUnsafe();
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    38
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    39
        long address = unsafe.allocateMemory(1);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    40
        assertNotEquals(address, 0L);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    41
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    42
        // Make sure we reallocate correctly
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    43
        unsafe.putByte(address, Byte.MAX_VALUE);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    44
        address = unsafe.reallocateMemory(address, 2);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    45
        assertNotEquals(address, 0L);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    46
        assertEquals(unsafe.getByte(address), Byte.MAX_VALUE);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    47
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    48
        // Reallocating with a 0 size should return a null pointer
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    49
        address = unsafe.reallocateMemory(address, 0);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    50
        assertEquals(address, 0L);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    51
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    52
        // Reallocating with a null pointer should result in a normal allocation
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    53
        address = unsafe.reallocateMemory(0L, 1);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    54
        assertNotEquals(address, 0L);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    55
        unsafe.putByte(address, Byte.MAX_VALUE);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    56
        assertEquals(unsafe.getByte(address), Byte.MAX_VALUE);
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    57
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    58
        // Make sure we can throw an OOME when we fail to reallocate due to OOM
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    59
        try {
28473
cff666a4057d 8068733: [TESTBUG] runtime/Unsafe/Reallocate.java sometimes fails when running with -Xcomp
ctornqvi
parents: 28470
diff changeset
    60
            unsafe.reallocateMemory(address, 100 * 1024 * 1024 * 8);
28470
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    61
        } catch (OutOfMemoryError e) {
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    62
            // Expected
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    63
            return;
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    64
        }
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    65
        throw new RuntimeException("Did not get expected OOM");
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    66
    }
bf674dd8644e 8058897: Unsafe.reallocateMemory() ignores -XX:MallocMaxTestWords setting
ctornqvi
parents:
diff changeset
    67
}