Search in sources :

Example 1 with PackageValidationResult

use of com.newrelic.weave.weavepackage.PackageValidationResult in project newrelic-java-agent by newrelic.

the class EnumNoNewFieldsTest method testEnumInvalidNewFields.

@Test
public void testEnumInvalidNewFields() throws IOException {
    List<byte[]> weaveBytes = new ArrayList<>();
    WeavePackageConfig config = WeavePackageConfig.builder().name("weave_unittest").source("com.newrelic.weave.weavepackage.testclasses").build();
    weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumNoNewFieldsTest$Day_Weave"));
    WeavePackage weavePackage = new WeavePackage(config, weaveBytes);
    ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumNoNewFieldsTest.class.getClassLoader()));
    PackageValidationResult result = weavePackage.validate(cache);
    WeaveTestUtils.expectViolations(result, new WeaveViolation(WeaveViolationType.ENUM_NEW_FIELD, "com/newrelic/weave/EnumNoNewFieldsTest$Day"));
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Test(org.junit.Test)

Example 2 with PackageValidationResult

use of com.newrelic.weave.weavepackage.PackageValidationResult in project newrelic-java-agent by newrelic.

the class WeavePackageVerifier method verify.

/**
 * Run validation using the weave package and classpath and return whether or not the weave package verified.
 *
 * @return whether or not the weave package verified
 */
private boolean verify() throws Exception {
    WeavePackage weavePackage = getWeavePackage(instrumentationJar);
    ClassLoader loader = createClassloaderForVerification(userJars);
    ClassCache cache = new ClassCache(new ClassLoaderFinder(loader));
    PackageValidationResult result = weavePackage.validate(cache);
    if (!result.succeeded()) {
        for (WeaveViolation violation : result.getViolations()) {
            out.println(violation.toString());
        }
    }
    return result.succeeded();
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult)

Example 3 with PackageValidationResult

use of com.newrelic.weave.weavepackage.PackageValidationResult in project newrelic-java-agent by newrelic.

the class EnumAccessTest method setup.

