Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public final class ClassFile {
private static final String[] NO_ANNOTATIONS = {};

private static final int ACC_INTERFACE = 0x0200;
private static final int ACC_MODULE = 0x8000;

private static final byte[] JAVA_LANG_OBJECT_BYTES = JAVA_LANG_OBJECT.getBytes(US_ASCII);
private static final byte[] CONSTRUCTOR_BYTES = CONSTRUCTOR.getBytes(US_ASCII);
Expand Down Expand Up @@ -213,14 +212,17 @@ private static ClassHeader parse(byte[] bytecode, int offset, boolean onlyHeader
String superName;
if ((access & ACC_INTERFACE) != 0) {
superName = JAVA_LANG_OBJECT;
} else if (access != ACC_MODULE) {
int superNameOffset = cp[cp[u2(bytecode, cursor)]];
superName =
utfEquals(bytecode, superNameOffset, JAVA_LANG_OBJECT_BYTES)
? JAVA_LANG_OBJECT
: utf(bytecode, superNameOffset);
} else {
superName = null;
// java.lang.Object and modules have a zero super_class index (and therefore offset)
int superNameOffset = cp[cp[u2(bytecode, cursor)]];
if (superNameOffset != 0) {
superName =
utfEquals(bytecode, superNameOffset, JAVA_LANG_OBJECT_BYTES)
? JAVA_LANG_OBJECT
: utf(bytecode, superNameOffset);
} else {
superName = null;
}
}
cursor += 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ClassHeader {
/** Internal name of this class. */
public final String className;

/** Internal name of the super-class declared by this class; {@code null} for modules. */
/** Internal name of the super-class; {@code null} for {@code java.lang.Object} and modules. */
@Nullable public final String superName;

/** Internal names of the interfaces declared by this class. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import datadog.instrument.utils.JVM;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -16,6 +19,8 @@
import java.util.function.Consumer;
import java.util.jar.JarFile;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;

class ClassFileTest {

Expand Down Expand Up @@ -48,6 +53,41 @@ void header() {
testParsing("asm-test.jar", ClassFile::header);
}

@Test
void objectParsing() throws IOException {
try (InputStream in = Object.class.getResourceAsStream("Object.class")) {
assertNotNull(in);
byte[] bytecode = JVM.readAllBytes(in);
for (ClassHeader header :
new ClassHeader[] {ClassFile.header(bytecode), ClassFile.outline(bytecode)}) {
assertEquals("java/lang/Object", header.className);
assertNull(header.superName);
assertArrayEquals(new String[0], header.interfaces);
}
}
}

@Test
void moduleParsing() throws IOException {
byte[] bytecode;
try (JarFile sample = new JarFile("build/sampleBytecode/asm-test.jar");
InputStream in = sample.getInputStream(sample.getEntry("jdk9/module-info.class"))) {
bytecode = JVM.readAllBytes(in);
}
// also check a module with extra access flags, such as ACC_SYNTHETIC
byte[] syntheticBytecode = bytecode.clone();
int accessOffset = new ClassReader(syntheticBytecode).header;
syntheticBytecode[accessOffset] |= (byte) (Opcodes.ACC_SYNTHETIC >> 8);
for (byte[] moduleBytecode : new byte[][] {bytecode, syntheticBytecode}) {
for (ClassHeader header :
new ClassHeader[] {ClassFile.header(moduleBytecode), ClassFile.outline(moduleBytecode)}) {
assertEquals("module-info", header.className);
assertNull(header.superName);
assertArrayEquals(new String[0], header.interfaces);
}
}
}

@Test
void unicodeHeader() {
ClassHeader header = ClassFile.header(sampleUnicodeClass);
Expand Down
Loading