Skip to content

Commit

Permalink
Fix race condition in javassist
Browse files Browse the repository at this point in the history
javassist fails to find a class when concurrently running process
compresses the class (converts classfile to raw bytes)

the idea of the fix is to make sure to only update rawClassfile and classfile
under lock in getClassFile3, all other places that modify classfile are
already synchronized

when reading the object state, we need to read under lock both classfile and
rawClassFile otherwise we might get an inconsistent state
  • Loading branch information
michalkurka committed Mar 17, 2021
1 parent a2e7bf5 commit 1226f44
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions h2o-core/src/main/java/javassist/CtClassType.java
Expand Up @@ -179,24 +179,37 @@ public ClassFile getClassFile2() {
}

public ClassFile getClassFile3(boolean doCompress) {
// quick path - no locking
ClassFile cfile = classfile;
if (cfile != null)
return cfile;

if (doCompress)
classPool.compress();

if (rawClassfile != null) {
byte[] rcfile;
synchronized (this) {
// repeat under lock to make sure we get a consistent result (classfile might have been set by another thread)
cfile = classfile;
if (cfile != null)
return cfile;

rcfile = rawClassfile;
}

if (rcfile != null) {
final ClassFile cf;
try {
ClassFile cf = new ClassFile(new DataInputStream(
new ByteArrayInputStream(rawClassfile)));
rawClassfile = null;
getCount = GET_THRESHOLD;
return setClassFile(cf);
cf = new ClassFile(new DataInputStream(new ByteArrayInputStream(rcfile)));
}
catch (IOException e) {
throw new RuntimeException(e.toString(), e);
}
getCount = GET_THRESHOLD;
synchronized (this) {
rawClassfile = null;
return setClassFile(cf);
}
}

InputStream fin = null;
Expand Down

0 comments on commit 1226f44

Please sign in to comment.