Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b299779

Browse files
smowtonigfoo
authored andcommitted
Create Files table entries for JAR/JRT files
1 parent 8e63d10 commit b299779

3 files changed

Lines changed: 46 additions & 9 deletions

File tree

java/kotlin-extractor/src/main/java/com/semmle/extractor/java/PopulateFile.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import com.github.codeql.Label;
88
import com.github.codeql.TrapWriter;
99
import com.github.codeql.KotlinExtractorDbSchemeKt;
10-
import com.semmle.util.trap.pathtransformers.PathTransformer;
10+
import com.semmle.util.exception.CatastrophicError;
1111
import com.semmle.util.files.FileUtil;
12+
import com.semmle.util.trap.pathtransformers.PathTransformer;
1213

1314
public class PopulateFile {
1415

@@ -101,4 +102,36 @@ private void populateParents(File normalisedFile, Label label) {
101102
KotlinExtractorDbSchemeKt.writeContainerparent(tw, parentLabel, label);
102103
}
103104

105+
public Label relativeFileId(File jarFile, String pathWithinJar) {
106+
if (pathWithinJar.contains("\\"))
107+
throw new CatastrophicError("Invalid jar path: '" + pathWithinJar + "' should not contain '\\'.");
108+
109+
Label jarFileId = this.populateFile(jarFile);
110+
Label jarFileLocation = tw.getLocation(jarFileId,0,0,0,0);
111+
KotlinExtractorDbSchemeKt.writeHasLocation(tw, jarFileId, jarFileLocation);
112+
113+
String databasePath = transformer.fileAsDatabaseString(jarFile);
114+
StringBuilder fullName = new StringBuilder(databasePath);
115+
String[] split = pathWithinJar.split("/");
116+
Label current = jarFileId;
117+
for (int i = 0; i < split.length; i++) {
118+
String shortName = split[i];
119+
120+
fullName.append("/");
121+
fullName.append(shortName);
122+
Label fileId = tw.getLabelFor("@\"" + fullName + ";jarFile" + "\"");
123+
124+
boolean file = i == split.length - 1;
125+
if (file) {
126+
KotlinExtractorDbSchemeKt.writeFiles(tw, fileId, fullName.toString());
127+
} else {
128+
KotlinExtractorDbSchemeKt.writeFolders(tw, fileId, fullName.toString());
129+
}
130+
KotlinExtractorDbSchemeKt.writeContainerparent(tw, current, fileId);
131+
current = fileId;
132+
}
133+
134+
return current;
135+
}
136+
104137
}

java/kotlin-extractor/src/main/kotlin/TrapWriter.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import org.jetbrains.kotlin.ir.declarations.path
88
import org.jetbrains.kotlin.ir.declarations.IrFile
99
import org.jetbrains.kotlin.ir.declarations.IrVariable
1010

11+
import com.semmle.extractor.java.PopulateFile
12+
1113
class TrapLabelManager {
1214
public var nextId: Int = 100
1315

@@ -23,6 +25,7 @@ open class TrapWriter (val lm: TrapLabelManager, val bw: BufferedWriter) {
2325
@Suppress("UNCHECKED_CAST")
2426
return lm.labelMapping.get(label) as Label<T>?
2527
}
28+
@JvmOverloads
2629
fun <T> getLabelFor(label: String, initialise: (Label<T>) -> Unit = {}): Label<T> {
2730
val maybeId: Label<T>? = getExistingLabelFor(label)
2831
if(maybeId == null) {
@@ -96,12 +99,14 @@ open class FileTrapWriter (
9699
val filePath: String,
97100
val sourceOffsetResolver: SourceOffsetResolver
98101
): TrapWriter (lm, bw) {
99-
val fileId = {
100-
val fileLabel = "@\"$filePath;sourcefile\""
101-
val id: Label<DbFile> = getLabelFor(fileLabel)
102-
writeFiles(id, filePath)
103-
id
104-
}()
102+
val populateFile = PopulateFile(this)
103+
val splitFilePath = filePath.split("!/")
104+
val fileId =
105+
(if(splitFilePath.size == 1)
106+
populateFile.populateFile(File(filePath))
107+
else
108+
populateFile.relativeFileId(File(splitFilePath.get(0)), splitFilePath.get(1))
109+
) as Label<DbFile>
105110

106111
fun getLocation(e: IrElement): Label<DbLocation> {
107112
return getLocation(e.startOffset, e.endOffset)

java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ fun getRawIrClassBinaryPath(irClass: IrClass): String? {
5050
}
5151

5252
fun getIrClassBinaryPath(irClass: IrClass): String {
53-
// If a class location is known, replace the JAR delimiter !/:
54-
return getRawIrClassBinaryPath(irClass)?.replaceFirst("!/", "/")
53+
return getRawIrClassBinaryPath(irClass)
5554
// Otherwise, make up a fake location:
5655
?: "/!unknown-binary-location/${getIrClassBinaryName(irClass).replace(".", "/")}.class"
5756
}

0 commit comments

Comments
 (0)