Skip to content

Commit

Permalink
Buggy 2nd attempt - crashes hotspot
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 15, 2024
1 parent d584d97 commit 5dd62ca
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,24 @@ public static int hashString(int stringHash) {
return stringHash | NON_ZERO;
}

public static Utf8Entry rawUtf8EntryFromStandardAttributeName(String name) {
//assuming standard attribute names are all US_ASCII
var raw = name.getBytes(StandardCharsets.US_ASCII);
return new Utf8EntryImpl(null, 0, raw, 0, raw.length);
static int hashClassFromUtf8(boolean isArray, Utf8EntryImpl content) {
int hash = content.contentHash();
return hashClassFromDescriptor(isArray ? hash : Util.descriptorStringHash(content.length(), hash));
}

static int hashClassFromDescriptor(int descriptorHash) {
return hash1(ClassFile.TAG_CLASS, descriptorHash);
}

static boolean isArrayDescriptor(Utf8EntryImpl cs) {
// Do not throw out-of-bounds for empty strings
return !cs.isEmpty() && cs.charAt(0) == '[';
}

@SuppressWarnings("unchecked")
public static <T extends PoolEntry> T maybeClone(ConstantPoolBuilder cp, T entry) {
if (cp.canWriteDirect(entry.constantPool()))
return entry;
return (T)((AbstractPoolEntry)entry).clone(cp);
}

Expand Down Expand Up @@ -147,7 +157,7 @@ enum State { RAW, BYTE, CHAR, STRING }
private final int offset;
private final int rawLen;
// Set in any state other than RAW
private @Stable int hash;
private @Stable int contentHash;
private @Stable int charLen;
// Set in CHAR state
private @Stable char[] chars;
Expand All @@ -166,18 +176,18 @@ enum State { RAW, BYTE, CHAR, STRING }
}

Utf8EntryImpl(ConstantPool cpm, int index, String s) {
this(cpm, index, s, hashString(s.hashCode()));
this(cpm, index, s, s.hashCode());
}

Utf8EntryImpl(ConstantPool cpm, int index, String s, int hash) {
Utf8EntryImpl(ConstantPool cpm, int index, String s, int contentHash) {
super(cpm, ClassFile.TAG_UTF8, index, 0);
this.rawBytes = null;
this.offset = 0;
this.rawLen = 0;
this.state = State.STRING;
this.stringValue = s;
this.charLen = s.length();
this.hash = hash;
this.contentHash = contentHash;
}

Utf8EntryImpl(ConstantPool cpm, int index, Utf8EntryImpl u) {
Expand All @@ -186,7 +196,7 @@ enum State { RAW, BYTE, CHAR, STRING }
this.offset = u.offset;
this.rawLen = u.rawLen;
this.state = u.state;
this.hash = u.hash;
this.contentHash = u.contentHash;
this.charLen = u.charLen;
this.chars = u.chars;
this.stringValue = u.stringValue;
Expand Down Expand Up @@ -232,7 +242,7 @@ private void inflate() {
int singleBytes = JLA.countPositives(rawBytes, offset, rawLen);
int hash = ArraysSupport.hashCodeOfUnsigned(rawBytes, offset, singleBytes, 0);
if (singleBytes == rawLen) {
this.hash = hashString(hash);
this.contentHash = hashString(hash);
charLen = rawLen;
state = State.BYTE;
}
Expand Down Expand Up @@ -290,7 +300,7 @@ private void inflate() {
throw new CpException("malformed input around byte " + px);
}
}
this.hash = hashString(hash);
this.contentHash = hashString(hash);
charLen = chararr_count;
this.chars = chararr;
state = State.CHAR;
Expand All @@ -300,18 +310,20 @@ private void inflate() {

@Override
public Utf8EntryImpl clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool))
return this;
return (state == State.STRING && rawBytes == null)
? (Utf8EntryImpl) cp.utf8Entry(stringValue)
: ((SplitConstantPool) cp).maybeCloneUtf8Entry(this);
}

@Override
public int hashCode() {
return hashString(contentHash());
}

int contentHash() {
if (state == State.RAW)
inflate();
return hash;
return contentHash;
}

@Override
Expand Down Expand Up @@ -422,7 +434,7 @@ public boolean equalsString(String s) {
case STRING:
return stringValue.equals(s);
case CHAR:
if (charLen != s.length() || hash != hashString(s.hashCode()))
if (charLen != s.length() || contentHash != s.hashCode())
return false;
for (int i=0; i<charLen; i++)
if (chars[i] != s.charAt(i))
Expand All @@ -431,7 +443,7 @@ public boolean equalsString(String s) {
state = State.STRING;
return true;
case BYTE:
if (rawLen != s.length() || hash != hashString(s.hashCode()))
if (rawLen != s.length() || contentHash != s.hashCode())
return false;
for (int i=0; i<rawLen; i++)
if (rawBytes[offset+i] != s.charAt(i))
Expand Down Expand Up @@ -544,21 +556,22 @@ public String asInternalName() {

public static final class ClassEntryImpl extends AbstractNamedEntry implements ClassEntry {

public ClassDesc sym = null;
public @Stable ClassDesc sym;
private @Stable int hash;

ClassEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) {
super(cpm, ClassFile.TAG_CLASS, index, name);
}

ClassEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name, int hash, ClassDesc sym) {
super(cpm, ClassFile.TAG_CLASS, index, name);
this.hash = hash;
this.sym = sym;
}

@Override
public ClassEntry clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool)) {
return this;
} else {
ClassEntryImpl ret = (ClassEntryImpl)cp.classEntry(ref1);
ret.sym = sym;
return ret;
}
return ((SplitConstantPool) cp).cloneClassEntry(this);
}