@Before
public void setup() throws IOException {
    WeavePackage weavePackage = makeWeavePackage();
    ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumAccessTest.class.getClassLoader()));
    PackageValidationResult result = weavePackage.validate(cache);
    WeaveTestUtils.expectViolations(result);
    WeaveTestUtils.loadUtilityClasses(EnumAccessTest.class.getClassLoader(), result.computeUtilityClassBytes(cache));
    byte[] stateCompositeBytes = result.weave("com/newrelic/weave/EnumAccessTest$Foo$State", new String[0], new String[0], WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$Foo$State"), cache, Collections.emptyMap()).getCompositeBytes(cache);
    Assert.assertNotNull(stateCompositeBytes);
    byte[] aCompositeBytes = result.weave("com/newrelic/weave/EnumAccessTest$Foo", new String[0], new String[0], WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$Foo"), cache, Collections.emptyMap()).getCompositeBytes(cache);
    Assert.assertNotNull(aCompositeBytes);
    WeaveTestUtils.addToContextClassloader("com.newrelic.weave.EnumAccessTest$Foo$State", stateCompositeBytes);
    WeaveTestUtils.addToContextClassloader("com.newrelic.weave.EnumAccessTest$Foo", aCompositeBytes);
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Before(org.junit.Before)

Example 4 with PackageValidationResult

use of com.newrelic.weave.weavepackage.PackageValidationResult in project newrelic-java-agent by newrelic.

the class ClassLoaderClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer, final InstrumentationContext context, Match match) throws IllegalClassFormatException {
    if (classloadersToSkip.contains(className)) {
        Agent.LOG.log(Level.FINEST, "ClassLoaderClassTransformer: classloadersToSkip contains {0}", className);
        return null;
    }
    // For any new ClassLoaders that we encounter, we should add them to the map
    if (!observedClassLoaders.containsKey(className)) {
        observedClassLoaders.put(className, classfileBuffer);
    }
    try {
        if (loader == null) {
            loader = BootstrapLoader.PLACEHOLDER;
        }
        Agent.LOG.log(Level.FINER, "ClassLoaderClassTransformer transforming: {0} -- {1}", loader, className);
        // This ClassCache will only consult the map of the observedClassLoaders that we pass in, so
        // we don't need to worry about it possibly using findResource() when we call validate(cache).
        ClassCache cache = new ClassCache(new ClassLoaderClassFinder(observedClassLoaders));
        PackageValidationResult result;
        if (className.equals("java/lang/ClassLoader")) {
            // For "java.lang.ClassLoader" we only want to instrument one of the loadClass() methods
            result = classloaderPackage.validate(cache);
        } else {
            // For every other ClassLoader that extends "java.lang.ClassLoader" we want to instrument both loadClass() methods
            result = classloaderBasePackage.validate(cache);
        }
        if (result.succeeded()) {
            Map<String, byte[]> utilityClassBytes = result.computeUtilityClassBytes(cache);
            if (utilityClassBytes != null && !utilityClassBytes.isEmpty()) {
                // Note: This is a duplication of code from the WeavePackageManager. This is duplicated here because we are
                // not using WeavePackageManager directly.
                NewClassAppender.appendClassesToBootstrapClassLoader(instrumentation, utilityClassBytes);
            }
            // Since we know that this class validated we are assured that this class extends
            // "java.lang.ClassLoader" so we can manually set the super class name to "java/lang/ClassLoader" or
            // to empty if it's the java.lang.ClassLoader instance itself.
            final String[] superNames;
            if (className.equals("java/lang/ClassLoader")) {
                superNames = new String[0];
            } else {
                superNames = new String[] { "java/lang/ClassLoader" };
            }
            Map<Method, Collection<String>> skipMethods = (context != null) ? context.getSkipMethods() : Collections.emptyMap();
            // This applies the "checkPackageAccess" weaved code only if it should be enabled from the constructor above
            byte[] newBytes = classfileBuffer;
            if (checkPackageAccessPackage != null && newBytes != null && className.equals("java/lang/ClassLoader")) {
                PackageValidationResult checkPackageAccessResult = checkPackageAccessPackage.validate(cache);
                if (checkPackageAccessResult.succeeded()) {
                    newBytes = checkPackageAccessResult.weave(className, superNames, new String[0], newBytes, cache, skipMethods).getCompositeBytes(cache);
                } else {
                    logClassLoaderWeaveViolations(checkPackageAccessResult, className);
                }
                if (newBytes == null) {
                    newBytes = classfileBuffer;
                }
            }
            PackageWeaveResult packageWeaveResult = result.weave(className, superNames, new String[0], newBytes, cache, skipMethods);
            // Do the weaving and use our "non-findResource" cache from above
            newBytes = packageWeaveResult.getCompositeBytes(cache);
            if (newBytes != null) {
                Agent.LOG.log(Level.FINE, "ClassLoaderClassTransformer patched {0} -- {1}", loader, className);
                return newBytes;
            }
        } else {
            logClassLoaderWeaveViolations(result, className);
        }
    } catch (Throwable t) {
        Agent.LOG.log(Level.FINE, t, "ClassLoaderClassTransformer unable to instrument classloader {0} -- {1}", loader, className);
    }
    Agent.LOG.log(Level.FINE, "ClassLoaderClassTransformer skipped transformation: {0} -- {1}", loader, className);
    return null;
}
Also used : ClassLoaderClassFinder(com.newrelic.agent.instrumentation.ClassLoaderClassFinder) PackageWeaveResult(com.newrelic.weave.weavepackage.PackageWeaveResult) Collection(java.util.Collection) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Method(org.objectweb.asm.commons.Method)

Aggregations

ClassCache (com.newrelic.weave.utils.ClassCache)4 PackageValidationResult (com.newrelic.weave.weavepackage.PackageValidationResult)4 ClassLoaderFinder (com.newrelic.weave.utils.ClassLoaderFinder)3 WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)3 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)2 ClassLoaderClassFinder (com.newrelic.agent.instrumentation.ClassLoaderClassFinder)1 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)1 PackageWeaveResult (com.newrelic.weave.weavepackage.PackageWeaveResult)1 WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Method (org.objectweb.asm.commons.Method)1