-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3173587
commit b627bb2
Showing
4 changed files
with
71 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
44 changes: 44 additions & 0 deletions
44
src/main/java/ca/dmoj/java/DisallowedClassesClassFileTransformer.java
This file contains 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,44 @@ | ||
package ca.dmoj.java; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
|
||
public class DisallowedClassesClassFileTransformer implements ClassFileTransformer { | ||
protected String[] disallowedClassNames; | ||
protected RuntimeException[] disallowedClassExceptions; | ||
|
||
public DisallowedClassesClassFileTransformer(String[] disallowedClassNames, RuntimeException[] disallowedClassExceptions) { | ||
super(); | ||
assert disallowedClassNames.length == disallowedClassExceptions.length; | ||
this.disallowedClassNames = disallowedClassNames; | ||
this.disallowedClassExceptions = disallowedClassExceptions; | ||
} | ||
|
||
@Override | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, byte[] classfileBuffer) | ||
throws IllegalClassFormatException { | ||
if (className == null) return classfileBuffer; | ||
|
||
RuntimeException disallowed = null; | ||
// If the class ever loaded it's because a submission used it | ||
for (int i = 0; i < this.disallowedClassNames.length; ++i) { | ||
if (className.startsWith(this.disallowedClassNames[i])) { | ||
disallowed = this.disallowedClassExceptions[i]; | ||
break; | ||
} | ||
} | ||
|
||
if (disallowed != null) dumpExceptionAndExit(disallowed); | ||
|
||
// Don't actually retransform anything | ||
return classfileBuffer; | ||
} | ||
|
||
private static void dumpExceptionAndExit(Throwable exception) { | ||
System.err.print("7257b50d-e37a-4664-b1a5-b1340b4206c0: "); | ||
exception.printStackTrace(); | ||
System.exit(1); | ||
} | ||
} |
This file contains 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 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,4 @@ | ||
package ca.dmoj.java; | ||
|
||
public class UnsafeDisallowedException extends RuntimeException { | ||
} |