Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVASSIST-242: Demonstrates a race condition in DefineClassHelper + proposed fix #243

Merged
merged 2 commits into from Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/javassist/util/proxy/DefineClassHelper.java
Expand Up @@ -219,9 +219,6 @@ Class<?> defineClass(String name, byte[] b, int off, int len, Class<?> neighbor,
if (e instanceof RuntimeException) throw (RuntimeException) e;
throw new CannotCompileException(e);
}
finally {
SecurityActions.setAccessible(defineClass, false);
Copy link

@Pscheidl Pscheidl Jan 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's quote the reason I approved:

As other posters have indicated, setAccessible is only applicable to that instance, so setting the accessibility back to its original state is not needed.

}
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/javassist/util/proxy/DefinePackageHelper.java
Expand Up @@ -128,9 +128,6 @@ Package definePackage(ClassLoader loader, String name, String specTitle,
}
if (e instanceof RuntimeException) throw (RuntimeException) e;
}
finally {
definePackage.setAccessible(false);
}
return null;
}
};
Expand Down
75 changes: 75 additions & 0 deletions src/test/javassist/ConcurrentClassDefinitionTest.java
@@ -0,0 +1,75 @@
package javassist;

import javassist.bytecode.ClassFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class ConcurrentClassDefinitionTest {

@Parameterized.Parameter
public int N;

@Parameterized.Parameters
public static Object[] data() {
return new Object[] {
1, // single threaded - should pass
Runtime.getRuntime().availableProcessors() * 2
};
}

@Test
public void showDefineClassRaceCondition() throws Exception{
Worker[] workers = new Worker[N];
for (int i = 0; i < N; i++) {
workers[i] = new Worker(N + "_ " + i, 100);
workers[i].start();
}
for (Worker w : workers) {
w.join();
}
for (Worker w : workers) {
if (w.e != null) {
throw w.e;
}
}
}

private static class Worker extends Thread {
String id;
int count;
Exception e;

Worker(String id, int count) {
this.id = id;
this.count = count;
}

@Override
public void run() {
try {
for (int i = 0; i < count; i++) {
Class c = makeClass(id + "_" + i);
assert c != null;
}
} catch (Exception e) {
this.e = e;
}
}

@Override
public void interrupt() {
super.interrupt();
}
}

private static Class makeClass(String id) throws Exception {
ClassFile cf = new ClassFile(
false, "com.example.JavassistGeneratedClass_" + id, null);
ClassPool classPool = ClassPool.getDefault();
return classPool.makeClass(cf).toClass();
}


}