1 /*
   2  * Copyright (c) 2013, 2013, 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 package org.graalvm.compiler.hotspot.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  30 
  31 import jdk.vm.ci.code.InvalidInstalledCodeException;
  32 import jdk.vm.ci.hotspot.HotSpotNmethod;
  33 import jdk.vm.ci.meta.ResolvedJavaMethod;
  34 
  35 public class HotSpotNmethodTest extends GraalCompilerTest {
  36 
  37     private static final int ITERATION_COUNT = 100000;
  38 
  39     @Test
  40     public void testInstallCodeInvalidation() {
  41         final ResolvedJavaMethod testJavaMethod = getResolvedJavaMethod("foo");
  42         final HotSpotNmethod nmethod = (HotSpotNmethod) getCode(testJavaMethod, parseEager("otherFoo", AllowAssumptions.YES));
  43         Assert.assertTrue(nmethod.isValid());
  44         Object result;
  45         try {
  46             result = nmethod.executeVarargs(null, "b", "c");
  47             assertDeepEquals(43, result);
  48         } catch (InvalidInstalledCodeException e) {
  49             Assert.fail("Code was invalidated");
  50         }
  51         Assert.assertTrue(nmethod.isValid());
  52         nmethod.invalidate();
  53         Assert.assertFalse(nmethod.isValid());
  54         try {
  55             result = nmethod.executeVarargs(null, null, null);
  56             Assert.fail("Code was not invalidated");
  57         } catch (InvalidInstalledCodeException e) {
  58         }
  59         Assert.assertFalse(nmethod.isValid());
  60     }
  61 
  62     @Test
  63     public void testInstallCodeInvalidationWhileRunning() {
  64         final ResolvedJavaMethod testJavaMethod = getResolvedJavaMethod("foo");
  65         final HotSpotNmethod nmethod = (HotSpotNmethod) getCode(testJavaMethod, parseEager("otherFoo", AllowAssumptions.YES));
  66         Object result;
  67         try {
  68             result = nmethod.executeVarargs(nmethod, null, null);
  69             assertDeepEquals(43, result);
  70         } catch (InvalidInstalledCodeException e) {
  71             Assert.fail("Code was invalidated");
  72         }
  73         Assert.assertFalse(nmethod.isValid());
  74     }
  75 
  76     @Test
  77     public void testInstalledCodeCalledFromCompiledCode() {
  78         final ResolvedJavaMethod testJavaMethod = getResolvedJavaMethod("foo");
  79         final HotSpotNmethod nmethod = (HotSpotNmethod) getCode(testJavaMethod, parseEager("otherFoo", AllowAssumptions.YES));
  80         Assert.assertTrue(nmethod.isValid());
  81         try {
  82             for (int i = 0; i < ITERATION_COUNT; ++i) {
  83                 nmethod.executeVarargs(null, "b", "c");
  84             }
  85         } catch (InvalidInstalledCodeException e) {
  86             Assert.fail("Code was invalidated");
  87         }
  88     }
  89 
  90     @SuppressWarnings("unused")
  91     public static Object foo(HotSpotNmethod method, Object a2, Object a3) {
  92         return 42;
  93     }
  94 
  95     @SuppressWarnings("unused")
  96     public static Object otherFoo(HotSpotNmethod method, Object a2, Object a3) {
  97         if (method != null) {
  98             method.invalidate();
  99         }
 100         return 43;
 101     }
 102 }