1 /*
   2  * Copyright (c) 2014, 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 test.java.lang.invoke.MethodHandles;
  24 
  25 import com.oracle.testlibrary.jsr292.Helper;
  26 import jdk.testlibrary.Asserts;
  27 import jdk.testlibrary.TimeLimitedRunner;
  28 import jdk.testlibrary.Utils;
  29 
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.invoke.MethodType;
  33 import java.lang.reflect.Array;
  34 import java.util.*;
  35 import java.util.function.BiFunction;
  36 import java.util.function.Function;
  37 import java.util.function.Supplier;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 /* @test
  41  * @library /lib/testlibrary/jsr292 /lib/testlibrary/
  42  * @compile CatchExceptionTest.java
  43  * @run main/othervm -esa test.java.lang.invoke.MethodHandles.CatchExceptionTest
  44  * @key intermittent
  45  */
  46 public class CatchExceptionTest {
  47     private static final List<Class<?>> ARGS_CLASSES;
  48     protected static final int MAX_ARITY = Helper.MAX_ARITY - 1;
  49 
  50     static {
  51         Class<?> classes[] = {
  52                 Object.class,
  53                 long.class,
  54                 int.class,
  55                 byte.class,
  56                 Integer[].class,
  57                 double[].class,
  58                 String.class,
  59         };
  60         ARGS_CLASSES = Collections.unmodifiableList(
  61                 Helper.randomClasses(classes, MAX_ARITY));
  62     }
  63 
  64     private final TestCase testCase;
  65     private final int nargs;
  66     private final int argsCount;
  67     private final MethodHandle catcher;
  68     private int dropped;
  69     private MethodHandle thrower;
  70 
  71     public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
  72             final int catchDrops) {
  73         this.testCase = testCase;
  74         this.dropped = catchDrops;
  75         if (Helper.IS_VERBOSE) {
  76             System.out.printf("CatchException::CatchException(%s, isVararg=%b " +
  77                             "argsCount=%d catchDrops=%d)%n",
  78                     testCase, isVararg, argsCount, catchDrops
  79             );
  80         }
  81         MethodHandle thrower = testCase.thrower;
  82         int throwerLen = thrower.type().parameterCount();
  83         List<Class<?>> classes;
  84         int extra = Math.max(0, argsCount - throwerLen);
  85         classes = getThrowerParams(isVararg, extra);
  86         this.argsCount = throwerLen + classes.size();
  87         thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
  88         if (isVararg && argsCount > throwerLen) {
  89             MethodType mt = thrower.type();
  90             Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
  91             thrower = thrower.asVarargsCollector(lastParam);
  92         }
  93         this.thrower = thrower;
  94         this.dropped = Math.min(this.argsCount, catchDrops);
  95         catcher = testCase.getCatcher(getCatcherParams());
  96         nargs = Math.max(2, this.argsCount);
  97     }
  98 
  99     public static void main(String[] args) throws Throwable {
 100         TestFactory factory = new TestFactory();
 101         long timeout = Helper.IS_THOROUGH ? 0L : Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
 102         // substract vm init time and reserve time for vm exit
 103         timeout *= 0.9;
 104         TimeLimitedRunner runner = new TimeLimitedRunner(timeout, 2.0d,
 105                 () -> {
 106                     CatchExceptionTest test = factory.nextTest();
 107                     if (test != null) {
 108                         test.runTest();
 109                         return true;
 110                     }
 111                     return false;
 112                 });
 113         for (CatchExceptionTest test : TestFactory.MANDATORY_TEST_CASES) {
 114             test.runTest();
 115         }
 116         runner.call();
 117     }
 118 
 119     private List<Class<?>> getThrowerParams(boolean isVararg, int argsCount) {
 120         return Helper.getParams(ARGS_CLASSES, isVararg, argsCount);
 121     }
 122 
 123 
 124     private List<Class<?>> getCatcherParams() {
 125         int catchArgc = 1 + this.argsCount - dropped;
 126         List<Class<?>> result = new ArrayList<>(
 127                 thrower.type().parameterList().subList(0, catchArgc - 1));
 128         // prepend throwable
 129         result.add(0, testCase.throwableClass);
 130         return result;
 131     }
 132 
 133     private void runTest() {
 134         Helper.clear();
 135 
 136         Object[] args = Helper.randomArgs(
 137                 argsCount, thrower.type().parameterArray());
 138         Object arg0 = Helper.MISSING_ARG;
 139         Object arg1 = testCase.thrown;
 140         if (argsCount > 0) {
 141             arg0 = args[0];
 142         }
 143         if (argsCount > 1) {
 144             args[1] = arg1;
 145         }
 146         Asserts.assertEQ(nargs, thrower.type().parameterCount());
 147         if (argsCount < nargs) {
 148             Object[] appendArgs = {arg0, arg1};
 149             appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
 150             thrower = MethodHandles.insertArguments(
 151                     thrower, argsCount, appendArgs);
 152         }
 153         Asserts.assertEQ(argsCount, thrower.type().parameterCount());
 154 
 155         MethodHandle target = MethodHandles.catchException(
 156                 testCase.filter(thrower), testCase.throwableClass,
 157                 testCase.filter(catcher));
 158 
 159         Asserts.assertEQ(thrower.type(), target.type());
 160         Asserts.assertEQ(argsCount, target.type().parameterCount());
 161 
 162         Object returned;
 163         try {
 164             returned = target.invokeWithArguments(args);
 165         } catch (Throwable ex) {
 166             testCase.assertCatch(ex);
 167             returned = ex;
 168         }
 169 
 170         testCase.assertReturn(returned, arg0, arg1, dropped, args);
 171     }
 172 }
 173 
 174 class TestFactory {
 175     public static final List<CatchExceptionTest> MANDATORY_TEST_CASES = new ArrayList<>();
 176 
 177     private static final int MIN_TESTED_ARITY = 10;
 178 
 179     static {
 180         for (int[] args : new int[][]{
 181                 {0, 0},
 182                 {MIN_TESTED_ARITY, 0},
 183                 {MIN_TESTED_ARITY, MIN_TESTED_ARITY},
 184                 {CatchExceptionTest.MAX_ARITY, 0},
 185                 {CatchExceptionTest.MAX_ARITY, CatchExceptionTest.MAX_ARITY},
 186         }) {
 187                 MANDATORY_TEST_CASES.addAll(createTests(args[0], args[1]));
 188         }
 189     }
 190 
 191     private int count;
 192     private int args;
 193     private int dropArgs;
 194     private int currentMaxDrops;
 195     private int maxArgs;
 196     private int maxDrops;
 197     private int constructor;
 198     private int constructorSize;
 199     private boolean isVararg;
 200 
 201     public TestFactory() {
 202         if (Helper.IS_THOROUGH) {
 203             maxArgs = maxDrops = CatchExceptionTest.MAX_ARITY;
 204         } else {
 205             maxArgs = MIN_TESTED_ARITY
 206                     + Helper.RNG.nextInt(CatchExceptionTest.MAX_ARITY
 207                             - MIN_TESTED_ARITY)
 208                     + 1;
 209             maxDrops = MIN_TESTED_ARITY
 210                     + Helper.RNG.nextInt(maxArgs - MIN_TESTED_ARITY)
 211                     + 1;
 212             args = 1;
 213         }
 214 
 215         if (Helper.IS_VERBOSE) {
 216             System.out.printf("maxArgs = %d%nmaxDrops = %d%n",
 217                     maxArgs, maxDrops);
 218         }
 219         constructorSize = TestCase.CONSTRUCTORS.size();
 220     }
 221 
 222     private static List<CatchExceptionTest> createTests(int argsCount,
 223             int catchDrops) {
 224         if (catchDrops > argsCount || argsCount < 0 || catchDrops < 0) {
 225             throw new IllegalArgumentException("argsCount = " + argsCount
 226                     + ", catchDrops = " + catchDrops
 227             );
 228         }
 229         List<CatchExceptionTest> result = new ArrayList<>(
 230                 TestCase.CONSTRUCTORS.size());
 231         for (Supplier<TestCase> constructor : TestCase.CONSTRUCTORS) {
 232             result.add(new CatchExceptionTest(constructor.get(),
 233                     /* isVararg = */ true,
 234                     argsCount,
 235                     catchDrops));
 236             result.add(new CatchExceptionTest(constructor.get(),
 237                     /* isVararg = */ false,
 238                     argsCount,
 239                     catchDrops));
 240         }
 241         return result;
 242     }
 243 
 244     /**
 245      * @return next test from test matrix:
 246      * {varArgs, noVarArgs} x TestCase.rtypes x TestCase.THROWABLES x {1, .., maxArgs } x {1, .., maxDrops}
 247      */
 248     public CatchExceptionTest nextTest() {
 249         if (constructor < constructorSize) {
 250             return createTest();
 251         }
 252         constructor = 0;
 253         count++;
 254         if (!Helper.IS_THOROUGH && count > Helper.TEST_LIMIT) {
 255             System.out.println("test limit is exceeded");
 256             return null;
 257         }
 258         if (dropArgs <= currentMaxDrops) {
 259             if (dropArgs == 1) {
 260                 if (Helper.IS_THOROUGH || Helper.RNG.nextBoolean()) {
 261                     ++dropArgs;
 262                     return createTest();
 263                 } else if (Helper.IS_VERBOSE) {
 264                     System.out.printf(
 265                             "argsCount=%d : \"drop\" scenarios are skipped%n",
 266                             args);
 267                 }
 268             } else {
 269                 ++dropArgs;
 270                 return createTest();
 271             }
 272         }
 273 
 274         if (args <= maxArgs) {
 275             dropArgs = 1;
 276             currentMaxDrops = Math.min(args, maxDrops);
 277             ++args;
 278             return createTest();
 279         }
 280         return null;
 281     }
 282 
 283     private CatchExceptionTest createTest() {
 284         if (!Helper.IS_THOROUGH) {
 285             return new CatchExceptionTest(
 286                     TestCase.CONSTRUCTORS.get(constructor++).get(),
 287                     Helper.RNG.nextBoolean(), args, dropArgs);
 288         } else {
 289            if (isVararg) {
 290                isVararg = false;
 291                return new CatchExceptionTest(
 292                        TestCase.CONSTRUCTORS.get(constructor++).get(),
 293                        isVararg, args, dropArgs);
 294            } else {
 295                isVararg = true;
 296                return new CatchExceptionTest(
 297                        TestCase.CONSTRUCTORS.get(constructor).get(),
 298                        isVararg, args, dropArgs);
 299            }
 300         }
 301     }
 302 }
 303 
 304 class TestCase<T> {
 305     private static enum ThrowMode {
 306         NOTHING,
 307         CAUGHT,
 308         UNCAUGHT,
 309         ADAPTER
 310     }
 311 
 312     @SuppressWarnings("unchecked")
 313     public static final List<Supplier<TestCase>> CONSTRUCTORS;
 314     private static final MethodHandle FAKE_IDENTITY;
 315     private static final MethodHandle THROW_OR_RETURN;
 316     private static final MethodHandle CATCHER;
 317 
 318     static {
 319         try {
 320             MethodHandles.Lookup lookup = MethodHandles.lookup();
 321             THROW_OR_RETURN = lookup.findStatic(
 322                     TestCase.class,
 323                     "throwOrReturn",
 324                     MethodType.methodType(Object.class, Object.class,
 325                             Throwable.class)
 326             );
 327             CATCHER = lookup.findStatic(
 328                     TestCase.class,
 329                     "catcher",
 330                     MethodType.methodType(Object.class, Object.class));
 331             FAKE_IDENTITY = lookup.findVirtual(
 332                     TestCase.class, "fakeIdentity",
 333                     MethodType.methodType(Object.class, Object.class));
 334 
 335         } catch (NoSuchMethodException | IllegalAccessException e) {
 336             throw new Error(e);
 337         }
 338         PartialConstructor[] constructors = {
 339                 create(Object.class, Object.class::cast),
 340                 create(String.class, Objects::toString),
 341                 create(int[].class, x -> new int[]{Objects.hashCode(x)}),
 342                 create(long.class,
 343                         x -> Objects.hashCode(x) & (-1L >>> 32)),
 344                 create(void.class, TestCase::noop)};
 345         Throwable[] throwables = {
 346                 new ClassCastException("testing"),
 347                 new java.io.IOException("testing"),
 348                 new LinkageError("testing")};
 349         List<Supplier<TestCase>> list = new ArrayList<>(constructors.length *
 350                 throwables.length * ThrowMode.values().length);
 351         //noinspection unchecked
 352         for (PartialConstructor f : constructors) {
 353             for (ThrowMode mode : ThrowMode.values()) {
 354                 for (Throwable t : throwables) {
 355                     list.add(f.apply(mode, t));
 356                 }
 357             }
 358         }
 359         CONSTRUCTORS = Collections.unmodifiableList(list);
 360     }
 361 
 362     public final Class<T> rtype;
 363     public final ThrowMode throwMode;
 364     public final Throwable thrown;
 365     public final Class<? extends Throwable> throwableClass;
 366     /**
 367      * MH which takes 2 args (Object,Throwable), 1st is the return value,
 368      * 2nd is the exception which will be thrown, if it's supposed in current
 369      * {@link #throwMode}.
 370      */
 371     public final MethodHandle thrower;
 372     private final Function<Object, T> cast;
 373     protected MethodHandle filter;
 374     private int fakeIdentityCount;
 375 
 376     private TestCase(Class<T> rtype, Function<Object, T> cast,
 377             ThrowMode throwMode, Throwable thrown)
 378             throws NoSuchMethodException, IllegalAccessException {
 379         this.cast = cast;
 380         filter = MethodHandles.lookup().findVirtual(
 381                 Function.class,
 382                 "apply",
 383                 MethodType.methodType(Object.class, Object.class))
 384                               .bindTo(cast);
 385         this.rtype = rtype;
 386         this.throwMode = throwMode;
 387         this.throwableClass = thrown.getClass();
 388         switch (throwMode) {
 389             case NOTHING:
 390                 this.thrown = null;
 391                 break;
 392             case ADAPTER:
 393             case UNCAUGHT:
 394                 this.thrown = new Error("do not catch this");
 395                 break;
 396             default:
 397                 this.thrown = thrown;
 398         }
 399 
 400         MethodHandle throwOrReturn = THROW_OR_RETURN;
 401         if (throwMode == ThrowMode.ADAPTER) {
 402             MethodHandle fakeIdentity = FAKE_IDENTITY.bindTo(this);
 403             for (int i = 0; i < 10; ++i) {
 404                 throwOrReturn = MethodHandles.filterReturnValue(
 405                         throwOrReturn, fakeIdentity);
 406             }
 407         }
 408         thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
 409     }
 410 
 411     private static Void noop(Object x) {
 412         return null;
 413     }
 414 
 415     private static <T2> PartialConstructor create(
 416             Class<T2> rtype, Function<Object, T2> cast) {
 417         return (t, u) -> () -> {
 418             try {
 419                 return new TestCase<>(rtype, cast, t, u);
 420             } catch (NoSuchMethodException | IllegalAccessException e) {
 421                 throw new Error(e);
 422             }
 423         };
 424     }
 425 
 426     private static <T extends Throwable>
 427     Object throwOrReturn(Object normal, T exception) throws T {
 428         if (exception != null) {
 429             Helper.called("throwOrReturn/throw", normal, exception);
 430             throw exception;
 431         }
 432         Helper.called("throwOrReturn/normal", normal, exception);
 433         return normal;
 434     }
 435 
 436     private static <T extends Throwable>
 437     Object catcher(Object o) {
 438         Helper.called("catcher", o);
 439         return o;
 440     }
 441 
 442     public MethodHandle filter(MethodHandle target) {
 443         return MethodHandles.filterReturnValue(target, filter);
 444     }
 445 
 446     public MethodHandle getCatcher(List<Class<?>> classes) {
 447         return MethodHandles.filterReturnValue(Helper.AS_LIST.asType(
 448                         MethodType.methodType(Object.class, classes)),
 449                 CATCHER
 450         );
 451     }
 452 
 453     @Override
 454     public String toString() {
 455         return "TestCase{" +
 456                 "rtype=" + rtype +
 457                 ", throwMode=" + throwMode +
 458                 ", throwableClass=" + throwableClass +
 459                 '}';
 460     }
 461 
 462     public String callName() {
 463         return "throwOrReturn/" +
 464                 (throwMode == ThrowMode.NOTHING
 465                         ? "normal"
 466                         : "throw");
 467     }
 468 
 469     public void assertReturn(Object returned, Object arg0, Object arg1,
 470             int catchDrops, Object... args) {
 471         int lag = 0;
 472         if (throwMode == ThrowMode.CAUGHT) {
 473             lag = 1;
 474         }
 475         Helper.assertCalled(lag, callName(), arg0, arg1);
 476 
 477         if (throwMode == ThrowMode.NOTHING) {
 478             assertEQ(cast.apply(arg0), returned);
 479         } else if (throwMode == ThrowMode.CAUGHT) {
 480             List<Object> catchArgs = new ArrayList<>(Arrays.asList(args));
 481             // catcher receives an initial subsequence of target arguments:
 482             catchArgs.subList(args.length - catchDrops, args.length).clear();
 483             // catcher also receives the exception, prepended:
 484             catchArgs.add(0, thrown);
 485             Helper.assertCalled("catcher", catchArgs);
 486             assertEQ(cast.apply(catchArgs), returned);
 487         }
 488         Asserts.assertEQ(0, fakeIdentityCount);
 489     }
 490 
 491     private void assertEQ(T t, Object returned) {
 492         if (rtype.isArray()) {
 493             Asserts.assertEQ(t.getClass(), returned.getClass());
 494             int n = Array.getLength(t);
 495             Asserts.assertEQ(n, Array.getLength(returned));
 496             for (int i = 0; i < n; ++i) {
 497                 Asserts.assertEQ(Array.get(t, i), Array.get(returned, i));
 498             }
 499         } else {
 500             Asserts.assertEQ(t, returned);
 501         }
 502     }
 503 
 504     private Object fakeIdentity(Object x) {
 505         System.out.println("should throw through this!");
 506         ++fakeIdentityCount;
 507         return x;
 508     }
 509 
 510     public void assertCatch(Throwable ex) {
 511         try {
 512             Asserts.assertSame(thrown, ex,
 513                     "must get the out-of-band exception");
 514         } catch (Throwable t) {
 515             ex.printStackTrace();
 516         }
 517     }
 518 
 519     public interface PartialConstructor
 520             extends BiFunction<ThrowMode, Throwable, Supplier<TestCase>> {
 521     }
 522 }