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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ Research artifacts and reproducible probes are indexed by topic in [docs/researc
- Java synthetic enum-switch holders and Kotlin callable references are classified separately from source anonymous objects. Library-inline implementation classes require verified compiler output, synthetic metadata and a verified enclosing declaration; their raw JVM identities remain significant. If provenance fails, graph analysis continues with raw identities and explicitly unverified diff observations, while the verdict remains FAIL and dependent obligations remain unproved. See [origin boundaries](docs/design.md#104-匿名类与图的稳定关联).
- JDK declarations are resolved from the running JDK image, including `jdk.compiler`, without searching GraphFold’s application classpath. This resolves declarations only; it does not establish safe effects for arbitrary JDK calls. Frontend proof decisions consume structured completeness from the final folded graph and reconcile the method receipts delivered by the same `loadObserved` invocation. Missing bodies, failed observations and incomplete dispatch cannot become evidence of no effects. Application string literals are not method-completeness records. Selection uses Graphite’s parsed Cypher AST: certified direct CallSite-property predicates can tolerate unrelated native/dispatch gaps, while other query shapes conservatively require complete graph evidence. Incomplete body extraction still blocks; selecting only known targets of incomplete dispatch cannot broaden the fold.
- Native Gradle and Maven plugins consume packaged outputs from their current build models. There is no standalone CLI or plain-source build adapter. Direct javac/kotlinc fixture compilers are test-only. Custom compiler plugins, generators, Kotlin Multiplatform, and dependency arrangements require separate validation.
- Build-system analysis reads application classes from final packaged applications. Gradle application JARs use their build-declared ordered dependency archives; explicitly selected JARs also include local manifest `Class-Path` entries. Compiler outputs supply source provenance only and never add classes missing from the package. Bytecode replaced during packaging stays replaced in the graph; it cannot inherit compiler provenance without verified byte identity. Multi-release JARs select one view using the actual build JDK recorded by the integration, independently for BASE and HEAD; the analyzer host JVM does not choose a version. Conflicting class definitions require explicit input evidence rather than inferred launcher behavior. Resources and service registrations come from packaged archives.
- Build-system analysis reads application classes from final packaged applications. Gradle application JARs use their build-declared ordered dependency archives; explicitly selected JARs also include local manifest `Class-Path` entries. Compiler outputs supply source provenance only and never add classes missing from the package. Bytecode replaced during packaging stays replaced in the graph; it cannot inherit compiler provenance without verified byte identity. Multi-release JARs select one view using the actual build JDK recorded by the integration, independently for BASE and HEAD; the analyzer host JVM does not choose a version. An integration’s ordered classpath or a complete packaged Spring Boot `BOOT-INF/classpath.idx` determines which definition enters the graph. Without order evidence, different definitions remain unresolved static input; an index is unnecessary for unambiguous archives. All packaged class bytes, annotations and resources remain in the change inventory, including definitions outside the effective graph. Resources and service registrations come from packaged archives.
- Each selected archive defines a separate static analysis scope. Library-only Gradle builds use production JARs declared by `runtimeElements`; Maven uses each current module’s packaged artifact. Unrelated module outputs are never merged into an application. Reports qualify scenario names as `name [scope identity]` when multiple scopes are analyzed. Added and removed scopes are compared against empty inputs using their actual bytecode; scope membership is not itself a verdict. Plugins select final artifacts from their native models; engine input decoding normalizes archives and rejects unreadable inputs before graph analysis. Missing source mappings reduce attribution precision without preventing graph construction or folding.
- Unknown external observation behavior does not block literal application-code comparison when declarations and method identities are retained without opaque normalization or method omission. Surviving method differences still fail control coverage. Missing application bodies, metadata changes, resource proofs and transformations requiring knowledge of an external observer retain their relevant diagnostics.
- Effective dependency projections are reused within one analysis only when dependency content, input projection policy, relevant class definitions and application class copies agree. Inputs are hashed again on lookup, including directory resources. This avoids repeated validation and rewriting of shared libraries without synthesizing deployment scopes from library modules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.johnsonlee.graphfold.engine.inputs.archive
import java.nio.file.Files
import java.nio.file.Path
import java.util.jar.JarFile
import java.util.jar.JarEntry
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

/** An explicitly selected archive; compiler outputs provide source evidence, never replacement graph bytes. */
data class ArchiveEntryInput(
Expand Down Expand Up @@ -33,16 +34,16 @@ internal fun prepareArchiveInput(
entry.copy(application = entry.application || selectedPrimary || input.ownsApplicationArchive(entry.artifact))
})
val expanded = springBootArchiveInput(selected, output.resolve("boot"))
?: warArchiveInput(selected, output.resolve("war")) ?: selected
return prepareFlatArchiveInput(expanded, provenance, output, variantThreshold).copy(archiveInput = input)
?: warArchiveInput(selected, output.resolve("war")) ?: ArchiveProjection(selected, StaticClassOrder.EXPLICIT_ORDER)
Comment thread
johnsonlee marked this conversation as resolved.
Resolved
return prepareFlatArchiveInput(expanded.input, provenance, output, variantThreshold, expanded.classOrder).copy(archiveInput = input)
}

/** Membership is independent of source association; legacy producer records also establish project ownership. */
internal fun ArchiveInput.ownsApplicationArchive(artifact: Path): Boolean =
(applicationArchives + producerArchives.keys).any { Files.isRegularFile(it) && Files.mismatch(it, artifact) == -1L }

private fun prepareFlatArchiveInput(
input: ArchiveInput, provenance: InputProject, output: Path, variantThreshold: Int,
input: ArchiveInput, provenance: InputProject, output: Path, variantThreshold: Int, classOrder: StaticClassOrder,
): InputProject {
checkInput(input.classpath.isNotEmpty(), "Input scope ${input.id} has no packaged classpath")
val original = input.classpath.map { canonicalPackagedArchive(it.artifact) }
Expand All @@ -52,33 +53,27 @@ private fun prepareFlatArchiveInput(
val restored = original.mapIndexed { index, artifact ->
selectedArchiveInventory(artifact, variantThreshold, output.resolve("inventory/$index.jar"))
}
val ordinary = restored.mapIndexed { index, path ->
val ordinary = original.mapIndexed { index, path ->
val archive = output.resolve("archives/$index.jar")
if (input.classpath[index].inventoryOnly) {
Files.createDirectories(archive.parent)
java.util.jar.JarOutputStream(Files.newOutputStream(archive)).use { }
archive
} else normalizePackagedArchive(path, archive)
}
// Identical definitions may deduplicate, but an application's copy must retain its application role.
// Different definitions still fail shared normalization regardless of this role-based processing order.
val classOrder = input.classpath.indices.sortedBy { if (input.classpath[it].application) 0 else 1 }
val normalized = normalizeDependencyInputs(classOrder.map(ordinary::get), output.resolve("effective"),
variantThreshold = variantThreshold)
val byIndex = classOrder.zip(normalized).toMap()
val effective = ordinary.indices.map(byIndex::getValue)
val effective = normalizeDependencyInputs(ordinary, output.resolve("effective"),
variantThreshold = variantThreshold, classOrder = classOrder)
val classes = Files.createDirectories(output.resolve("classes"))
input.classpath.indices.filter { input.classpath[it].application }.forEach { index ->
copyPackagedClasses(effective[index], classes)
}
copyApplicationClasses(effective, input.classpath.indices.filter { input.classpath[it].application }.map(ordinary::get),
classes, variantThreshold)
val dependencies = normalizeDependencyInputs(effective, output.resolve("dependencies"),
variantThreshold = variantThreshold, applicationClasses = classes)
val ownedOutputs = input.classpath.flatMap { it.producerOutputs }.map { it.toFile().canonicalFile.toPath() }.toSet()
val sourceOutputs = provenance.sourceOutputs.entries.groupBy({ it.key.toFile().canonicalFile.toPath() }, { it.value })
.mapValues { (_, groups) -> groups.flatten().map { it.toFile().canonicalFile.toPath() }
.filter { it in ownedOutputs }.distinct() }.filterValues { it.isNotEmpty() }
val resources = restored.mapIndexed { index, artifact ->
packagedResources(artifact, output.resolve("resources/$index"))
packagedResources(artifact, output.resolve("resources/$index"), original = artifact == original[index])
}
val inventory = captureProductionInventory(classes, resources, restored,
input.classpath.indices.filter { input.classpath[it].application }.map { restored[it] })
Expand All @@ -102,22 +97,12 @@ private fun canonicalCompilerClasspaths(
contexts.singleOrNull().orEmpty()
}

private fun copyPackagedClasses(archive: Path, output: Path) {
JarFile(archive.toFile()).use { jar ->
jar.entries().asSequence().filter { !it.isDirectory && it.name.endsWith(".class") }.forEach { entry ->
val target = output.resolve(entry.name).normalize()
checkInput(target.startsWith(output) && output.relativize(target).joinToString("/") == entry.name,
"Unsupported packaged class path: ${entry.name}")
Files.createDirectories(target.parent)
jar.getInputStream(entry).use { Files.copy(it, target) }
}
}
}

/** Preserve each artifact's effective resource entries separately; conflicting properties remain unproved. */
private fun packagedResources(artifact: Path, output: Path): Path {
private fun packagedResources(artifact: Path, output: Path, original: Boolean): Path {
Files.createDirectories(output)
JarFile(artifact.toFile()).use { jar ->
// The inventory projection preserves signature evidence, but only original bytes can be authenticated.
val archive = if (original) JarFile(artifact.toFile()) else ZipFile(artifact.toFile())
archive.use { jar ->
jar.stream().use { entries ->
entries.filter { !it.isDirectory && !it.name.endsWith(".class") && !it.name.startsWith("META-INF/versions/") }
.forEach { entry -> copyPackagedResource(jar, entry, output) }
Expand All @@ -133,7 +118,7 @@ private fun ProductionInventory.withArchiveMetadata(input: ArchiveInput): Produc
resourceIndependentEntries = resourceIndependentEntries + inputs)
}

private fun copyPackagedResource(jar: JarFile, entry: JarEntry, output: Path) {
private fun copyPackagedResource(jar: ZipFile, entry: ZipEntry, output: Path) {
val target = output.resolve(entry.name).normalize()
checkInput(target.startsWith(output) && target != output && output.relativize(target).joinToString("/") == entry.name,
"Unsupported packaged resource path: ${entry.name}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.johnsonlee.graphfold.engine.inputs.archive

/** Order evidence belongs to static input preparation, independently of application ownership. */
internal enum class StaticClassOrder { UNAMBIGUOUS, EXPLICIT_ORDER }

internal data class ArchiveProjection(val input: ArchiveInput, val classOrder: StaticClassOrder)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ internal fun archiveVariant(name: String): Pair<Int, String>? = VERSIONED_ENTRY.

private val VERSIONED_ENTRY = Regex("""META-INF/versions/([1-9][0-9]*)/(.+)""")

/** Inventory and graphs see the same build-selected MR contents, including effective resource entries. */
/**
* Inventory retains original metadata alongside build-selected contents. This is an evidence ZIP, not a signed JAR:
* consumers must use ZIP readers; graph normalization independently reads the original signed archive.
*/
internal fun selectedArchiveInventory(archive: Path, javaVersion: Int, output: Path): Path =
JarFile(archive.toFile()).use { jar ->
val multiRelease = jar.manifest?.mainAttributes?.getValue(Attributes.Name.MULTI_RELEASE)
Expand All @@ -43,3 +46,11 @@ internal fun selectedArchiveInventory(archive: Path, javaVersion: Int, output: P
private fun copyArchiveEntry(jar: JarFile, entry: JarEntry, target: JarOutputStream) {
jar.getInputStream(entry).use { it.copyTo(target) }
}

/** Signing blocks authenticate the original archive, never an analysis projection with rewritten entries. */
internal fun archiveSignature(name: String): Boolean {
val normalized = name.uppercase(java.util.Locale.ROOT)
if (!normalized.startsWith("META-INF/")) return false
val file = normalized.removePrefix("META-INF/")
return '/' !in file && (file.startsWith("SIG-") || file.substringAfterLast('.') in setOf("SF", "RSA", "DSA", "EC"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import java.util.jar.JarFile
/** Per-analysis reuse only: content, prior class definitions and application copies all participate in the key. */
internal class DependencyInputCache {
private data class Input(val path: Path, val digest: String, val variant: Int)
private data class Key(val input: Input, val shadowed: Map<String, String>, val application: Map<String, String>)
private data class Key(
val input: Input, val shadowed: Map<String, String>, val application: Map<String, String>, val classOrder: StaticClassOrder,
)
private data class Projection(val path: Path, val introduced: Map<String, String>)
private val names = mutableMapOf<Input, Set<String>>()
private val projections = mutableMapOf<Key, Projection>()

fun materialize(
dependency: Path, variantThreshold: Int, application: Path?, selected: MutableMap<String, String>,
produce: () -> Path,
classOrder: StaticClassOrder, produce: () -> Path,
): Path {
val path = dependency.toAbsolutePath().normalize()
val input = Input(path, digest(path), variantThreshold)
Expand All @@ -25,7 +27,7 @@ internal class DependencyInputCache {
root?.resolve(name)?.normalize()?.takeIf { it.startsWith(root) && Files.isRegularFile(it) }
?.let { name to digest(it) }
}.toMap()
val key = Key(input, selected.filterKeys { it in classes }, copies)
val key = Key(input, selected.filterKeys { it in classes }, copies, classOrder)
val projection = projections.getOrPut(key) {
val before = selected.keys.toSet()
Projection(produce(), selected.filterKeys { it !in before })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import org.objectweb.asm.tree.ClassNode
* Normalizes explicitly selected static classpaths without selecting bytecode by the analyzer JVM.
* All bytecode consumers must receive these same paths; production inventories retain the original archives.
* Multi-release alternatives are explicit variants. Validated module descriptors remain metadata, while service
* resources remain in the selected classpath. Different class definitions are ambiguous; independent scopes are never combined.
* resources remain in the selected classpath. Definitions follow explicit input order when supplied; independent scopes stay separate.
*/
internal fun normalizeDependencyInputs(
dependencies: List<Path>, output: Path, variantThreshold: Int = 0,
applicationClasses: Path? = null, cache: DependencyInputCache? = null,
classOrder: StaticClassOrder = StaticClassOrder.UNAMBIGUOUS,
): List<Path> {
val selection = StaticClassSelection(applicationClasses)
val selection = StaticClassSelection(applicationClasses, classOrder)
return dependencies.mapIndexed { index, dependency ->
checkInput(Files.exists(dependency), "Missing input dependency: $dependency")
val target = output.resolve("dependency-$index.jar")
Expand All @@ -33,7 +34,7 @@ internal fun normalizeDependencyInputs(
else materializeArchive(dependency, target, selection, variantThreshold)
}
if (cache == null) produce()
else cache.materialize(dependency, variantThreshold, applicationClasses, selection.selected, produce)
else cache.materialize(dependency, variantThreshold, applicationClasses, selection.selected, classOrder, produce)
}
}

Expand All @@ -56,7 +57,7 @@ private fun materializeArchive(
if (!requiresArchiveProjection(archive, physical, entries.size, retained.size)) return@use dependency
validateArchiveProjection(archive, dependency)
writeInputArchive(target) { zip ->
retained.forEach { entry ->
retained.filterNot { archiveSignature(it.name) }.forEach { entry ->
val bytes = contents(entry)
writeEntry(zip, entry.name, if (entry.name == JarFile.MANIFEST_NAME) projectedManifest(bytes) else bytes)
}
Expand Down Expand Up @@ -96,16 +97,16 @@ private fun materializeDirectory(
}
if (retained.size == entries.size) return dependency
writeInputArchive(target) { zip ->
retained.forEach { (name, file) ->
retained.filterKeys { !archiveSignature(it) }.forEach { (name, file) ->
val bytes = Files.readAllBytes(file)
writeEntry(zip, name, if (name == JarFile.MANIFEST_NAME) projectedManifest(bytes) else bytes)
}
}
return target
}

/** A static scope may contain identical copies, but never two different definitions of one class. */
private class StaticClassSelection(applicationClasses: Path?) {
/** Different definitions require explicit classpath order; ownership never determines the selected bytes. */
private class StaticClassSelection(applicationClasses: Path?, private val classOrder: StaticClassOrder) {
private val application = applicationClasses?.toAbsolutePath()?.normalize()
val selected = mutableMapOf<String, String>()

Expand All @@ -114,12 +115,14 @@ private class StaticClassSelection(applicationClasses: Path?) {
val contents = bytes()
val identity = MessageDigest.getInstance("SHA-256").digest(contents).joinToString("") { "%02x".format(it) }
val previous = selected.putIfAbsent(name, identity)
checkInput(previous == null || previous == identity, "Ambiguous static class definition: $name")
checkInput(classOrder == StaticClassOrder.EXPLICIT_ORDER || previous == null || previous == identity,
"Ambiguous static class definition: $name")
val file = application?.let { root ->
root.resolve(name).normalize().takeIf { it.startsWith(root) && Files.isRegularFile(it) }
}
if (file != null) {
checkInput(Files.readAllBytes(file).contentEquals(contents), "Ambiguous application/dependency class definition: $name")
checkInput(classOrder == StaticClassOrder.EXPLICIT_ORDER || Files.readAllBytes(file).contentEquals(contents),
"Ambiguous application/dependency class definition: $name")
}
return previous == null && file == null
}
Expand Down
Loading
Loading