langtools/test/tools/javap/BadAttributeLength.java
author goetz
Wed, 27 Jan 2016 10:35:49 +0100
changeset 35775 00710c1b504a
parent 30846 2b3f379840f0
child 36526 3b41f1c69604
permissions -rw-r--r--
8149529: Adapt SAP copyrights to new company name in jdk repository Reviewed-by: simonis, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25847
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 25847
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
25847
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     4
 *
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     8
 *
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    13
 * accompanied this code).
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    14
 *
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    18
 *
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    21
 * questions.
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    22
 */
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    23
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    24
/*
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    25
 * @test
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    26
 * @bug 8047072
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    27
 * @summary javap OOM on fuzzed classfile
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    28
 * @modules jdk.jdeps
25847
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    29
 * @run main BadAttributeLength
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    30
 */
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    31
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    32
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    33
import java.io.*;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    34
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    35
public class BadAttributeLength {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    36
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    37
    public static String source = "public class Test {\n" +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    38
                                  "    public static void main(String[] args) {}\n" +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    39
                                  "}";
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    40
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    42
        final File sourceFile = new File("Test.java");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    43
        if (sourceFile.exists()) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    44
            if (!sourceFile.delete()) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    45
                throw new IOException("Can't override the Test.java file. " +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    46
                        "Check permissions.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    47
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    48
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    49
        try (FileWriter fw = new FileWriter(sourceFile)) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    50
            fw.write(source);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    51
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    52
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    53
        final String[] javacOpts = {"Test.java"};
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    54
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    55
        if (com.sun.tools.javac.Main.compile(javacOpts) != 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    56
            throw new Exception("Can't compile embedded test.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    57
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    58
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    59
        RandomAccessFile raf = new RandomAccessFile("Test.class", "rw");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    60
        long attPos = getFirstAttributePos(raf);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    61
        if (attPos < 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    62
            throw new Exception("The class file contains no attributes at all.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    63
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    64
        raf.seek(attPos + 2); // Jump to the attribute length
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    65
        raf.writeInt(Integer.MAX_VALUE - 1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    66
        raf.close();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    67
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    68
        String[] opts = { "-v", "Test.class" };
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    69
        StringWriter sw = new StringWriter();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    70
        PrintWriter pout = new PrintWriter(sw);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    71
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    72
        com.sun.tools.javap.Main.run(opts, pout);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    73
        pout.flush();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    74
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    75
        if (sw.getBuffer().indexOf("OutOfMemoryError") != -1) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    76
            throw new Exception("javap exited with OutOfMemoryError " +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    77
                    "instead of giving the proper error message.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    78
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    79
    }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    80
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    81
    private static long getFirstAttributePos(RandomAccessFile cfile) throws Exception {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    82
        cfile.seek(0);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    83
        int v1, v2;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    84
        v1 = cfile.readInt();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    85
        // System.out.println("Magic: " + String.format("%X", v1));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    86
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    87
        v1 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    88
        v2 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    89
        // System.out.println("Version: " + String.format("%d.%d", v1, v2));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    90
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    91
        v1 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    92
        // System.out.println("CPool size: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    93
        // Exhaust the constant pool
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    94
        for (; v1 > 1; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    95
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    96
            byte tag = cfile.readByte();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    97
            switch (tag) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    98
                case 7  : // Class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    99
                case 8  : // String
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   100
                    // Data is 2 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   101
                    cfile.skipBytes(2);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   102
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   103
                case 3  : // Integer
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   104
                case 4  : // Float
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   105
                case 9  : // FieldRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   106
                case 10 : // MethodRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   107
                case 11 : // InterfaceMethodRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   108
                case 12 : // Name and Type
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   109
                    // Data is 4 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   110
                    cfile.skipBytes(4);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   111
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   112
                case 5  : // Long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   113
                case 6  : // Double
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   114
                    // Data is 8 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   115
                    cfile.skipBytes(8);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   116
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   117
                case 1  : // Utf8
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   118
                    v2 = cfile.readUnsignedShort(); // Read buffer size
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   119
                    cfile.skipBytes(v2); // Skip buffer
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   120
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   121
                default :
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   122
                    throw new Exception("Unexpected tag in CPool: [" + tag + "] at "
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   123
                            + Long.toHexString(cfile.getFilePointer()));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   124
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   125
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   126
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   127
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   128
        cfile.skipBytes(6); // Access flags, this_class and super_class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   129
        v1 = cfile.readUnsignedShort(); // Number of interfaces
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   130
        // System.out.println("Interfaces: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   131
        cfile.skipBytes(3 * v1); // Each interface_info record is 3 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   132
        v1 = cfile.readUnsignedShort(); // Number of fields
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   133
        // System.out.println("Fields: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   134
        // Exhaust the fields table
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   135
        for (; v1 > 0; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   136
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   137
            cfile.skipBytes(6); // Skip access_flags, name_index and descriptor_index
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   138
            v2 = cfile.readUnsignedShort(); // Field attributes count
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   139
            if (v2 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   140
                // This field has some attributes - suits our needs
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   141
                // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   142
                return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   143
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   144
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   145
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   146
        v1 = cfile.readUnsignedShort(); // Number of methods
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   147
        // System.out.println("Methods: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   148
        // Exhaust the methods table
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   149
        for (; v1 > 0; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   150
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   151
            cfile.skipBytes(6); // Skip access_flags, name_index and descriptor_index
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   152
            v2 = cfile.readUnsignedShort(); // Method attributes count
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   153
            if (v2 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   154
                // This method got attributes - Ok with us,
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   155
                // return position of the first one
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   156
                // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   157
                return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   158
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   159
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   160
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   161
        // Class attributes section
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   162
        v1 = cfile.readUnsignedShort(); // Counts of attributes in class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   163
        if (v1 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   164
            // Class has some attributes, return position of the first one
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   165
            return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   166
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   167
        // Bummer! No attributes in the entire class file. Not fair!
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   168
        return -1L;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   169
    }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   170
}