Skip to content

Commit

Permalink
[JENKINS-18403] More reliable way of enforcing maximum bytecode versi…
Browse files Browse the repository at this point in the history
…on on a remoting channel.
  • Loading branch information
jglick committed Jul 10, 2013
1 parent f906bbf commit 09f20f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/hudson/remoting/Channel.java
Expand Up @@ -28,7 +28,6 @@
import hudson.remoting.ExportTable.ExportList;
import hudson.remoting.PipeWindow.Key;
import hudson.remoting.PipeWindow.Real;
import hudson.remoting.RemoteClassLoader.IClassLoader;
import hudson.remoting.forward.ListeningPort;
import hudson.remoting.forward.ForwarderFactory;
import hudson.remoting.forward.PortForwarder;
Expand Down Expand Up @@ -289,6 +288,8 @@ protected int[] initialValue() {

/*package*/ final JarLoaderImpl jarLoader;

short maximumBytecodeLevel = Short.MAX_VALUE;

/**
* Communication mode used in conjunction with {@link ClassicCommandTransport}.
*
Expand Down Expand Up @@ -860,6 +861,33 @@ public void setRestricted(boolean b) {
isRestricted = b;
}

/**
* Sets the maximum bytecode version (~ JDK) that we expect this channel to be able to load.
* If attempts are made to load remote classes using newer bytecode, they are immediately rejected,
* even if the remote JVM is actually new enough to load it.
* This helps maintain compatibility by making tests fail immediately without the need for an old JDK installation.
* By default, the remote class loader will try to load any bytecode version.
* @param level e.g. 5 for JDK 5 (the minimum sensible value)
* @since 2.29
*/
public void setMaximumBytecodeLevel(short level) throws IOException, InterruptedException {
if (level < 5) {
throw new IllegalArgumentException("Does not make sense to specify JDK 1.4 or below since remoting itself requires JDK 5+");
}
call(new SetMaximumBytecodeLevel(level));
}
private static final class SetMaximumBytecodeLevel implements Callable<Void,RuntimeException> {
private static final long serialVersionUID = 1;
private final short level;
SetMaximumBytecodeLevel(short level) {
this.level = level;
}
public Void call() throws RuntimeException {
Channel.current().maximumBytecodeLevel = level;
return null;
}
}

/**
* Waits for this {@link Channel} to be closed down, but only up the given milliseconds.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/hudson/remoting/RemoteClassLoader.java
Expand Up @@ -279,6 +279,14 @@ private static Object _getClassLoadingLock(RemoteClassLoader rcl, String name) {
static boolean TESTING;

private Class<?> loadClassFile(String name, byte[] bytes) {
if (bytes.length < 8) {
throw new ClassFormatError(name + " is <8 bytes long");
}
short bytecodeLevel = (short) ((bytes[6] << 8) + (bytes[7] & 0xFF) - 44);
if (bytecodeLevel > channel.maximumBytecodeLevel) {
throw new ClassFormatError("this channel is restricted to JDK 1." + channel.maximumBytecodeLevel + " compatibility but " + name + " was compiled for 1." + bytecodeLevel);
}

// if someone else is forcing us to load a class by giving as bytecode,
// discard our prefetched version since we'll never use them.
prefetchedClasses.remove(name);
Expand Down

0 comments on commit 09f20f1

Please sign in to comment.