Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin Engine crashes on types with missing dependencies even when error handlers are disabled. #1512

Closed
stiemannkj1 opened this issue Aug 27, 2023 · 2 comments
Assignees
Labels
Milestone

Comments

@stiemannkj1
Copy link
Contributor

To reproduce run the following test:

package net.bytebuddy.build;

import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import static org.junit.Assert.assertTrue;

public final class DoesNotCrashWhenTypeMissingDepsTest {
    @Rule
    public TemporaryFolder tempDir = new TemporaryFolder();

    @Test
    public void does_not_crash_when_type_is_missing_deps() throws IOException {

        File jar = tempDir.newFile("testInput.jar");

        try (
            FileOutputStream fileOutputStream = new FileOutputStream(jar);
            JarOutputStream jarBuilder = new JarOutputStream(fileOutputStream, new Manifest())
        ) {

            addDirs(jarBuilder, DoesNotCrashWhenTypeMissingDepsTest.class);

            // Add type with a required dependency, but don't add its dependency
            // so building/instrumenting the type fails.
            addClass(jarBuilder, TypeWithDep.class);
            addClass(jarBuilder, TypeToInstrument.class);
        }


        String containerTypeName = DoesNotCrashWhenTypeMissingDepsTest.class.getTypeName();

        Plugin plugin = new Plugin() {
            @Override
            public DynamicType.Builder<?> apply(
                DynamicType.Builder<?> builder,
                TypeDescription typeDescription,
                ClassFileLocator classFileLocator
            ) {
                return builder.visit(new AsmVisitorWrapper.ForDeclaredFields().field(target -> true));
            }

            @Override
            public boolean matches(TypeDescription target) {
                return (
                    (containerTypeName + "$" + TypeToInstrument.class.getSimpleName()).equals(target.getTypeName()) ||
                    (containerTypeName + "$" + TypeWithDep.class.getSimpleName()).equals(target.getTypeName())
                );
            }

            @Override
            public void close() {}
        };

        assertTrue(
            new Plugin.Engine.Default()
                .withoutErrorHandlers()
                .apply(
                    jar,
                    tempDir.newFile("testOutput.jar"),
                    Arrays.asList(() -> plugin)
                )
                .getFailed()
                .containsKey(
                    TypeDescription.ForLoadedType.of(TypeWithDep.class)));

    }

    public static final class TypeDep {}

    public static final class TypeWithDep {
        private static final TypeDep DEP = new TypeDep();
    }

    public static final class TypeToInstrument {}

    private static void addDirs(JarOutputStream jarBuilder, Class<?> aClass) throws IOException {
        StringBuilder pathBuilder = new StringBuilder();
        String filePath = getFilePath(aClass);
        String[] pathSegments = filePath.split("[" + File.separator + "]");

        for (int i = 0; i < pathSegments.length - 1; i++) {
            pathBuilder.append(pathSegments[i]);
            pathBuilder.append("/");
            jarBuilder.putNextEntry(new ZipEntry(pathBuilder.toString()));
            jarBuilder.closeEntry();
        }
    }

    private static void addClass(JarOutputStream jarBuilder, Class<?> aClass) throws IOException {
        String filePath = getFilePath(aClass);
        jarBuilder.putNextEntry(new ZipEntry(filePath));

        try (InputStream inputStream =
            DoesNotCrashWhenTypeMissingDepsTest.class.getClassLoader().getResourceAsStream(filePath)) {

            byte[] bytes = new byte[4096];
            int bytesRead;

            while ((bytesRead = inputStream.read(bytes, 0, bytes.length)) != -1) {
                jarBuilder.write(bytes, 0, bytesRead);
            }
        }

        jarBuilder.closeEntry();
    }

    private static String getFilePath(Class<?> aClass) {
        return aClass.getTypeName().replace(".", "/") + ".class";
    }
}

@raphw raphw closed this as completed in addd088 Aug 27, 2023
raphw added a commit that referenced this issue Aug 27, 2023
…n-types-missing-deps

Fixes: #1512 Plugin Engine crashes on types with missing dependencies even when error handlers are disabled.
@raphw
Copy link
Owner

raphw commented Aug 27, 2023

Good catch. Thanks for the merged contribution, just reworked the test a bit.

@raphw raphw self-assigned this Aug 27, 2023
@raphw raphw added the bug label Aug 27, 2023
@raphw raphw added this to the 1.14.6 milestone Aug 27, 2023
@stiemannkj1
Copy link
Contributor Author

Makes sense. Thanks for the quick merge @raphw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants