Move the sandbox code to net.corda

This commit is contained in:
Mike Hearn 2016-11-29 10:25:24 +00:00
parent 2f02e56893
commit 674e345ba2
24 changed files with 78 additions and 70 deletions

View File

@ -11,7 +11,7 @@ This code was written by Ben Evans.
# Roadmap
* Thorough code and security review.
* Thorough testing, code and security review.
* Possibly, a conversion to Kotlin.
* Make the instrumentation ahead of time only.
* Finalise the chosen subset of the Java platform to expose to contract code.

View File

@ -1,4 +1,4 @@
package com.r3cev;
package net.corda.sandbox;
import java.util.*;
@ -8,7 +8,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.r3cev.CandidateMethod.State.*;
import static net.corda.sandbox.CandidateMethod.State.*;
/**
* Represents the status of the candidacy of a particular set of candidate methods, i.e. Their progress from
@ -172,7 +172,7 @@ public class CandidacyStatus {
final Set<String> out = new HashSet<>();
for (final String candidateName : candidateMethods.keySet()) {
final CandidateMethod candidate = candidateMethods.get(candidateName);
if (candidate.getCurrentState() == DISALLOWED) {
if (candidate.getCurrentState() == CandidateMethod.State.DISALLOWED) {
out.add(candidateName);
}
}

View File

@ -1,4 +1,4 @@
package com.r3cev;
package net.corda.sandbox;
import java.lang.invoke.MethodType;
import java.util.HashSet;

View File

@ -1,6 +1,6 @@
package com.r3cev;
package net.corda.sandbox;
import static com.r3cev.Utils.*;
import static net.corda.sandbox.Utils.*;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

View File

@ -1,4 +1,4 @@
package com.r3cev;
package net.corda.sandbox;
import org.objectweb.asm.commons.Remapper;

View File

@ -1,6 +1,6 @@
package com.r3cev;
package net.corda.sandbox;
import com.r3cev.visitors.CostInstrumentingMethodVisitor;
import net.corda.sandbox.visitors.CostInstrumentingMethodVisitor;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

View File

@ -1,7 +1,7 @@
package com.r3cev;
package net.corda.sandbox;
import com.r3cev.visitors.CostInstrumentingMethodVisitor;
import com.r3cev.visitors.WhitelistCheckingClassVisitor;
import net.corda.sandbox.visitors.CostInstrumentingMethodVisitor;
import net.corda.sandbox.visitors.WhitelistCheckingClassVisitor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

View File

@ -1,4 +1,4 @@
package com.r3cev.costing;
package net.corda.sandbox.costing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,4 @@
package com.r3cev.costing;
package net.corda.sandbox.costing;
/**
* This interface is to decouple the actual executable code from the entry point and

View File

@ -1,4 +1,4 @@
package com.r3cev.costing;
package net.corda.sandbox.costing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -1,13 +1,15 @@
package com.r3cev.tools;
package net.corda.sandbox.tools;
import com.r3cev.WhitelistClassLoader;
import com.r3cev.visitors.SandboxPathVisitor;
import net.corda.sandbox.WhitelistClassLoader;
import net.corda.sandbox.visitors.SandboxPathVisitor;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.corda.sandbox.visitors.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import joptsimple.OptionParser;

View File

@ -1,6 +1,6 @@
package com.r3cev.visitors;
package net.corda.sandbox.visitors;
import com.r3cev.Utils;
import net.corda.sandbox.Utils;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -28,7 +28,7 @@ public final class CostInstrumentingMethodVisitor extends GeneratorAdapter {
public CostInstrumentingMethodVisitor(MethodVisitor methodVisitor, int access, String name, String desc) {
super(Opcodes.ASM5, methodVisitor, access, name, desc);
runtimeAccounterTypeName = "com/r3cev/costing/RuntimeCostAccounter";
runtimeAccounterTypeName = "net/corda/sandbox/costing/RuntimeCostAccounter";
// save other calling parameters as well...?
}

View File

@ -1,4 +1,4 @@
package com.r3cev.visitors;
package net.corda.sandbox.visitors;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

View File

@ -1,7 +1,7 @@
package com.r3cev.visitors;
package net.corda.sandbox.visitors;
import com.r3cev.Utils;
import com.r3cev.WhitelistClassLoader;
import net.corda.sandbox.Utils;
import net.corda.sandbox.WhitelistClassLoader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import org.slf4j.Logger;

View File

@ -1,19 +1,21 @@
package com.r3cev.visitors;
package net.corda.sandbox.visitors;
import com.r3cev.WhitelistClassLoader;
import com.r3cev.CandidacyStatus;
import net.corda.sandbox.WhitelistClassLoader;
import net.corda.sandbox.CandidacyStatus;
import java.util.Arrays;
import com.r3cev.CandidateMethod;
import com.r3cev.Utils;
import net.corda.sandbox.CandidateMethod;
import net.corda.sandbox.Utils;
import java.util.HashSet;
import java.util.Set;
import net.corda.sandbox.*;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.r3cev.CandidateMethod.State.*;
import static net.corda.sandbox.CandidateMethod.State.*;
import static org.objectweb.asm.Opcodes.*;
/**
@ -39,14 +41,14 @@ public final class WhitelistCheckingClassVisitor extends ClassVisitor {
super.visit(version, access, name, signature, superName, interfaces);
currentClassName = name;
if (resolveState(Utils.convertInternalFormToQualifiedClassName(superName)) == DISALLOWED) {
if (resolveState(Utils.convertInternalFormToQualifiedClassName(superName)) == CandidateMethod.State.DISALLOWED) {
candidacyStatus.setLoadable(false);
candidacyStatus.setReason("Superclass " + superName + " could not be loaded");
return;
}
for (final String interfaceName : interfaces) {
if (resolveState(Utils.convertInternalFormToQualifiedClassName(interfaceName)) == DISALLOWED) {
if (resolveState(Utils.convertInternalFormToQualifiedClassName(interfaceName)) == CandidateMethod.State.DISALLOWED) {
candidacyStatus.setLoadable(false);
candidacyStatus.setReason("Interface " + interfaceName + " could not be loaded");
return;
@ -137,7 +139,7 @@ public final class WhitelistCheckingClassVisitor extends ClassVisitor {
final String toLoadQualified = Utils.convertInternalMethodNameToQualifiedClassName(internalName);
if (!Utils.shouldAttemptToTransitivelyLoad(toLoadQualified)
|| resolveState(toLoadQualified) == DISALLOWED) {
|| resolveState(toLoadQualified) == CandidateMethod.State.DISALLOWED) {
referred.disallowed(internalName + " is DISALLOWED");
candidacyStatus.setLoadable(false);
candidacyStatus.setReason(candidateMethod.getReason());
@ -169,14 +171,14 @@ public final class WhitelistCheckingClassVisitor extends ClassVisitor {
clz = loader.loadClass(qualifiedClassname);
candidacyStatus.decRecursiveCount();
} catch (ClassNotFoundException ex) {
return DISALLOWED;
return CandidateMethod.State.DISALLOWED;
}
if (clz == null) {
LOGGER.error("Couldn't load: " + qualifiedClassname);
return DISALLOWED;
return CandidateMethod.State.DISALLOWED;
}
return DETERMINISTIC;
return CandidateMethod.State.DETERMINISTIC;
}
public CandidacyStatus getCandidacyStatus() {

View File

@ -1,14 +1,15 @@
package com.r3cev.visitors;
package net.corda.sandbox.visitors;
import com.r3cev.CandidacyStatus;
import com.r3cev.CandidateMethod;
import net.corda.sandbox.CandidacyStatus;
import net.corda.sandbox.CandidateMethod;
import net.corda.sandbox.*;
import org.objectweb.asm.Handle;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.r3cev.CandidateMethod.State.*;
import com.r3cev.Utils;
import static net.corda.sandbox.CandidateMethod.State.*;
import net.corda.sandbox.Utils;
import org.objectweb.asm.Label;
/**
@ -135,12 +136,12 @@ final class WhitelistCheckingMethodVisitor extends MethodVisitor {
@Override
public void visitEnd() {
// Start from the assumption that the method is deterministic, and try to disprove
CandidateMethod.State checkState = DETERMINISTIC;
CandidateMethod.State checkState = CandidateMethod.State.DETERMINISTIC;
final CandidateMethod candidateMethod = candidacyStatus.getCandidateMethod(currentMethodName);
if (candidateMethod == null) {
throw new IllegalArgumentException(currentMethodName + " not found in CandidacyStatus");
}
if (candidateMethod.getCurrentState() == DISALLOWED) {
if (candidateMethod.getCurrentState() == CandidateMethod.State.DISALLOWED) {
return;
}
@ -151,13 +152,13 @@ final class WhitelistCheckingMethodVisitor extends MethodVisitor {
case DETERMINISTIC:
break;
case MENTIONED:
checkState = MENTIONED;
checkState = CandidateMethod.State.MENTIONED;
break;
case DISALLOWED:
checkState = DISALLOWED;
checkState = CandidateMethod.State.DISALLOWED;
break CHECK;
case SCANNED:
checkState = MENTIONED;
checkState = CandidateMethod.State.MENTIONED;
if (referredMethod != candidateMethod)
throw new IllegalStateException("Illegal state of method " + referredMethod.getInternalMethodName() + " occurred when visiting method " + currentMethodName);
break;
@ -168,7 +169,7 @@ final class WhitelistCheckingMethodVisitor extends MethodVisitor {
candidateMethod.setCurrentState(checkState);
// If this methods state hasn't already been determined, it should be set to SCANNED
if (candidateMethod.getCurrentState() == MENTIONED)
if (candidateMethod.getCurrentState() == CandidateMethod.State.MENTIONED)
candidateMethod.scanned();
}
}

View File

@ -1,4 +1,4 @@
package sandbox.com.r3cev.costing;
package sandbox.net.corda.sandbox.costing;
/**
* A helper class that just forwards any static sandboxed calls to the real runtime
@ -10,23 +10,23 @@ package sandbox.com.r3cev.costing;
public class RuntimeCostAccounter {
public static void recordJump() {
com.r3cev.costing.RuntimeCostAccounter.recordJump();
net.corda.sandbox.costing.RuntimeCostAccounter.recordJump();
}
public static void recordAllocation(final String typeName) {
com.r3cev.costing.RuntimeCostAccounter.recordAllocation(typeName);
net.corda.sandbox.costing.RuntimeCostAccounter.recordAllocation(typeName);
}
public static void recordArrayAllocation(final int length, final int multiplier) {
com.r3cev.costing.RuntimeCostAccounter.recordArrayAllocation(length, multiplier);
net.corda.sandbox.costing.RuntimeCostAccounter.recordArrayAllocation(length, multiplier);
}
public static void recordMethodCall() {
com.r3cev.costing.RuntimeCostAccounter.recordMethodCall();
net.corda.sandbox.costing.RuntimeCostAccounter.recordMethodCall();
}
public static void recordThrow() {
com.r3cev.costing.RuntimeCostAccounter.recordThrow();
net.corda.sandbox.costing.RuntimeCostAccounter.recordThrow();
}
}

View File

@ -1,6 +1,6 @@
package com.r3cev;
package net.corda.sandbox;
import com.r3cev.CandidateMethod;
import net.corda.sandbox.CandidateMethod;
import org.junit.Test;
import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,4 @@
package com.r3cev;
package net.corda.sandbox;
class Constants {
public final static String INVALID_CLASS = "foobar";

View File

@ -1,6 +1,9 @@
package com.r3cev;
package net.corda.sandbox;
import net.corda.sandbox.costing.RuntimeCostAccounter;
import net.corda.sandbox.costing.*;
import org.junit.*;
import com.r3cev.costing.RuntimeCostAccounter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
@ -45,7 +48,7 @@ public class TestUtils {
}
public static void checkAllCosts(final int allocCost, final int jumpCost, final int invokeCost, final int throwCost) {
assertEquals(allocCost, RuntimeCostAccounter.getAllocationCost());
Assert.assertEquals(allocCost, RuntimeCostAccounter.getAllocationCost());
assertEquals(jumpCost, RuntimeCostAccounter.getJumpCost());
assertEquals(invokeCost, RuntimeCostAccounter.getInvokeCost());
assertEquals(throwCost, RuntimeCostAccounter.getThrowCost());

View File

@ -1,4 +1,4 @@
package com.r3cev;
package net.corda.sandbox;
import static org.junit.Assert.*;
import org.junit.Test;

View File

@ -1,7 +1,7 @@
package com.r3cev.costing;
package net.corda.sandbox.costing;
import com.r3cev.TestUtils;
import com.r3cev.WhitelistClassLoader;
import net.corda.sandbox.TestUtils;
import net.corda.sandbox.WhitelistClassLoader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

View File

@ -1,8 +1,8 @@
package com.r3cev.costing;
package net.corda.sandbox.costing;
import com.r3cev.TestUtils;
import static com.r3cev.TestUtils.*;
import com.r3cev.Utils;
import net.corda.sandbox.TestUtils;
import static net.corda.sandbox.TestUtils.*;
import net.corda.sandbox.Utils;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;