test/jdk/tools/jlink/plugins/StripDebugPluginTest.java
author mikael
Tue, 02 Oct 2018 22:36:08 -0700
changeset 51994 7577686cc9bd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211350: Remove jprt support Reviewed-by: dholmes, erikj, mchung, alanb, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test StripDebugPlugin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library ../../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 39834
diff changeset
    33
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @run main StripDebugPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import com.sun.tools.classfile.Attribute;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import com.sun.tools.classfile.ClassFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import com.sun.tools.classfile.Code_attribute;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import com.sun.tools.classfile.ConstantPoolException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import com.sun.tools.classfile.Method;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import java.util.Map;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    58
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    60
import jdk.tools.jlink.plugin.ResourcePoolEntry;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    61
import jdk.tools.jlink.plugin.ResourcePool;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 38320
diff changeset
    62
import jdk.tools.jlink.plugin.Plugin;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
public class StripDebugPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        new StripDebugPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        if (helper == null) {
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
    73
            // Skip test if the jmods directory is missing (e.g. exploded image)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            System.err.println("Test not run, NO jmods directory");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        Path moduleFile = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), "leaf1", classes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        Path moduleInfo = moduleFile.resolve("module-info.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        // Classes have been compiled in debug.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        List<Path> covered = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        byte[] infoContent = Files.readAllBytes(moduleInfo);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        try (Stream<Path> stream = Files.walk(moduleFile)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    byte[] content = Files.readAllBytes(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                    String path = "/" + helper.getJmodClassesDir().relativize(p).toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                    String moduleInfoPath = path + "/module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                    check(path, content, moduleInfoPath, infoContent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                    covered.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        if (covered.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            throw new AssertionError("No class to compress");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            System.err.println("removed debug attributes from "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                    + covered.size() + " classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        path = path.replace('\\', '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        StripDebugPlugin debug = new StripDebugPlugin();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        debug.configure(new HashMap<>());
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   110
        ResourcePoolEntry result1 = stripDebug(debug, ResourcePoolEntry.create(path,content), path, infoPath, moduleInfo);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        if (!path.endsWith("module-info.class")) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   113
            if (result1.contentLength() >= content.length) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                throw new AssertionError("Class size not reduced, debug info not "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                        + "removed for " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   117
            checkDebugAttributes(result1.contentBytes());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   120
        ResourcePoolEntry result2 = stripDebug(debug, result1, path, infoPath, moduleInfo);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   121
        if (result1.contentLength() != result2.contentLength()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            throw new AssertionError("removing debug info twice reduces class size of "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                    + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   125
        checkDebugAttributes(result1.contentBytes());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   128
    private ResourcePoolEntry stripDebug(Plugin debug, ResourcePoolEntry classResource,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            String path, String infoPath, byte[] moduleInfo) throws Exception {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   130
        ResourcePoolManager resources = new ResourcePoolManager();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        resources.add(classResource);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        if (!path.endsWith("module-info.class")) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   133
            ResourcePoolEntry res2 = ResourcePoolEntry.create(infoPath, moduleInfo);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            resources.add(res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   136
        ResourcePoolManager results = new ResourcePoolManager();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   137
        ResourcePool resPool = debug.transform(resources.resourcePool(),
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   138
                results.resourcePoolBuilder());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   139
        System.out.println(classResource.path());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   140
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   141
        return resPool.findEntry(classResource.path()).get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        String[] debugAttributes = new String[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                Attribute.LineNumberTable,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                Attribute.LocalVariableTable,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                Attribute.LocalVariableTypeTable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        for (Method method : classFile.methods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            String methodName = method.getName(classFile.constant_pool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            for (String attr : debugAttributes) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                if (code.attributes.get(attr) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                    throw new AssertionError("Debug attribute was not removed: " + attr +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                            " from method " + classFile.getName() + "#" + methodName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
}