@Override
Expand All @@ -567,19 +580,42 @@ public ClassDesc asSymbol() {
if (sym != null) {
return sym;
}
return this.sym = Util.toClassDesc(asInternalName());

if (isArrayDescriptor(ref1)) {
sym = ref1.fieldTypeSymbol(); // array, symbol already available
} else {
sym = ClassDesc.ofInternalName(asInternalName()); // class or interface
}
return this.sym = sym;
}

@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o instanceof ClassEntryImpl cce) {
return cce.name().equals(this.name());
} else if (o instanceof ClassEntry c) {
return c.asSymbol().equals(this.asSymbol());
if (o instanceof ClassEntryImpl other) {
return equalsEntry(other);
}
return false;
}

boolean equalsEntry(ClassEntryImpl other) {
var tsym = this.sym;
var osym = other.sym;
if (tsym != null && osym != null) {
return tsym.equals(osym);
}

return ref1.equalsUtf8(other.ref1);
}

@Override
public int hashCode() {
var hash = this.hash;
if (hash != 0)
return hash;

return this.hash = hashClassFromUtf8(isArrayDescriptor(ref1), ref1);
}
}

public static final class PackageEntryImpl extends AbstractNamedEntry implements PackageEntry {
Expand All @@ -590,7 +626,7 @@ public static final class PackageEntryImpl extends AbstractNamedEntry implements

@Override
public PackageEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.packageEntry(ref1);
return cp.packageEntry(ref1);
}

@Override
Expand All @@ -616,7 +652,7 @@ public static final class ModuleEntryImpl extends AbstractNamedEntry implements

@Override
public ModuleEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.moduleEntry(ref1);
return cp.moduleEntry(ref1);
}

@Override
Expand Down Expand Up @@ -653,9 +689,6 @@ public Utf8Entry type() {

@Override
public NameAndTypeEntry clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool)) {
return this;
}
return cp.nameAndTypeEntry(ref1, ref2);
}

Expand Down Expand Up @@ -715,7 +748,7 @@ public static final class FieldRefEntryImpl extends AbstractMemberRefEntry imple

@Override
public FieldRefEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.fieldRefEntry(ref1, ref2);
return cp.fieldRefEntry(ref1, ref2);
}
}

Expand All @@ -728,7 +761,7 @@ public static final class MethodRefEntryImpl extends AbstractMemberRefEntry impl

@Override
public MethodRefEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.methodRefEntry(ref1, ref2);
return cp.methodRefEntry(ref1, ref2);
}
}

Expand All @@ -741,7 +774,7 @@ public static final class InterfaceMethodRefEntryImpl extends AbstractMemberRefE

@Override
public InterfaceMethodRefEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.interfaceMethodRefEntry(ref1, ref2);
return cp.interfaceMethodRefEntry(ref1, ref2);
}
}

Expand Down Expand Up @@ -832,7 +865,7 @@ public static final class InvokeDynamicEntryImpl

@Override
public InvokeDynamicEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.invokeDynamicEntry(bootstrap(), nameAndType());
return cp.invokeDynamicEntry(bootstrap(), nameAndType());
}
}

Expand All @@ -852,7 +885,7 @@ public static final class ConstantDynamicEntryImpl extends AbstractDynamicConsta

@Override
public ConstantDynamicEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.constantDynamicEntry(bootstrap(), nameAndType());
return cp.constantDynamicEntry(bootstrap(), nameAndType());
}
}

Expand Down Expand Up @@ -904,7 +937,7 @@ void writeTo(BufWriterImpl pool) {

@Override
public MethodHandleEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.methodHandleEntry(refKind, reference);
return cp.methodHandleEntry(refKind, reference);
}

@Override
Expand Down Expand Up @@ -939,9 +972,6 @@ public Utf8Entry descriptor() {

@Override
public MethodTypeEntry clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool)) {
return this;
}
return cp.methodTypeEntry(ref1);
}

Expand Down Expand Up @@ -985,7 +1015,7 @@ public ConstantDesc constantValue() {

@Override
public StringEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.stringEntry(ref1);
return cp.stringEntry(ref1);
}

@Override
Expand Down Expand Up @@ -1023,7 +1053,7 @@ void writeTo(BufWriterImpl pool) {

@Override
public IntegerEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.intEntry(val);
return cp.intEntry(val);
}

@Override
Expand Down Expand Up @@ -1064,7 +1094,7 @@ void writeTo(BufWriterImpl pool) {

@Override
public FloatEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.floatEntry(val);
return cp.floatEntry(val);
}

@Override
Expand Down Expand Up @@ -1104,7 +1134,7 @@ void writeTo(BufWriterImpl pool) {

@Override
public LongEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.longEntry(val);
return cp.longEntry(val);
}

@Override
Expand Down Expand Up @@ -1144,7 +1174,7 @@ void writeTo(BufWriterImpl pool) {

@Override
public DoubleEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.doubleEntry(val);
return cp.doubleEntry(val);
}

@Override
Expand Down
Loading

0 comments on commit 5dd62ca

Please sign in to comment.