langtools/test/tools/javap/BadAttributeLength.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 25847 90e43c49e318
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25847
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     1
/*
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    28
 * @run main BadAttributeLength
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    29
 */
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
import java.io.*;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    33
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    34
public class BadAttributeLength {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    35
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    36
    public static String source = "public class Test {\n" +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    37
                                  "    public static void main(String[] args) {}\n" +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    38
                                  "}";
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    39
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    41
        final File sourceFile = new File("Test.java");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    42
        if (sourceFile.exists()) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    43
            if (!sourceFile.delete()) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    44
                throw new IOException("Can't override the Test.java file. " +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    45
                        "Check permissions.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    46
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    47
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    48
        try (FileWriter fw = new FileWriter(sourceFile)) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    49
            fw.write(source);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    50
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    51
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    52
        final String[] javacOpts = {"Test.java"};
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    53
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    54
        if (com.sun.tools.javac.Main.compile(javacOpts) != 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    55
            throw new Exception("Can't compile embedded test.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    56
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    57
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    58
        RandomAccessFile raf = new RandomAccessFile("Test.class", "rw");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    59
        long attPos = getFirstAttributePos(raf);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    60
        if (attPos < 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    61
            throw new Exception("The class file contains no attributes at all.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    62
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    63
        raf.seek(attPos + 2); // Jump to the attribute length
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    64
        raf.writeInt(Integer.MAX_VALUE - 1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    65
        raf.close();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    66
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    67
        String[] opts = { "-v", "Test.class" };
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    68
        StringWriter sw = new StringWriter();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    69
        PrintWriter pout = new PrintWriter(sw);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    70
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    71
        com.sun.tools.javap.Main.run(opts, pout);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    72
        pout.flush();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    73
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    74
        if (sw.getBuffer().indexOf("OutOfMemoryError") != -1) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    75
            throw new Exception("javap exited with OutOfMemoryError " +
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    76
                    "instead of giving the proper error message.");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    77
        }
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
    private static long getFirstAttributePos(RandomAccessFile cfile) throws Exception {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    81
        cfile.seek(0);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    82
        int v1, v2;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    83
        v1 = cfile.readInt();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    84
        // System.out.println("Magic: " + String.format("%X", v1));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    85
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    86
        v1 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    87
        v2 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    88
        // System.out.println("Version: " + String.format("%d.%d", v1, v2));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    89
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    90
        v1 = cfile.readUnsignedShort();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    91
        // System.out.println("CPool size: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    92
        // Exhaust the constant pool
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    93
        for (; v1 > 1; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    94
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    95
            byte tag = cfile.readByte();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    96
            switch (tag) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    97
                case 7  : // Class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    98
                case 8  : // String
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
    99
                    // Data is 2 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   100
                    cfile.skipBytes(2);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   101
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   102
                case 3  : // Integer
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   103
                case 4  : // Float
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   104
                case 9  : // FieldRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   105
                case 10 : // MethodRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   106
                case 11 : // InterfaceMethodRef
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   107
                case 12 : // Name and Type
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   108
                    // Data is 4 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   109
                    cfile.skipBytes(4);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   110
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   111
                case 5  : // Long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   112
                case 6  : // Double
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   113
                    // Data is 8 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   114
                    cfile.skipBytes(8);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   115
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   116
                case 1  : // Utf8
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   117
                    v2 = cfile.readUnsignedShort(); // Read buffer size
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   118
                    cfile.skipBytes(v2); // Skip buffer
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   119
                    break;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   120
                default :
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   121
                    throw new Exception("Unexpected tag in CPool: [" + tag + "] at "
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   122
                            + Long.toHexString(cfile.getFilePointer()));
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   123
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   124
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   125
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   126
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   127
        cfile.skipBytes(6); // Access flags, this_class and super_class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   128
        v1 = cfile.readUnsignedShort(); // Number of interfaces
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   129
        // System.out.println("Interfaces: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   130
        cfile.skipBytes(3 * v1); // Each interface_info record is 3 bytes long
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   131
        v1 = cfile.readUnsignedShort(); // Number of fields
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   132
        // System.out.println("Fields: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   133
        // Exhaust the fields table
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   134
        for (; v1 > 0; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   135
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   136
            cfile.skipBytes(6); // Skip access_flags, name_index and descriptor_index
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   137
            v2 = cfile.readUnsignedShort(); // Field attributes count
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   138
            if (v2 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   139
                // This field has some attributes - suits our needs
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   140
                // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   141
                return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   142
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   143
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   144
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   145
        v1 = cfile.readUnsignedShort(); // Number of methods
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   146
        // System.out.println("Methods: " + v1);
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   147
        // Exhaust the methods table
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   148
        for (; v1 > 0; v1--) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   149
            // System.out.print(".");
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   150
            cfile.skipBytes(6); // Skip access_flags, name_index and descriptor_index
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   151
            v2 = cfile.readUnsignedShort(); // Method attributes count
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   152
            if (v2 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   153
                // This method got attributes - Ok with us,
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   154
                // return position of the first one
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   155
                // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   156
                return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   157
            }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   158
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   159
        // System.out.println();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   160
        // Class attributes section
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   161
        v1 = cfile.readUnsignedShort(); // Counts of attributes in class
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   162
        if (v1 > 0) {
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   163
            // Class has some attributes, return position of the first one
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   164
            return cfile.getFilePointer();
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   165
        }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   166
        // Bummer! No attributes in the entire class file. Not fair!
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   167
        return -1L;
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   168
    }
90e43c49e318 8047072: javap OOM on fuzzed classfile
kizune
parents:
diff changeset
   169
}