-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Meta Lambda failures - make the code more resilient to class loader challenges #3095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
485bfd0
MAJOR WIP - investigating Meta Lambda failures
bbakerman e176512
Support for checking if access is possible fist
bbakerman 4437aaf
Support for checking if access is possible fist - now with log
bbakerman 8c0110b
No longer checking if access is possible fist - too hard to know
bbakerman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/groovy/graphql/schema/PropertyDataFetcherClassLoadingTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package graphql.schema | ||
|
|
||
| import graphql.GraphQLException | ||
| import graphql.Scalars | ||
| import spock.lang.Specification | ||
|
|
||
| class PropertyDataFetcherClassLoadingTest extends Specification { | ||
|
|
||
| GraphQLFieldDefinition fld(String fldName) { | ||
| return GraphQLFieldDefinition.newFieldDefinition().name(fldName).type(Scalars.GraphQLString).build() | ||
| } | ||
|
|
||
| static class BrokenClass { | ||
| static { | ||
| // this should prevent it from existing | ||
| throw new RuntimeException("No soup for you!") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
| } | ||
| } | ||
|
|
||
|
|
||
| static class TargetClass { | ||
|
|
||
| String getOkThings() { | ||
| return "ok" | ||
| } | ||
|
|
||
| BrokenClass getBrokenThings() { | ||
| return BrokenClass.cast(null) | ||
| } | ||
| } | ||
bbakerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def "can survive linkage errors during access to broken classes in Lambda support"() { | ||
| def okDF = PropertyDataFetcher.fetching("okThings") | ||
| def brokenDF = PropertyDataFetcher.fetching("brokenThings") | ||
|
|
||
| def target = new TargetClass() | ||
|
|
||
| when: | ||
| def value = okDF.get(fld("okThings"), target, { -> null }) | ||
| then: | ||
| value == "ok" | ||
|
|
||
| when: | ||
| brokenDF.get(fld("brokenThings"), target, { -> null }) | ||
| then: | ||
| // This is because the reflection method finder cant get to it | ||
| // but it has made it past the Meta Lambda support | ||
| thrown(GraphQLException) | ||
|
|
||
| // multiple times - same result | ||
| when: | ||
| value = okDF.get(fld("okThings"), target, { -> null }) | ||
| then: | ||
| value == "ok" | ||
|
|
||
| when: | ||
| brokenDF.get(fld("brokenThings"), target, { -> null }) | ||
| then: | ||
| thrown(GraphQLException) | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/test/groovy/graphql/util/javac/DynamicJavacSupport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package graphql.util.javac; | ||
|
|
||
| import javax.tools.DiagnosticCollector; | ||
| import javax.tools.FileObject; | ||
| import javax.tools.ForwardingJavaFileManager; | ||
| import javax.tools.JavaCompiler; | ||
| import javax.tools.JavaFileManager; | ||
| import javax.tools.JavaFileObject; | ||
| import javax.tools.SimpleJavaFileObject; | ||
| import javax.tools.StandardJavaFileManager; | ||
| import javax.tools.ToolProvider; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.URI; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
|
|
||
| /** | ||
| * This utility allows is to dynamically create Java classes and place them into | ||
| * floating class loaders. This will allow us to test class loader challenges | ||
| * <p> | ||
| * Proprs to https://www.baeldung.com/java-string-compile-execute-code where | ||
| * most of this code came from. | ||
| */ | ||
| public class DynamicJavacSupport { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this to allow is to generate classes in the tests - that are in seperate and diverse class loaders. Neato! |
||
|
|
||
| private final JavaCompiler compiler; | ||
| private final InMemoryFileManager manager; | ||
|
|
||
| public DynamicJavacSupport(ClassLoader parentClassLoader) { | ||
| compiler = ToolProvider.getSystemJavaCompiler(); | ||
| StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null); | ||
| manager = new InMemoryFileManager(parentClassLoader, standardFileManager); | ||
| } | ||
|
|
||
|
|
||
| public <T> Class<T> compile(String qualifiedClassName, String sourceCode) throws ClassNotFoundException { | ||
|
|
||
| List<JavaFileObject> sourceFiles = Collections.singletonList(new JavaSourceFromString(qualifiedClassName, sourceCode)); | ||
|
|
||
| DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); | ||
| JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, null, null, sourceFiles); | ||
|
|
||
| boolean result = task.call(); | ||
|
|
||
| if (!result) { | ||
| diagnostics.getDiagnostics() | ||
| .forEach(d -> System.out.printf("dyna-javac : %s\n", d)); | ||
| throw new IllegalStateException("Could not compile " + qualifiedClassName + " as a class"); | ||
| } else { | ||
| ClassLoader classLoader = manager.getClassLoader(null); | ||
| Class<?> clazz = classLoader.loadClass(qualifiedClassName); | ||
| return (Class<T>) clazz; | ||
| } | ||
| } | ||
|
|
||
| static class InMemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { | ||
| private final InMemoryClassLoader loader; | ||
| private final Map<String, JavaClassAsBytes> compiledClasses; | ||
|
|
||
| InMemoryFileManager(ClassLoader parentClassLoader, StandardJavaFileManager standardManager) { | ||
| super(standardManager); | ||
| this.compiledClasses = new ConcurrentHashMap<>(); | ||
| this.loader = new InMemoryClassLoader(parentClassLoader, this); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaFileObject getJavaFileForOutput(Location location, | ||
| String className, JavaFileObject.Kind kind, FileObject sibling) { | ||
|
|
||
| JavaClassAsBytes classAsBytes = new JavaClassAsBytes(className, kind); | ||
| compiledClasses.put(className, classAsBytes); | ||
|
|
||
| return classAsBytes; | ||
| } | ||
|
|
||
| public Map<String, JavaClassAsBytes> getBytesMap() { | ||
| return compiledClasses; | ||
| } | ||
|
|
||
| @Override | ||
| public ClassLoader getClassLoader(Location location) { | ||
| return loader; | ||
| } | ||
| } | ||
|
|
||
| static class InMemoryClassLoader extends ClassLoader { | ||
|
|
||
| private InMemoryFileManager manager; | ||
|
|
||
| InMemoryClassLoader(ClassLoader parentClassLoader, InMemoryFileManager manager) { | ||
| super(parentClassLoader); | ||
| this.manager = requireNonNull(manager, "manager must not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
|
|
||
| Map<String, JavaClassAsBytes> compiledClasses = manager.getBytesMap(); | ||
|
|
||
| if (compiledClasses.containsKey(name)) { | ||
| byte[] bytes = compiledClasses.get(name).getBytes(); | ||
| return defineClass(name, bytes, 0, bytes.length); | ||
| } else { | ||
| throw new ClassNotFoundException(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| static class JavaSourceFromString extends SimpleJavaFileObject { | ||
|
|
||
|
|
||
| private String sourceCode; | ||
|
|
||
| JavaSourceFromString(String name, String sourceCode) { | ||
| super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), | ||
| Kind.SOURCE); | ||
| this.sourceCode = requireNonNull(sourceCode, "sourceCode must not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public CharSequence getCharContent(boolean ignoreEncodingErrors) { | ||
| return sourceCode; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| static class JavaClassAsBytes extends SimpleJavaFileObject { | ||
|
|
||
|
|
||
| protected ByteArrayOutputStream bos = | ||
| new ByteArrayOutputStream(); | ||
|
|
||
| JavaClassAsBytes(String name, Kind kind) { | ||
| super(URI.create("string:///" + name.replace('.', '/') | ||
| + kind.extension), kind); | ||
| } | ||
|
|
||
| public byte[] getBytes() { | ||
| return bos.toByteArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public OutputStream openOutputStream() { | ||
| return bos; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.