test/langtools/tools/javac/diags/examples/BadConstantValueType/processors/CreateBadClassFile.java
author jlahoda
Tue, 13 Aug 2019 10:27:34 +0200
changeset 57725 ffc34eaf7b49
permissions -rw-r--r--
8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object Summary: Produce an error when reading a classfile with a field with ConstantValue with a wrong type. Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57725
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     1
/*
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     4
 *
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     8
 *
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    14
 *
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    18
 *
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    21
 * questions.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    22
 */
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    23
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    24
import java.io.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    25
import java.util.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    26
import javax.annotation.processing.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    27
import javax.lang.model.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    28
import javax.lang.model.element.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    29
import javax.tools.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    30
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    31
import com.sun.tools.classfile.*;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    32
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    33
import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    34
import com.sun.tools.classfile.ConstantPool.CPInfo;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    35
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    36
/* Create an invalid classfile with a static final field of type object with
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    37
 * ConstantValue of type String.
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    38
 */
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    39
@SupportedAnnotationTypes("*")
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    40
public class CreateBadClassFile extends AbstractProcessor {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    41
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    42
        if (++round == 1) {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    43
            ConstantPool cp = new ConstantPool(new CPInfo[] {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    44
                new CONSTANT_Utf8_info(""),                     //0
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    45
                new CONSTANT_Utf8_info("Test"),                 //1
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    46
                new CONSTANT_Class_info(null, 1),               //2
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    47
                new CONSTANT_Utf8_info("java/lang/Object"),     //3
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    48
                new CONSTANT_Class_info(null, 3),               //4
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    49
                new CONSTANT_Utf8_info("test"),                 //5
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    50
                new CONSTANT_Utf8_info("Ljava/lang/Object;"),   //6
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    51
                new CONSTANT_Utf8_info("ConstantValue"),        //7
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    52
            });
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    53
            ClassFile cf = new ClassFile(0xCAFEBABE,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    54
                          0,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    55
                          51,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    56
                          cp,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    57
                          new AccessFlags(AccessFlags.ACC_ABSTRACT |
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    58
                                          AccessFlags.ACC_INTERFACE |
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    59
                                          AccessFlags.ACC_PUBLIC),
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    60
                          2,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    61
                          4,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    62
                          new int[0],
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    63
                          new Field[] {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    64
                              new Field(new AccessFlags(AccessFlags.ACC_PUBLIC |
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    65
                                                        AccessFlags.ACC_STATIC |
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    66
                                                        AccessFlags.ACC_FINAL),
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    67
                                        5,
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    68
                                      new Descriptor(6),
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    69
                                      new Attributes(cp, new Attribute[] {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    70
                                          new ConstantValue_attribute(7, 6)
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    71
                                      }))
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    72
                          },
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    73
                          new Method[0],
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    74
                          new Attributes(cp, new Attribute[0]));
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    75
            try {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    76
                JavaFileObject clazz = processingEnv.getFiler().createClassFile("Test");
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    77
                try (OutputStream out = clazz.openOutputStream()) {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    78
                    new ClassWriter().write(cf, out);
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    79
                }
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    80
            } catch (IOException ex) {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    81
                ex.printStackTrace();
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    82
            }
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    83
        }
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    84
        return false;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    85
    }
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    86
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    87
    public SourceVersion getSupportedSourceVersion() {
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    88
        return SourceVersion.latest();
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    89
    }
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    90
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    91
    int round = 0;
ffc34eaf7b49 8228502: javac crashed on a broken classfile with ConstantValue attribute on a field of type Object
jlahoda
parents:
diff changeset
    92
}