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

Skip to content

Commit 29f4eb9

Browse files
tamasvajkigfoo
authored andcommitted
Handle exceptions on file level
1 parent 468a911 commit 29f4eb9

3 files changed

Lines changed: 84 additions & 54 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ public class TrapFileManager implements AutoCloseable {
310310
private TrapDependencies trapDependenciesForClass;
311311
private File trapFile;
312312
private IrClass sym;
313+
private boolean hasError = false;
313314

314315
private TrapFileManager(File trapFile, String relative, boolean concurrentCreation, Logger log, IrClass sym) {
315316
trapDependenciesForClass = new TrapDependencies(relative);
@@ -326,6 +327,10 @@ public void addDependency(IrClass dep) {
326327
}
327328

328329
public void close() {
330+
if (hasError) {
331+
return;
332+
}
333+
329334
writeTrapDependencies(trapDependenciesForClass);
330335
// Record major/minor version information for extracted class files.
331336
// This is subsequently used to determine whether to re-extract (a newer version of) the same class.
@@ -346,6 +351,10 @@ private void writeTrapDependencies(TrapDependencies trapDependencies) {
346351
trapDependencies.save(
347352
currentSpecFileEntry.getTrapFolder().toPath().resolve(dep));
348353
}
354+
355+
public void closeWithoutAdditionalFiles() {
356+
hasError = true;
357+
}
349358
}
350359

351360
/*

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,39 @@ class ExternalClassExtractor(val logger: FileLogger, val invocationTrapFile: Str
2828
externalClassWorkList.clear()
2929
nextBatch.forEach { irClass ->
3030
output.getTrapLockerForClassFile(irClass).useAC { locker ->
31-
locker.getTrapFileManager().useAC { manager ->
31+
locker.trapFileManager.useAC { manager ->
3232
if(manager == null) {
3333
logger.info("Skipping extracting class ${irClass.name}")
3434
} else {
35-
GZIPOutputStream(manager.getFile().outputStream()).bufferedWriter().use { trapFileBW ->
36-
// We want our comments to be the first thing in the file,
37-
// so start off with a mere TrapWriter
38-
val tw = TrapWriter(TrapLabelManager(), trapFileBW)
39-
tw.writeComment("Generated by the CodeQL Kotlin extractor for external dependencies")
40-
tw.writeComment("Part of invocation $invocationTrapFile")
41-
// Now elevate to a SourceFileTrapWriter, and populate the
42-
// file information
43-
val binaryPath = getIrClassBinaryPath(irClass)
44-
val ftw = tw.makeFileTrapWriter(binaryPath, true)
35+
val trapFile = manager.file
36+
val binaryPath = getIrClassBinaryPath(irClass)
37+
try {
38+
GZIPOutputStream(trapFile.outputStream()).bufferedWriter().use { trapFileBW ->
39+
// We want our comments to be the first thing in the file,
40+
// so start off with a mere TrapWriter
41+
val tw = TrapWriter(TrapLabelManager(), trapFileBW)
42+
tw.writeComment("Generated by the CodeQL Kotlin extractor for external dependencies")
43+
tw.writeComment("Part of invocation $invocationTrapFile")
44+
// Now elevate to a SourceFileTrapWriter, and populate the
45+
// file information
46+
val ftw = tw.makeFileTrapWriter(binaryPath, true)
4547

46-
val fileExtractor = KotlinFileExtractor(logger, ftw, binaryPath, manager, this, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
48+
val fileExtractor = KotlinFileExtractor(logger, ftw, binaryPath, manager, this, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
4749

48-
// Populate a location and compilation-unit package for the file. This is similar to
49-
// the beginning of `KotlinFileExtractor.extractFileContents` but without an `IrFile`
50-
// to start from.
51-
val pkg = irClass.packageFqName?.asString() ?: ""
52-
val pkgId = fileExtractor.extractPackage(pkg)
53-
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
54-
ftw.writeCupackage(ftw.fileId, pkgId)
50+
// Populate a location and compilation-unit package for the file. This is similar to
51+
// the beginning of `KotlinFileExtractor.extractFileContents` but without an `IrFile`
52+
// to start from.
53+
val pkg = irClass.packageFqName?.asString() ?: ""
54+
val pkgId = fileExtractor.extractPackage(pkg)
55+
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
56+
ftw.writeCupackage(ftw.fileId, pkgId)
5557

56-
fileExtractor.extractClassSource(irClass)
58+
fileExtractor.extractClassSource(irClass)
59+
}
60+
} catch (e: Exception) {
61+
manager.closeWithoutAdditionalFiles()
62+
trapFile.delete()
63+
logger.error("Failed to extract '$binaryPath'", e)
5764
}
5865
}
5966
}

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.codeql
33
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
44
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
55
import org.jetbrains.kotlin.ir.declarations.*
6-
import org.jetbrains.kotlin.ir.util.*
76
import java.io.File
87
import java.io.FileOutputStream
98
import java.nio.file.Files
@@ -89,30 +88,33 @@ fun doFile(invocationTrapFile: String,
8988
fileTrapWriter: FileTrapWriter,
9089
checkTrapIdentical: Boolean,
9190
logCounter: LogCounter,
92-
trapDir: File,
93-
srcDir: File,
94-
file: IrFile,
91+
dbTrapDir: File,
92+
dbSrcDir: File,
93+
srcFile: IrFile,
9594
primitiveTypeMapping: PrimitiveTypeMapping,
9695
pluginContext: IrPluginContext,
9796
genericSpecialisationsExtracted: MutableSet<String>) {
98-
val filePath = file.path
97+
val srcFilePath = srcFile.path
9998
val logger = FileLogger(logCounter, fileTrapWriter)
100-
logger.info("Extracting file $filePath")
99+
logger.info("Extracting file $srcFilePath")
101100
logger.flush()
102-
val dest = Paths.get("$srcDir/${file.path}")
103-
val destDir = dest.getParent()
104-
Files.createDirectories(destDir)
105-
val srcTmpFile = File.createTempFile(dest.getFileName().toString() + ".", ".src.tmp", destDir.toFile())
106-
val srcTmpOS = FileOutputStream(srcTmpFile)
107-
Files.copy(Paths.get(file.path), srcTmpOS)
108-
srcTmpOS.close()
109-
srcTmpFile.renameTo(dest.toFile())
110101

111-
val trapFile = File("$trapDir/$filePath.trap")
112-
val trapFileDir = trapFile.getParentFile()
102+
val dbSrcFilePath = Paths.get("$dbSrcDir/$srcFilePath")
103+
val dbSrcDirPath = dbSrcFilePath.parent
104+
Files.createDirectories(dbSrcDirPath)
105+
val srcTmpFile = File.createTempFile(dbSrcFilePath.fileName.toString() + ".", ".src.tmp", dbSrcDirPath.toFile())
106+
srcTmpFile.outputStream().use {
107+
Files.copy(Paths.get(srcFilePath), it)
108+
}
109+
srcTmpFile.renameTo(dbSrcFilePath.toFile())
110+
111+
val trapFile = File("$dbTrapDir/$srcFilePath.trap")
112+
val trapFileDir = trapFile.parentFile
113113
trapFileDir.mkdirs()
114+
114115
if (checkTrapIdentical || !trapFile.exists()) {
115-
val trapTmpFile = File.createTempFile("$filePath.", ".trap.tmp", trapFileDir)
116+
val trapTmpFile = File.createTempFile("$srcFilePath.", ".trap.tmp", trapFileDir)
117+
var hasError = false
116118
trapTmpFile.bufferedWriter().use { trapFileBW ->
117119
// We want our comments to be the first thing in the file,
118120
// so start off with a mere TrapWriter
@@ -121,29 +123,41 @@ fun doFile(invocationTrapFile: String,
121123
tw.writeComment("Part of invocation $invocationTrapFile")
122124
// Now elevate to a SourceFileTrapWriter, and populate the
123125
// file information
124-
val sftw = tw.makeSourceFileTrapWriter(file, true)
125-
val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, file.path, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
126-
val fileExtractor = KotlinFileExtractor(logger, sftw, file.path, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
127-
fileExtractor.extractFileContents(file, sftw.fileId)
128-
externalClassExtractor.extractExternalClasses()
126+
val sftw = tw.makeSourceFileTrapWriter(srcFile, true)
127+
val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
128+
val fileExtractor = KotlinFileExtractor(logger, sftw, srcFilePath, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
129+
try {
130+
fileExtractor.extractFileContents(srcFile, sftw.fileId)
131+
externalClassExtractor.extractExternalClasses()
132+
} catch (e: Exception) {
133+
hasError = true
134+
logger.error("Failed to extract '$srcFilePath'", e)
135+
}
129136
}
130-
if (checkTrapIdentical && trapFile.exists()) {
131-
if(equivalentTrap(trapTmpFile, trapFile)) {
132-
if(!trapTmpFile.delete()) {
133-
logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile")
137+
138+
if (hasError) {
139+
if (!trapTmpFile.delete()) {
140+
logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile")
141+
}
142+
} else {
143+
if (checkTrapIdentical && trapFile.exists()) {
144+
if (equivalentTrap(trapTmpFile, trapFile)) {
145+
if (!trapTmpFile.delete()) {
146+
logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile")
147+
}
148+
} else {
149+
val trapDifferentFile = File.createTempFile("$srcFilePath.", ".trap.different", dbTrapDir)
150+
if (trapTmpFile.renameTo(trapDifferentFile)) {
151+
logger.warn(Severity.Warn, "TRAP difference: $trapFile vs $trapDifferentFile")
152+
} else {
153+
logger.warn(Severity.WarnLow, "Failed to rename $trapTmpFile to $trapFile")
154+
}
134155
}
135156
} else {
136-
val trapDifferentFile = File.createTempFile("$filePath.", ".trap.different", trapDir)
137-
if(trapTmpFile.renameTo(trapDifferentFile)) {
138-
logger.warn(Severity.Warn, "TRAP difference: $trapFile vs $trapDifferentFile")
139-
} else {
157+
if (!trapTmpFile.renameTo(trapFile)) {
140158
logger.warn(Severity.WarnLow, "Failed to rename $trapTmpFile to $trapFile")
141159
}
142160
}
143-
} else {
144-
if(!trapTmpFile.renameTo(trapFile)) {
145-
logger.warn(Severity.WarnLow, "Failed to rename $trapTmpFile to $trapFile")
146-
}
147161
}
148162
}
149163
}

0 commit comments

Comments
 (0)