Author: mbenson Date: Tue Jul 30 17:54:58 2013 New Revision: 1508553 URL: http://svn.apache.org/r1508553 Log: promote logging to the WeaveEnvironment; create a new one for every Weaver/Cleaner invoked, with its very own logger
Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/InlineNestedPrivilegedCalls.java commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java (original) +++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java Tue Jul 30 17:54:58 2013 @@ -114,7 +114,7 @@ class ActionGenerator extends Privilizer init(); impl(); visitEnd(); - owner.privilizer().debug("Generated %s implementation %s to call %s#%s", actionInterface.getClassName(), + owner.privilizer().env.debug("Generated %s implementation %s to call %s#%s", actionInterface.getClassName(), action.getClassName(), owner.target.getClassName(), helper); return action; } @@ -125,7 +125,7 @@ class ActionGenerator extends Privilizer * (__privileged_): */ private void generateHelper() { - owner.privilizer().debug("Generating static helper method %s.%s to call %s", owner.target.getClassName(), + owner.privilizer().env.debug("Generating static helper method %s.%s to call %s", owner.target.getClassName(), helper, impl); final GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, helper, null, exceptions, owner); Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java (original) +++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java Tue Jul 30 17:54:58 2013 @@ -142,8 +142,7 @@ class BlueprintingVisitor extends Privil new StringBuilder(key.getLeft().getInternalName().replace('/', '_')).append("$$") .append(key.getRight().getName()).toString(); importedMethods.put(key, result); - - privilizer().debug("importing %s#%s as %s", key.getLeft().getClassName(), key.getRight(), result); + privilizer().env.debug("importing %s#%s as %s", key.getLeft().getClassName(), key.getRight(), result); final int access = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC; final MethodNode source = getMethods(key.getLeft()).get(key.getRight()); @@ -154,7 +153,17 @@ class BlueprintingVisitor extends Privil // non-public fields accessed final Set<FieldAccess> fieldAccesses = new LinkedHashSet<FieldAccess>(); - source.accept(new FieldAccessAccumulator(fieldAccesses)); + source.accept(new MethodVisitor(Opcodes.ASM4) { + @Override + public void visitFieldInsn(int opcode, String owner, String name, String desc) { + final FieldAccess fieldAccess = fieldAccess(Type.getObjectType(owner), name, Type.getType(desc)); + + super.visitFieldInsn(opcode, owner, name, desc); + if (!Modifier.isPublic(fieldAccess.access)) { + fieldAccesses.add(fieldAccess); + } + } + }); final MethodNode withAccessibleAdvice = new MethodNode(access, result, source.desc, source.signature, exceptions); @@ -227,11 +236,6 @@ class BlueprintingVisitor extends Privil @Override public void visitEnd() { super.visitEnd(); -// if (privilizer().verify) { -// final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); -// ((ClassNode) cv).accept(classWriter); -// privilizer().verify(target, classWriter.toByteArray()); -// } ((ClassNode) cv).accept(next); } @@ -287,26 +291,10 @@ class BlueprintingVisitor extends Privil } } - private class FieldAccessAccumulator extends MethodVisitor { - final Set<FieldAccess> fieldAccesses; - - FieldAccessAccumulator(Set<FieldAccess> fieldAccesses) { - super(Opcodes.ASM4); - this.fieldAccesses = fieldAccesses; - } - - @Override - public void visitFieldInsn(int opcode, String owner, String name, String desc) { - final FieldAccess fieldAccess = fieldAccess(Type.getObjectType(owner), name, Type.getType(desc)); - - super.visitFieldInsn(opcode, owner, name, desc); - if (!Modifier.isPublic(fieldAccess.access)) { - fieldAccesses.add(fieldAccess); - } - } - - } - + /** + * For every non-public referenced field of an imported method, replaces with reflective calls. Additionally, for + * every such field that is not accessible, sets the field's accessibility and clears it as the method exits. + */ private class AccessibleAdvisor extends AdviceAdapter { final Type bitSetType = Type.getType(BitSet.class); final Type classType = Type.getType(Class.class); @@ -396,8 +384,6 @@ class BlueprintingVisitor extends Privil final FieldAccess fieldAccess = fieldAccessMap.get(key); Validate.isTrue(fieldAccesses.contains(fieldAccess), "Cannot find field %s", key); final int fieldIndex = fieldAccesses.indexOf(fieldAccess); - - privilizer().debug("accessing field %s with opcode %s", fieldIndex, opcode); visitInsn(NOP); loadLocal(localFieldArray); push(fieldIndex); Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/InlineNestedPrivilegedCalls.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/InlineNestedPrivilegedCalls.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/InlineNestedPrivilegedCalls.java (original) +++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/InlineNestedPrivilegedCalls.java Tue Jul 30 17:54:58 2013 @@ -66,7 +66,8 @@ class InlineNestedPrivilegedCalls extend final Method m = new Method(name, desc); if (privilegedMethods.containsKey(m)) { name = privilegedMethods.get(m); - privilizer.debug("Inlining call from %s to %s as %s", outer, m, name); + privilizer.env.debug("Inlining call from %s to %s as %s", outer, m, + name); } } super.visitMethodInsn(opcode, owner, name, desc); Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java (original) +++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java Tue Jul 30 17:54:58 2013 @@ -26,7 +26,6 @@ import java.net.URLClassLoader; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.logging.Logger; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.BooleanUtils; @@ -102,50 +101,28 @@ public class Privilizer { public static final String CONFIG_VERIFY = CONFIG_WEAVER + "verify"; - private static final Logger log = Logger.getLogger(PrivilizerWeaver.class.getName()); - private static final String GENERATE_NAME = "__privileged_%s"; static final Type[] EMPTY_TYPE_ARRAY = new Type[0]; - public final AccessLevel accessLevel; - public final ClassLoader classLoader; - public final FileArchive fileArchive; - public final Policy policy; - public final boolean verify; + final WeaveEnvironment env; + final AccessLevel accessLevel; + final ClassLoader classLoader; + final FileArchive fileArchive; + final Policy policy; + final boolean verify; private final List<String> classpath; - final Set<Class<?>> woven; - public Privilizer(WeaveEnvironment weaveEnvironment) { + public Privilizer(WeaveEnvironment env) { super(); - this.policy = Policy.parse(weaveEnvironment.config.getProperty(CONFIG_POLICY)); - this.accessLevel = AccessLevel.parse(weaveEnvironment.config.getProperty(CONFIG_ACCESS_LEVEL)); - this.classpath = weaveEnvironment.classpath; - classLoader = new URLClassLoader(URLArray.fromPaths(weaveEnvironment.classpath)); - fileArchive = new FileArchive(classLoader, weaveEnvironment.target); - verify = BooleanUtils.toBoolean(weaveEnvironment.config.getProperty(CONFIG_VERIFY)); - woven = new HashSet<Class<?>>(); - } - - protected void debug(String message, Object... args) { - log.fine(String.format(message, args)); - } - - protected void verbose(String message, Object... args) { - log.fine(String.format(message, args)); - } - - protected void warn(String message, Object... args) { - log.warning(String.format(message, args)); - } - - protected void info(String message, Object... args) { - log.info(String.format(message, args)); - } - - protected void error(String message, Object... args) { - log.severe(String.format(message, args)); + this.env = env; + this.policy = Policy.parse(env.config.getProperty(CONFIG_POLICY)); + this.accessLevel = AccessLevel.parse(env.config.getProperty(CONFIG_ACCESS_LEVEL)); + this.classpath = env.classpath; + classLoader = new URLClassLoader(URLArray.fromPaths(env.classpath)); + fileArchive = new FileArchive(classLoader, env.target); + verify = BooleanUtils.toBoolean(env.config.getProperty(CONFIG_VERIFY)); } String generateName(String simple) { @@ -178,7 +155,8 @@ public class Privilizer { } void blueprint(final Class<?> type, final Privilizing privilizing) { - debug("blueprinting class %s %s", type.getName(), privilizing); + Object[] args = { type.getName(), privilizing }; + env.debug("blueprinting class %s %s", args); try { final ClassReader classReader = new ClassReader(fileArchive.getBytecode(type.getName())); @@ -194,7 +172,8 @@ public class Privilizer { } void privilize(final Class<?> type) { - debug("privilizing class %s", type.getName()); + Object[] args = { type.getName() }; + env.debug("privilizing class %s", args); try { final ClassReader classReader = new ClassReader(fileArchive.getBytecode(type.getName())); ClassVisitor cv; @@ -227,10 +206,10 @@ public class Privilizer { CheckClassAdapter.verify(reader, verifyClassLoader, false, new PrintWriter(w)); final String error = w.toString(); if (!error.isEmpty()) { - error(error); + env.error(error); final StringWriter trace = new StringWriter(); reader.accept(new TraceClassVisitor(new PrintWriter(trace)), ClassReader.SKIP_DEBUG); - debug(trace.toString()); + env.debug(trace.toString()); throw new IllegalStateException(); } Validate.validState(StringUtils.isBlank(error), error); Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java (original) +++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java Tue Jul 30 17:54:58 2013 @@ -99,8 +99,8 @@ class PrivilizingVisitor extends Privili + localAccessLevel + "')")); } if (AccessLevel.PACKAGE.compareTo(accessLevel) > 0) { - privilizer().warn("Possible security leak: granting privileges to %s method %s.%s", - localAccessLevel, className, m); + Object[] args = { localAccessLevel, className, m }; + privilizer().env.warn("Possible security leak: granting privileges to %s method %s.%s", args); } privilegedMethods.put(m, privilizer().generateName(name)); } Modified: commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java (original) +++ commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java Tue Jul 30 17:54:58 2013 @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.List; import java.util.Properties; import java.util.ServiceLoader; +import java.util.logging.Logger; import org.apache.commons.lang3.Validate; import org.apache.commons.weaver.model.WeaveEnvironment; @@ -65,12 +66,9 @@ public class CleanProcessor { /** * Create a new {@link CleanProcessor} instance. * - * @param classpath - * not {@code null} - * @param target - * not {@code null} - * @param configuration - * not {@code null} + * @param classpath not {@code null} + * @param target not {@code null} + * @param configuration not {@code null} */ public CleanProcessor(List<String> classpath, File target, Properties configuration) { super(); @@ -85,8 +83,9 @@ public class CleanProcessor { public void clean() { final ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(classpath)); final Finder finder = new Finder(new FileArchive(classLoader, target)); - final WeaveEnvironment env = new WeaveEnvironment(classpath, target, configuration); for (Cleaner cleaner : CLEANERS) { + final WeaveEnvironment env = + new WeaveEnvironment(classpath, target, configuration, Logger.getLogger(cleaner.getClass().getName())); cleaner.clean(env, finder); } } Modified: commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java (original) +++ commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java Tue Jul 30 17:54:58 2013 @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.List; import java.util.Properties; import java.util.ServiceLoader; +import java.util.logging.Logger; import org.apache.commons.lang3.Validate; import org.apache.commons.weaver.model.WeaveEnvironment; @@ -66,12 +67,9 @@ public class WeaveProcessor { /** * Create a new {@link WeaveProcessor} instance. * - * @param classpath - * not {@code null} - * @param target - * not {@code null} - * @param configuration - * not {@code null} + * @param classpath not {@code null} + * @param target not {@code null} + * @param configuration not {@code null} */ public WeaveProcessor(List<String> classpath, File target, Properties configuration) { super(); @@ -86,8 +84,9 @@ public class WeaveProcessor { public void weave() { final ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(classpath)); final Finder finder = new Finder(new FileArchive(classLoader, target)); - final WeaveEnvironment env = new WeaveEnvironment(classpath, target, configuration); for (Weaver weaver : WEAVERS) { + final WeaveEnvironment env = + new WeaveEnvironment(classpath, target, configuration, Logger.getLogger(weaver.getClass().getName())); weaver.process(env, finder); } } Modified: commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java?rev=1508553&r1=1508552&r2=1508553&view=diff ============================================================================== --- commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java (original) +++ commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java Tue Jul 30 17:54:58 2013 @@ -22,6 +22,7 @@ import java.io.File; import java.util.Collections; import java.util.List; import java.util.Properties; +import java.util.logging.Logger; import org.apache.commons.lang3.Validate; import org.apache.commons.weaver.spi.Cleaner; @@ -46,6 +47,8 @@ public class WeaveEnvironment { * "privilizer". */ public final Properties config; + + private final Logger log; /** * Create a new {@link WeaveEnvironment}. @@ -53,12 +56,34 @@ public class WeaveEnvironment { * @param classpath * @param target * @param config + * @param log */ - public WeaveEnvironment(List<String> classpath, File target, Properties config) { + public WeaveEnvironment(List<String> classpath, File target, Properties config, Logger log) { super(); this.classpath = Collections.unmodifiableList(Validate.notNull(classpath, "classpath")); this.target = Validate.notNull(target, "target"); this.config = (Properties) Validate.notNull(config, "config").clone(); + this.log = log; + } + + public void debug(String message, Object... args) { + log.fine(String.format(message, args)); + } + + public void verbose(String message, Object... args) { + log.fine(String.format(message, args)); + } + + public void warn(String message, Object... args) { + log.warning(String.format(message, args)); + } + + public void info(String message, Object... args) { + log.info(String.format(message, args)); + } + + public void error(String message, Object... args) { + log.severe(String.format(message, args)); } }