Guest User

Untitled

a guest
May 6th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. Index: lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
  2. ===================================================================
  3. --- lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java (revision 1677928)
  4. +++ lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java (working copy)
  5. @@ -23,6 +23,7 @@
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.Modifier;
  8. import java.nio.charset.StandardCharsets;
  9. +import java.security.ProtectionDomain;
  10. import java.text.ParseException;
  11. import java.util.Arrays;
  12. import java.util.Collections;
  13. @@ -79,8 +80,8 @@
  14. super(parent);
  15. }
  16.  
  17. - public Class<? extends Expression> define(String className, byte[] bytecode) {
  18. - return defineClass(className, bytecode, 0, bytecode.length).asSubclass(Expression.class);
  19. + public Class<? extends Expression> define(String className, byte[] bytecode, ProtectionDomain domain) {
  20. + return defineClass(className, bytecode, 0, bytecode.length, domain).asSubclass(Expression.class);
  21. }
  22. }
  23.  
  24. @@ -123,10 +124,22 @@
  25. * @throws ParseException on failure to compile
  26. */
  27. public static Expression compile(String sourceText) throws ParseException {
  28. - return new JavascriptCompiler(sourceText).compileExpression(JavascriptCompiler.class.getClassLoader());
  29. + return compile(sourceText, null);
  30. }
  31.  
  32. /**
  33. + * Compiles the given expression.
  34. + *
  35. + * @param sourceText The expression to compile
  36. + * @param domain an optional {@code ProtectionDomain} for the loaded class.
  37. + * @return A new compiled expression
  38. + * @throws ParseException on failure to compile
  39. + */
  40. + public static Expression compile(String sourceText, ProtectionDomain domain) throws ParseException {
  41. + return new JavascriptCompiler(sourceText).compileExpression(JavascriptCompiler.class.getClassLoader(), domain);
  42. + }
  43. +
  44. + /**
  45. * Compiles the given expression with the supplied custom functions.
  46. * <p>
  47. * Functions must be {@code public static}, return {@code double} and
  48. @@ -140,6 +153,24 @@
  49. * @throws ParseException on failure to compile
  50. */
  51. public static Expression compile(String sourceText, Map<String,Method> functions, ClassLoader parent) throws ParseException {
  52. + return compile(sourceText, functions, parent, null);
  53. + }
  54. +
  55. + /**
  56. + * Compiles the given expression with the supplied custom functions.
  57. + * <p>
  58. + * Functions must be {@code public static}, return {@code double} and
  59. + * can take from zero to 256 {@code double} parameters.
  60. + *
  61. + * @param sourceText The expression to compile
  62. + * @param functions map of String names to functions
  63. + * @param parent a {@code ClassLoader} that should be used as the parent of the loaded class.
  64. + * It must contain all classes referred to by the given {@code functions}.
  65. + * @param domain an optional {@code ProtectionDomain} for the loaded class.
  66. + * @return A new compiled expression
  67. + * @throws ParseException on failure to compile
  68. + */
  69. + public static Expression compile(String sourceText, Map<String,Method> functions, ClassLoader parent, ProtectionDomain domain) throws ParseException {
  70. if (parent == null) {
  71. throw new NullPointerException("A parent ClassLoader must be given.");
  72. }
  73. @@ -146,7 +177,7 @@
  74. for (Method m : functions.values()) {
  75. checkFunction(m, parent);
  76. }
  77. - return new JavascriptCompiler(sourceText, functions).compileExpression(parent);
  78. + return new JavascriptCompiler(sourceText, functions).compileExpression(parent, domain);
  79. }
  80.  
  81. /**
  82. @@ -181,12 +212,12 @@
  83. }
  84.  
  85. /**
  86. - * Compiles the given expression with the specified parent classloader
  87. + * Compiles the given expression with the specified parent classloader and domain.
  88. *
  89. * @return A new compiled expression
  90. * @throws ParseException on failure to compile
  91. */
  92. - private Expression compileExpression(ClassLoader parent) throws ParseException {
  93. + private Expression compileExpression(ClassLoader parent, ProtectionDomain domain) throws ParseException {
  94. try {
  95. Tree antlrTree = getAntlrComputedExpressionTree();
  96.  
  97. @@ -195,7 +226,7 @@
  98. endCompile();
  99.  
  100. Class<? extends Expression> evaluatorClass = new Loader(parent)
  101. - .define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray());
  102. + .define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray(), domain);
  103. Constructor<? extends Expression> constructor = evaluatorClass.getConstructor(String.class, String[].class);
  104. return constructor.newInstance(sourceText, externalsMap.keySet().toArray(new String[externalsMap.size()]));
  105. } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
  106. Index: lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java
  107. ===================================================================
  108. --- lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java (revision 1677928)
  109. +++ lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java (working copy)
  110. @@ -16,6 +16,10 @@
  111. * limitations under the License.
  112. */
  113.  
  114. +import java.net.URL;
  115. +import java.security.CodeSigner;
  116. +import java.security.CodeSource;
  117. +import java.security.ProtectionDomain;
  118. import java.text.ParseException;
  119.  
  120. import org.apache.lucene.expressions.Expression;
  121. @@ -209,4 +213,10 @@
  122. x = JavascriptCompiler.compile("foo['\\\\'][\"\\\\\"]");
  123. assertEquals("foo['\\\\']['\\\\']", x.variables[0]);
  124. }
  125. +
  126. + public void testProtectionDomain() throws Exception {
  127. + ProtectionDomain fake = new ProtectionDomain(new CodeSource(new URL("file:/bogus"), (CodeSigner[])null), null);
  128. + Expression x = JavascriptCompiler.compile("foo + bar", fake);
  129. + assertSame(fake, x.getClass().getProtectionDomain());
  130. + }
  131. }
  132. Index: lucene/tools/junit4/tests.policy
  133. ===================================================================
  134. --- lucene/tools/junit4/tests.policy (revision 1677928)
  135. +++ lucene/tools/junit4/tests.policy (working copy)
  136. @@ -71,6 +71,8 @@
  137. permission java.lang.RuntimePermission "createClassLoader";
  138. // expressions TestCustomFunctions (only on older java8?)
  139. permission java.lang.RuntimePermission "getClassLoader";
  140. + // expressions TestJavaScriptCompiler
  141. + permission java.lang.RuntimePermission "getProtectionDomain";
  142. // needed to test unmap hack on platforms that support it
  143. permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
  144. // needed by jacoco to dump coverage
Advertisement
Add Comment
Please, Sign In to add comment