1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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 
  25 package org.graalvm.compiler.hotspot.test;
  26 
  27 import java.io.IOException;
  28 import java.util.concurrent.locks.ReentrantLock;
  29 import java.util.List;
  30 
  31 import org.graalvm.compiler.test.SubprocessUtil;
  32 import org.graalvm.compiler.test.SubprocessUtil.Subprocess;
  33 
  34 import org.junit.Assume;
  35 import org.junit.Before;
  36 import org.junit.Test;
  37 
  38 public class ReservedStackAccessTest extends HotSpotGraalCompilerTest {
  39     @Before
  40     public void check() {
  41         Assume.assumeTrue(runtime().getVMConfig().enableStackReservedZoneAddress != 0);
  42     }
  43 
  44     public void stackAccessTest() {
  45         Assume.assumeTrue(runtime().getVMConfig().enableStackReservedZoneAddress != 0);
  46 
  47         int passed = 0;
  48         for (int i = 0; i < 1000; i++) {
  49             // Each iteration has to be executed by a new thread. The test
  50             // relies on the random size area pushed by the VM at the beginning
  51             // of the stack of each Java thread it creates.
  52             RunWithSOEContext r = new RunWithSOEContext(new ReentrantLockTest(), 256);
  53             Thread thread = new Thread(r);
  54             thread.start();
  55             try {
  56                 thread.join();
  57                 assertTrue(r.result.equals("PASSED"), r.result);
  58                 ++passed;
  59             } catch (InterruptedException ex) {
  60             }
  61         }
  62         System.out.println("RESULT: " + (passed == 1000 ? "PASSED" : "FAILED"));
  63     }
  64 
  65     public static void main(String[] args) {
  66         new ReservedStackAccessTest().stackAccessTest();
  67     }
  68 
  69     @Test
  70     public void run() throws IOException, InterruptedException {
  71         Assume.assumeTrue(runtime().getVMConfig().enableStackReservedZoneAddress != 0);
  72         List<String> vmArgs = SubprocessUtil.withoutDebuggerArguments(SubprocessUtil.getVMCommandLine());
  73         vmArgs.add("-XX:+UseJVMCICompiler");
  74         vmArgs.add("-Dgraal.Inline=false");
  75         vmArgs.add("-XX:CompileCommand=exclude,java/util/concurrent/locks/AbstractOwnableSynchronizer.setExclusiveOwnerThread");
  76         Subprocess proc = SubprocessUtil.java(vmArgs, ReservedStackAccessTest.class.getName());
  77         boolean passed = false;
  78         for (String line : proc.output) {
  79             if (line.equals("RESULT: PASSED")) {
  80                 passed = true;
  81             }
  82         }
  83         if (!passed) {
  84             for (String line : proc.output) {
  85                 System.err.println("" + line);
  86             }
  87         }
  88         assertTrue(passed);
  89     }
  90 
  91     static class ReentrantLockTest {
  92 
  93         private ReentrantLock[] lockArray;
  94         // Frame sizes vary a lot between interpreted code and compiled code
  95         // so the lock array has to be big enough to cover all cases.
  96         // If test fails with message "Not conclusive test", try to increase
  97         // LOCK_ARRAY_SIZE value
  98         private static final int LOCK_ARRAY_SIZE = 8192;
  99         private boolean stackOverflowErrorReceived;
 100         StackOverflowError soe = null;
 101         int index = -1;
 102 
 103         public void initialize() {
 104             lockArray = new ReentrantLock[LOCK_ARRAY_SIZE];
 105             for (int i = 0; i < LOCK_ARRAY_SIZE; i++) {
 106                 lockArray[i] = new ReentrantLock();
 107             }
 108             stackOverflowErrorReceived = false;
 109         }
 110 
 111         public String getResult() {
 112             if (!stackOverflowErrorReceived) {
 113                 return "ERROR: Not conclusive test: no StackOverflowError received";
 114             }
 115             for (int i = 0; i < LOCK_ARRAY_SIZE; i++) {
 116                 if (lockArray[i].isLocked()) {
 117                     if (!lockArray[i].isHeldByCurrentThread()) {
 118                         StringBuilder s = new StringBuilder();
 119                         s.append("FAILED: ReentrantLock ");
 120                         s.append(i);
 121                         s.append(" looks corrupted");
 122                         return s.toString();
 123                     }
 124                 }
 125             }
 126             return "PASSED";
 127         }
 128 
 129         public void run() {
 130             try {
 131                 lockAndCall(0);
 132             } catch (StackOverflowError e) {
 133                 soe = e;
 134                 stackOverflowErrorReceived = true;
 135             }
 136         }
 137 
 138         private void lockAndCall(int i) {
 139             index = i;
 140             if (i < LOCK_ARRAY_SIZE) {
 141                 lockArray[i].lock();
 142                 lockAndCall(i + 1);
 143             }
 144         }
 145     }
 146 
 147     static class RunWithSOEContext implements Runnable {
 148 
 149         int counter;
 150         int deframe;
 151         int decounter;
 152         int setupSOEFrame;
 153         int testStartFrame;
 154         ReentrantLockTest test;
 155         String result = "FAILED: no result";
 156 
 157         RunWithSOEContext(ReentrantLockTest test, int deframe) {
 158             this.test = test;
 159             this.deframe = deframe;
 160         }
 161 
 162         @Override
 163         public void run() {
 164             counter = 0;
 165             decounter = deframe;
 166             test.initialize();
 167             recursiveCall();
 168             System.out.println("Framework got StackOverflowError at frame = " + counter);
 169             System.out.println("Test started execution at frame = " + (counter - deframe));
 170             result = test.getResult();
 171         }
 172 
 173         @SuppressWarnings("unused")
 174         void recursiveCall() {
 175             // Unused local variables to increase the frame size
 176             long l1;
 177             long l2;
 178             long l3;
 179             long l4;
 180             long l5;
 181             long l6;
 182             long l7;
 183             long l8;
 184             long l9;
 185             long l10;
 186             long l11;
 187             long l12;
 188             long l13;
 189             long l14;
 190             long l15;
 191             long l16;
 192             long l17;
 193             long l18;
 194             long l19;
 195             long l20;
 196             long l21;
 197             long l22;
 198             long l23;
 199             long l24;
 200             long l25;
 201             long l26;
 202             long l27;
 203             long l28;
 204             long l30;
 205             long l31;
 206             long l32;
 207             long l33;
 208             long l34;
 209             long l35;
 210             long l36;
 211             long l37;
 212             counter++;
 213             try {
 214                 recursiveCall();
 215             } catch (StackOverflowError e) {
 216             }
 217             decounter--;
 218             if (decounter == 0) {
 219                 setupSOEFrame = counter;
 220                 testStartFrame = counter - deframe;
 221                 test.run();
 222             }
 223         }
 224     }
 225 
 226 }