1 /* 2 * Copyright (c) 2016, 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.stubs; 26 27 import static org.graalvm.compiler.hotspot.stubs.StubUtil.printNumber; 28 import static org.graalvm.compiler.hotspot.stubs.StubUtil.printString; 29 30 import org.graalvm.compiler.api.replacements.Snippet; 31 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 32 import org.graalvm.compiler.debug.GraalError; 33 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 34 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 35 import org.graalvm.compiler.hotspot.nodes.AllocaNode; 36 import org.graalvm.compiler.options.OptionValues; 37 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 38 import org.graalvm.compiler.word.Word; 39 40 import jdk.vm.ci.code.Register; 41 42 /** 43 * Stub to allocate an {@link ArrayIndexOutOfBoundsException} thrown by a bytecode. 44 */ 45 public class OutOfBoundsExceptionStub extends CreateExceptionStub { 46 public OutOfBoundsExceptionStub(OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 47 super("createOutOfBoundsException", options, providers, linkage); 48 } 49 50 // JDK-8201593: Print array length in ArrayIndexOutOfBoundsException. 51 private static final boolean PRINT_LENGTH_IN_EXCEPTION = JavaVersionUtil.JAVA_SPECIFICATION_VERSION >= 11; 52 private static final int MAX_INT_STRING_SIZE = Integer.toString(Integer.MIN_VALUE).length(); 53 private static final String STR_INDEX = "Index "; 54 private static final String STR_OUTOFBOUNDSFORLENGTH = " out of bounds for length "; 55 56 @Override 57 protected Object getConstantParameterValue(int index, String name) { 58 switch (index) { 59 case 2: 60 return providers.getRegisters().getThreadRegister(); 61 case 3: 62 int wordSize = providers.getWordTypes().getWordKind().getByteCount(); 63 int bytes; 64 if (PRINT_LENGTH_IN_EXCEPTION) { 65 bytes = STR_INDEX.length() + STR_OUTOFBOUNDSFORLENGTH.length() + 2 * MAX_INT_STRING_SIZE; 66 } else { 67 bytes = MAX_INT_STRING_SIZE; 68 } 69 // (required words for maximum length + nullbyte), rounded up 70 return (bytes + 1) / wordSize + 1; 71 case 4: 72 return PRINT_LENGTH_IN_EXCEPTION; 73 default: 74 throw GraalError.shouldNotReachHere("unknown parameter " + name + " at index " + index); 75 } 76 } 77 78 @Snippet 79 private static Object createOutOfBoundsException(int idx, int length, @ConstantParameter Register threadRegister, @ConstantParameter int bufferSizeInWords, 80 @ConstantParameter boolean printLengthInException) { 81 Word buffer = AllocaNode.alloca(bufferSizeInWords); 82 Word ptr; 83 if (printLengthInException) { 84 ptr = printString(buffer, STR_INDEX); 85 ptr = printNumber(ptr, idx); 86 ptr = printString(ptr, STR_OUTOFBOUNDSFORLENGTH); 87 ptr = printNumber(ptr, length); 88 } else { 89 ptr = printNumber(buffer, idx); 90 } 91 ptr.writeByte(0, (byte) 0); 92 return createException(threadRegister, ArrayIndexOutOfBoundsException.class, buffer); 93 } 94 }