Skip to content

Commit 5d6779f

Browse files
author
Eric Caspole
committed
8292681: Add JMH for ProtectionDomain
Reviewed-by: mullan
1 parent 38e6706 commit 5d6779f

File tree

2 files changed

+336
-0
lines changed

2 files changed

+336
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package org.openjdk.bench.java.security;
25+
26+
import java.security.*;
27+
import java.net.*;
28+
import java.io.*;
29+
30+
import java.util.concurrent.TimeUnit;
31+
import org.openjdk.jmh.annotations.Benchmark;
32+
import org.openjdk.jmh.annotations.BenchmarkMode;
33+
import org.openjdk.jmh.annotations.Fork;
34+
import org.openjdk.jmh.annotations.Level;
35+
import org.openjdk.jmh.annotations.Measurement;
36+
import org.openjdk.jmh.annotations.Mode;
37+
import org.openjdk.jmh.annotations.OutputTimeUnit;
38+
import org.openjdk.jmh.annotations.Param;
39+
import org.openjdk.jmh.annotations.Scope;
40+
import org.openjdk.jmh.annotations.Setup;
41+
import org.openjdk.jmh.annotations.State;
42+
import org.openjdk.jmh.annotations.Warmup;
43+
44+
import org.openjdk.bench.util.InMemoryJavaCompiler;
45+
46+
@State(Scope.Thread)
47+
@OutputTimeUnit(TimeUnit.SECONDS)
48+
@Warmup(iterations = 5, time = 2)
49+
@Measurement(iterations = 5, time = 2)
50+
@BenchmarkMode(Mode.Throughput)
51+
public class ProtectionDomainBench {
52+
53+
@Param({"100"})
54+
public int numberOfClasses;
55+
56+
@Param({"10"})
57+
public int numberOfCodeSources;
58+
59+
static byte[][] compiledClasses;
60+
static Class[] loadedClasses;
61+
static String[] classNames;
62+
static int index = 0;
63+
static CodeSource[] cs;
64+
65+
static String B(int count) {
66+
return "public class B" + count + " {"
67+
+ " static int intField;"
68+
+ " public static void compiledMethod() { "
69+
+ " intField++;"
70+
+ " }"
71+
+ "}";
72+
}
73+
74+
@Setup(Level.Trial)
75+
public void setupClasses() throws Exception {
76+
compiledClasses = new byte[numberOfClasses][];
77+
loadedClasses = new Class[numberOfClasses];
78+
classNames = new String[numberOfClasses];
79+
cs = new CodeSource[numberOfCodeSources];
80+
81+
for (int i = 0; i < numberOfCodeSources; i++) {
82+
URL u = new URL("file:/tmp/duke" + i);
83+
cs[i] = new CodeSource(u, (java.security.cert.Certificate[]) null);
84+
}
85+
86+
for (int i = 0; i < numberOfClasses; i++) {
87+
classNames[i] = "B" + i;
88+
compiledClasses[i] = InMemoryJavaCompiler.compile(classNames[i], B(i));
89+
}
90+
91+
}
92+
93+
static class ProtectionDomainBenchLoader extends SecureClassLoader {
94+
95+
ProtectionDomainBenchLoader() {
96+
super();
97+
}
98+
99+
ProtectionDomainBenchLoader(ClassLoader parent) {
100+
super(parent);
101+
}
102+
103+
@Override
104+
protected Class<?> findClass(String name) throws ClassNotFoundException {
105+
if (name.equals(classNames[index] /* "B" + index */)) {
106+
assert compiledClasses[index] != null;
107+
return defineClass(name, compiledClasses[index] , 0, (compiledClasses[index]).length, cs[index % cs.length] );
108+
} else {
109+
return super.findClass(name);
110+
}
111+
}
112+
}
113+
114+
void work() throws ClassNotFoundException {
115+
ProtectionDomainBench.ProtectionDomainBenchLoader loader1 = new
116+
ProtectionDomainBench.ProtectionDomainBenchLoader();
117+
118+
for (index = 0; index < compiledClasses.length; index++) {
119+
Class c = loader1.findClass(classNames[index]);
120+
loadedClasses[index] = c;
121+
}
122+
}
123+
124+
@Benchmark
125+
@Fork(value = 3, jvmArgsPrepend={"-Djava.security.manager=allow"})
126+
public void withSecurityManager() throws ClassNotFoundException {
127+
work();
128+
}
129+
130+
@Benchmark
131+
@Fork(value = 3)
132+
public void noSecurityManager() throws ClassNotFoundException {
133+
work();
134+
}
135+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package org.openjdk.bench.util;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.OutputStream;
29+
30+
import java.net.URI;
31+
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.List;
34+
35+
import javax.tools.ForwardingJavaFileManager;
36+
import javax.tools.FileObject;
37+
import javax.tools.JavaCompiler;
38+
import javax.tools.JavaCompiler.CompilationTask;
39+
import javax.tools.JavaFileManager;
40+
import javax.tools.JavaFileObject;
41+
import javax.tools.JavaFileObject.Kind;
42+
import javax.tools.SimpleJavaFileObject;
43+
import javax.tools.StandardLocation;
44+
import javax.tools.ToolProvider;
45+
46+
/**
47+
* {@code InMemoryJavaCompiler} can be used for compiling a {@link
48+
* CharSequence} to a {@code byte[]}.
49+
*
50+
* The compiler will not use the file system at all, instead using a {@link
51+
* ByteArrayOutputStream} for storing the byte code. For the source code, any
52+
* kind of {@link CharSequence} can be used, e.g. {@link String}, {@link
53+
* StringBuffer} or {@link StringBuilder}.
54+
*
55+
* The {@code InMemoryCompiler} can easily be used together with a {@code
56+
* ByteClassLoader} to easily compile and load source code in a {@link String}:
57+
*
58+
* <pre>
59+
* {@code
60+
* import jdk.test.lib.compiler.InMemoryJavaCompiler;
61+
* import jdk.test.lib.ByteClassLoader;
62+
*
63+
* class Example {
64+
* public static void main(String[] args) {
65+
* String className = "Foo";
66+
* String sourceCode = "public class " + className + " {" +
67+
* " public void bar() {" +
68+
* " System.out.println("Hello from bar!");" +
69+
* " }" +
70+
* "}";
71+
* byte[] byteCode = InMemoryJavaCompiler.compile(className, sourceCode);
72+
* Class fooClass = ByteClassLoader.load(className, byteCode);
73+
* }
74+
* }
75+
* }
76+
* </pre>
77+
*/
78+
public class InMemoryJavaCompiler {
79+
private static class MemoryJavaFileObject extends SimpleJavaFileObject {
80+
private final String className;
81+
private final CharSequence sourceCode;
82+
private final ByteArrayOutputStream byteCode;
83+
84+
public MemoryJavaFileObject(String className, CharSequence sourceCode) {
85+
super(URI.create("string:///" + className.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
86+
this.className = className;
87+
this.sourceCode = sourceCode;
88+
this.byteCode = new ByteArrayOutputStream();
89+
}
90+
91+
@Override
92+
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
93+
return sourceCode;
94+
}
95+
96+
@Override
97+
public OutputStream openOutputStream() throws IOException {
98+
return byteCode;
99+
}
100+
101+
public byte[] getByteCode() {
102+
return byteCode.toByteArray();
103+
}
104+
105+
public String getClassName() {
106+
return className;
107+
}
108+
}
109+
110+
private static class FileManagerWrapper extends ForwardingJavaFileManager<JavaFileManager> {
111+
private static final Location PATCH_LOCATION = new Location() {
112+
@Override
113+
public String getName() {
114+
return "patch module location";
115+
}
116+
117+
@Override
118+
public boolean isOutputLocation() {
119+
return false;
120+
}
121+
};
122+
private final MemoryJavaFileObject file;
123+
private final String moduleOverride;
124+
125+
public FileManagerWrapper(MemoryJavaFileObject file, String moduleOverride) {
126+
super(getCompiler().getStandardFileManager(null, null, null));
127+
this.file = file;
128+
this.moduleOverride = moduleOverride;
129+
}
130+
131+
@Override
132+
public JavaFileObject getJavaFileForOutput(Location location, String className,
133+
Kind kind, FileObject sibling)
134+
throws IOException {
135+
if (!file.getClassName().equals(className)) {
136+
throw new IOException("Expected class with name " + file.getClassName() +
137+
", but got " + className);
138+
}
139+
return file;
140+
}
141+
142+
@Override
143+
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
144+
if (fo == file && moduleOverride != null) {
145+
return PATCH_LOCATION;
146+
}
147+
return super.getLocationForModule(location, fo);
148+
}
149+
150+
@Override
151+
public String inferModuleName(Location location) throws IOException {
152+
if (location == PATCH_LOCATION) {
153+
return moduleOverride;
154+
}
155+
return super.inferModuleName(location);
156+
}
157+
158+
@Override
159+
public boolean hasLocation(Location location) {
160+
return super.hasLocation(location) || location == StandardLocation.PATCH_MODULE_PATH;
161+
}
162+
163+
}
164+
165+
/**
166+
* Compiles the class with the given name and source code.
167+
*
168+
* @param className The name of the class
169+
* @param sourceCode The source code for the class with name {@code className}
170+
* @param options additional command line options
171+
* @throws RuntimeException if the compilation did not succeed
172+
* @return The resulting byte code from the compilation
173+
*/
174+
public static byte[] compile(String className, CharSequence sourceCode, String... options) {
175+
MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode);
176+
CompilationTask task = getCompilationTask(file, options);
177+
178+
if(!task.call()) {
179+
throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
180+
}
181+
182+
return file.getByteCode();
183+
}
184+
185+
private static JavaCompiler getCompiler() {
186+
return ToolProvider.getSystemJavaCompiler();
187+
}
188+
189+
private static CompilationTask getCompilationTask(MemoryJavaFileObject file, String... options) {
190+
List<String> opts = new ArrayList<>();
191+
String moduleOverride = null;
192+
for (String opt : options) {
193+
if (opt.startsWith("--patch-module=")) {
194+
moduleOverride = opt.substring("--patch-module=".length());
195+
} else {
196+
opts.add(opt);
197+
}
198+
}
199+
return getCompiler().getTask(null, new FileManagerWrapper(file, moduleOverride), null, opts, null, Arrays.asList(file));
200+
}
201+
}

0 commit comments

Comments
 (0)