diff --git a/AGENTS.md b/AGENTS.md index 2266c066..b41c9df5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,8 @@ GraphFold statically analyzes control coverage and safety evidence for productio - Selecting an artifact establishes the analysis input, not a startup contract. Do not require a runnable entrypoint, `Main-Class`, a container installation or a supported startup script to analyze a selected JAR/WAR. Missing static type or call evidence remains an analysis gap, not a startup-compatibility verdict. - Do not expand the task into JVM startup-option compatibility checks or runtime-environment behavior modeling. Reading archive contents to establish the static input is distinct from verifying how an application starts or behaves at runtime. - Runtime executions in tests may independently validate fixture behavior. They are not the engine's analysis method, cannot establish control coverage, and cannot by themselves justify accepting an uncontrolled change. Golden cases must assert static verdicts independently of whether observed outputs differ. -- General uncontrolled-but-safe acceptance and its fixture/result schema are tracked in [issue #6](https://github.com/johnsonlee/GraphFold/issues/6). The existing Boolean corpus labels do not establish support for that path. When implementing it, fixtures must separately assert control coverage, cited safety evidence and the acceptance decision, with paired cases accepting sufficient whole-change proof while retaining the uncontrolled classification and rejecting insufficient proof. Do not manufacture such evidence or relabel an existing controlled case to claim the capability is implemented. +- Fixtures must separately assert control coverage, cited safety evidence and the acceptance decision, with paired cases accepting sufficient whole-change proof while retaining the uncontrolled classification and rejecting insufficient proof. Boolean corpus labels alone do not establish this capability. Use engine-produced evidence; do not manufacture certificates or relabel an existing controlled case. +- An original-bytecode safety summary must not override a selected fold in the method or its transitive dependencies. Original control implementations are not substitutes for the explicitly selected values. Preserve production instructions that a proof does not traverse; an inactive branch cannot erase a changed body, effect or exception obligation. - A new exposure predicate before an AB check may be eliminated only when static proof establishes that it is effect-free, nonthrowing, terminating, unused elsewhere, and redundant after folding the selected control. Being read-only or not observed to execute is insufficient. - Review requests must be evaluated against this scope. Do not implement runtime-analysis features merely to satisfy a review comment; explain the scope boundary and address the relevant static-analysis concern. - Multi-release JARs use only the JDK selected by the corresponding build, supplied by the integration. Do not enumerate JDK variants or select using the engine JVM. BASE and HEAD retain their own build JDK metadata. diff --git a/README.md b/README.md index a8315fb0..198bca62 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ Detect uncontrolled code changes through conditional folding. -GraphFold compares two Git commits under the same explicit folding assumptions. The diff establishes what must be proved; Graphite selects call sites with Cypher and folds JVM code; the engine compares the remaining representation and reports changes that could not be proved controlled. AB tests are one use case, not an engine-specific concept. +GraphFold compares two Git commits under the same explicit folding assumptions. The diff establishes what must be proved; Graphite selects call sites with Cypher and folds JVM code; the engine reports control coverage, static safety evidence and an acceptance decision. AB tests are one use case, not an engine-specific concept. -The gate is fail-closed: both surviving changes and analysis failures produce `FAIL`. Surviving structural differences are reported as unverified changes; build, Git, and incomplete-analysis errors are diagnostics rather than invented source locations. +An uncontrolled change can pass with a complete static equivalence proof while remaining classified as uncontrolled. Surviving differences without sufficient proof and analysis failures produce `FAIL`. Build, Git, and incomplete-analysis errors are diagnostics rather than invented source locations. See [the design](docs/design.md) for the proof model, gaps, and trade-offs, and [validation results](docs/validation.md) for the executed checks. @@ -98,6 +98,10 @@ scenarios: Queries express caller-authorized assumptions; an overly broad rule is an overly broad assumption. GraphFold does not infer the intended feature ID from the source. The default scenario limit is 32; exceeding it fails rather than sampling scenarios. +Use an explicit `folds: []` to compare without selecting any controls. This permits +independent static safety proofs; it does not make uncontrolled changes controlled. +Omitting the `folds` field remains invalid. + Fold input must be one YAML document with an object root. Syntax, duplicate keys, unknown fields and rule structure are validated before Git checkout or compilation; named scenarios use the `scenarios` form. Cypher syntax and result shape are checked @@ -150,13 +154,38 @@ outcomes; a rename label is a candidate, not an equivalence proof. JSON preserves the existing code names. Other finite statuses and protocol kinds also use enums internally, with strings decoded and encoded at external boundaries. -Reports declare `schemaVersion: 1` and follow the packaged +Reports declare `schemaVersion: 2` and follow the packaged [verdict JSON Schema](engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json) (draft 2020-12), also available through `AnalysisReports.schema()`. The schema applies to the serialized result regardless of whether its filename is `verdict.json` or the Gradle default `report.json`. Structural schema validation complements the engine's cross-field and proof checks; it does not establish that a verdict is correct. +Each reason includes an `assessment` with independent `control`, `safety`, +`decision`, and `basis` fields. `CONTROLLED` requires proof connecting the change +to the selected controls. `UNCONTROLLED` can still be `ACCEPTED` on a complete +static whole-change equivalence proof; acceptance never relabels it controlled. +`UNPROVEN` records missing evidence, while `REFUTED` records a disproved safety +claim. A rejected assessment prevents the overall report from passing. + +Safety evidence identifies its claim, status, subject, supporting evidence, and +assumptions. `EFFECT_FREE`, `NONTHROWING`, and `TERMINATING` method proofs alone +cannot justify acceptance: changing `return price` to `return 0` can satisfy all +three and still change the result. `STATIC_SAFETY` acceptance requires a cited, +`PROVEN` `WHOLE_CHANGE_EQUIVALENT` certificate. Representation equivalence and +input identity have separate acceptance bases; neither implies control coverage. +The schema validates the certificate structure, not the truth of its proof. + +The safety analysis uses packaged method bodies, flow-sensitive null facts and +resolved callee summaries. Its supported equivalence proof compares total, +effect-free return expressions in both revisions and preserves untraversed +production instructions. Every surviving difference must be covered; changed +declarations, resources, dependencies or unresolved graph obligations cannot be +discharged by proving one helper safe. Original-bytecode proofs cannot override +selected folds in a method or its callees. Unsupported operations remain unproven. +The [safety golden manifest](engine/src/test/resources/safety/manifest.json) records +control, safety claims and acceptance separately for packaged Java/Kotlin cases. + `ruleAudits` records each scenario, revision and zero-based rule index. Each entry contains the selector, `selectionStatus`, all unique raw `selectedNodes`, nullable `selectedCount` and independent `matchedCallCount` from Graphite's fold report. diff --git a/docs/design.md b/docs/design.md index adf315ae..ec68ce12 100644 --- a/docs/design.md +++ b/docs/design.md @@ -27,7 +27,7 @@ fold(head, A_PR) ≡ fold(base, A_PR) | Verdict | 条件 | | --- | --- | | `PASS` | 分析成功完成,本次所有相关变化均被证明消除或等价 | -| `FAIL` | 确认 uncontrolled change,或存在阻止完成证明的错误、缺口或定位失败 | +| `FAIL` | 存在缺乏充分安全证明的 uncontrolled change,或存在阻止完成证明的错误、缺口或定位失败 | `FAIL` 区分未受 control 约束的静态生产代码差异、证据不足的变更和分析过程错误;它不声称已经发生运行时业务行为差异。 @@ -190,7 +190,7 @@ data class AnalysisResult( val metadata: AnalysisMetadata, val reasons: List = emptyList(), val ruleAudits: List = emptyList(), -) // schemaVersion 固定为 1;verdict 由 findings、blocking diagnostics 与证明完成情况派生,不由调用者赋值。 +) // schemaVersion 固定为 2;verdict 由 findings、blocking diagnostics 与证明完成情况派生,不由调用者赋值。 ``` `Evidence`、`Diagnostic`、`AnalysisMetadata` 和 `VerdictReason` 已在 engine 定义。`Finding.locations` 至少一项,可跨文件和 revision,且都是本次 diff 的归属位置;调用链等未修改位置放入 evidence。纯移动的 `VerdictReason.locations` 可为空,此时以旧/新路径描述变化,不制造字符 diff。 @@ -215,7 +215,7 @@ Finding 分类: 当前 Metadata 记录解析后的 SHA、Graphite 版本、场景和 obligation/证明完成数量。编译器版本、规则摘要、构建产物身份与完整 provenance 仍是待补的审计信息,不能声称已经输出。Finding ID 使用内容摘要,不依赖图节点编号。 -JSON 报告包含 `schemaVersion: 1`,规范为 [verdict.schema.json](../engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json),采用 JSON Schema draft 2020-12,标识为 `urn:io.johnsonlee.graphfold:verdict:1`。同一资源随 engine 打包,可通过 `AnalysisReports.schema()` 读取。Schema 校验序列化结构、必需字段、枚举、空值与数值边界,不代替引擎的语义证明。它适用于任意报告文件名;Gradle 默认仍输出 `report.json`。 +JSON 报告包含 `schemaVersion: 2`,规范为 [verdict.schema.json](../engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json),采用 JSON Schema draft 2020-12,标识为 `urn:io.johnsonlee.graphfold:verdict:2`。同一资源随 engine 打包,可通过 `AnalysisReports.schema()` 读取。Schema 校验序列化结构、必需字段、枚举、空值与数值边界,不代替引擎的语义证明。它适用于任意报告文件名;Gradle 默认仍输出 `report.json`。 ## 4. 仓库获取与共享对象 checkout @@ -904,6 +904,12 @@ P0 表示上线门禁前必须解决,或必须可靠识别并阻断;不意 `reasons` 对 PASS 和 FAIL 都提供解释,包括 AST 识别的 `changes`、证明结论 `code`、文字说明、场景、diff 位置和 base/head 路径。分类包括字段初始化值变化、条件增删、调用变化、成员增删、字段/方法重命名候选、文件移动/改名与类初始化变化。JavaParser/Kotlin PSI 只负责描述变化;分类本身不决定 verdict。 +报告 v2 为每个 reason 增加 `assessment`,将 `control`(CONTROLLED / UNCONTROLLED / UNPROVEN)、`safety` 证据、`decision`(ACCEPTED / REJECTED)及 `basis` 分开。受控性必须来自选定 control 的静态证明,接受不等于受控:完整的整体变更等价证明可以接受 UNCONTROLLED 变化,但不能把它改标 CONTROLLED。显式 REJECTED assessment 阻止整体 PASS;没有 reason 的早期或空 diff 报告仍由 findings、blocking diagnostics 和证明计数判定。 + +每项 safety 证据包含 claim、status、subject、evidence 和 assumptions。PROVEN、REFUTED、UNPROVEN 分别表示已证明、已反证、未证明;未知不等于危险。EFFECT_FREE、NONTHROWING、TERMINATING 只是方法性质,例如将 `return price` 改成 `return 0` 仍可能满足这三项,不能据此接受变更。STATIC_SAFETY 接受要求 UNCONTROLLED 分类及带实际引用证据的 PROVEN WHOLE_CHANGE_EQUIVALENT 证明。INPUT_IDENTITY、REPRESENTATION_EQUIVALENCE 和 CONTROL_COVERAGE 是独立的接受依据;缺口和存活差异分别使用 INCOMPLETE_PROOF、SURVIVING_DIFFERENCE。Schema 验证这些字段及证书结构,不验证证明真实性。 + +显式 `folds: []` 请求独立的静态比较,不提供控制覆盖。一般安全分析从实际 class 和声明依赖读取方法体,传播空值事实、参数、返回值、异常、效果与终止证据。整体等价证明要求所有剩余差异都获得证明,方法声明一致,且不存在未解决的图义务;未遍历的生产指令也必须保留在比较证据中。原始字节码摘要不能覆盖选定折叠对当前方法或其传递依赖的修改。新增曝光条件仍独立要求与选定 control 关联、结果没有其他用途且折叠后冗余;原实现对 control 返回值的计算不能代替用户选定的值。 + `FOLDED_EQUIVALENT` 表示受支持的表示中未留下差异;`UNREACHABLE` 表示同一输入内有静态证据证明,选定 control 的折叠消除了通往该变更的路径;未调用或没有流量本身不能得到该结论;`SURVIVING_DIFFERENCE` 表示仍有未证明一致的表示差异。分析完整性缺口由 blocking diagnostic 表达,不能伪装成已定位 violation。`METHOD_RENAMED`/`FIELD_RENAMED` 是一对一结构候选,正文或值同时变化时不能只凭名称相似进行配对。 分类器以实际 diff 范围筛选节点,并允许在两侧源码中配对重命名,使仅有 BASE 或 HEAD 位置的 obligation 也能得到解释。未细分的语法保守标记为 `DECLARATION_CHANGED`/`BODY_CHANGED`;例如部分构造器与 Kotlin 主构造器属性参数尚无专用分类。这影响解释粒度,不豁免证明义务。 diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisModel.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisModel.kt index 6ae51b5f..2715b13a 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisModel.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisModel.kt @@ -195,12 +195,14 @@ data class AnalysisResult( val ruleAudits: List = emptyList(), ) { /** Version of the serialized report contract described by [AnalysisReports.schema]. */ - val schemaVersion: Int = 1 - /** Passes only with no findings, no blocking diagnostics, and all obligations proved. */ + val schemaVersion: Int = 2 + /** Passes only with no findings, blocking diagnostics, rejected assessments, or unproved obligations. */ val verdict: Verdict - get() = if (findings.isEmpty() && diagnostics.none { it.blocking } && - metadata.obligations == metadata.provedObligations - ) Verdict.PASS else Verdict.FAIL + get() { + val proofsComplete = metadata.obligations == metadata.provedObligations && + reasons.none { it.assessment.decision == AcceptanceDecision.REJECTED } + return if (findings.isEmpty() && diagnostics.none { it.blocking } && proofsComplete) Verdict.PASS else Verdict.FAIL + } } /** Working storage and scenario limit for static analysis; no build or repository configuration. */ @@ -263,6 +265,8 @@ enum class ReasonCode { FOLDED_EQUIVALENT, /** Validated same-input evidence ties removal of all static paths to the explicitly selected controls. */ UNREACHABLE, + /** A complete, explicitly cited static whole-change proof supports acceptance without control coverage. */ + STATIC_SAFETY_PROVEN, /** The folded representations still differ for this obligation. */ SURVIVING_DIFFERENCE, /** An analysis gap prevents a safety conclusion. */ @@ -279,6 +283,7 @@ enum class ReasonCode { * @property scenario Name of the folding scenario used for this proof. * @property basePath Repository-relative old path, or null if absent or unspecified. * @property headPath Repository-relative new path, or null if absent or unspecified. + * @property assessment Independent control classification, safety evidence, and acceptance decision. */ data class VerdictReason( val changes: Set, @@ -288,4 +293,11 @@ data class VerdictReason( val scenario: String, val basePath: String? = null, val headPath: String? = null, -) + val assessment: ChangeAssessment = code.defaultAssessment(), +) { + init { + if (code == ReasonCode.STATIC_SAFETY_PROVEN && assessment.decision == AcceptanceDecision.ACCEPTED) { + require(assessment.basis == AcceptanceBasis.STATIC_SAFETY) { "static proof requires static safety evidence" } + } + } +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisReports.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisReports.kt index 417b13c4..44328281 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisReports.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/AnalysisReports.kt @@ -2,7 +2,7 @@ package io.johnsonlee.graphfold.engine import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -/** Common report encoding for the CLI, Gradle plugin, and SDK consumers. */ +/** Common report encoding for Gradle and Maven plugins and SDK consumers. */ object AnalysisReports { private val mapper = jacksonObjectMapper() @@ -57,6 +57,8 @@ object AnalysisReports { foldSelectionSummary(result).forEach { appendLine(it) } result.reasons.forEach { reason -> appendLine("${reason.code} [${reason.changes.joinToString()}]: ${reason.message}") + appendLine(" ${reason.assessment.control}: ${reason.assessment.decision} via ${reason.assessment.basis}") + reason.assessment.safety.forEach { proof -> appendSafetyEvidence(proof) } } result.findings.forEach { finding -> appendLine("${finding.kind}: ${finding.message}") @@ -72,3 +74,9 @@ object AnalysisReports { } }.trimEnd() } + +private fun StringBuilder.appendSafetyEvidence(proof: SafetyEvidence) { + appendLine(" ${proof.claim} ${proof.status}: ${proof.subject}") + proof.evidence.forEach { appendLine(" evidence: $it") } + proof.assumptions.forEach { appendLine(" assumption: $it") } +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessment.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessment.kt new file mode 100644 index 00000000..0b177023 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessment.kt @@ -0,0 +1,64 @@ +package io.johnsonlee.graphfold.engine + +/** Static control coverage, independently of safety and acceptance. */ +enum class ControlClassification { CONTROLLED, UNCONTROLLED, UNPROVEN } + +/** Property established or left unresolved by a static proof. */ +enum class SafetyClaim { EFFECT_FREE, NONTHROWING, TERMINATING, WHOLE_CHANGE_EQUIVALENT } + +/** An absent proof is not a demonstrated hazard. */ +enum class SafetyStatus { PROVEN, REFUTED, UNPROVEN } + +/** Per-obligation acceptance; accepting an uncontrolled change does not relabel it controlled. */ +enum class AcceptanceDecision { ACCEPTED, REJECTED } + +/** The specific proof or remaining difference responsible for the acceptance decision. */ +enum class AcceptanceBasis { + CONTROL_COVERAGE, STATIC_SAFETY, REPRESENTATION_EQUIVALENCE, INPUT_IDENTITY, + INCOMPLETE_PROOF, SURVIVING_DIFFERENCE, +} + +/** + * Cited static evidence for one claim about [subject], with its explicit [assumptions]. + * Method properties alone do not establish [SafetyClaim.WHOLE_CHANGE_EQUIVALENT]. + */ +data class SafetyEvidence( + val claim: SafetyClaim, + val status: SafetyStatus, + val subject: String, + val evidence: List, + val assumptions: List, +) + +/** Independent control, safety, and acceptance axes for one change obligation. */ +data class ChangeAssessment( + val control: ControlClassification, + val safety: List, + val decision: AcceptanceDecision, + val basis: AcceptanceBasis, +) { + init { + if (basis == AcceptanceBasis.STATIC_SAFETY && decision == AcceptanceDecision.ACCEPTED) { + require(control == ControlClassification.UNCONTROLLED) { "static safety does not establish control coverage" } + require(safety.any { it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT && + it.status == SafetyStatus.PROVEN && it.subject.isNotBlank() && + it.evidence.isNotEmpty() && it.evidence.all(String::isNotBlank) + }) { "static acceptance requires cited whole-change equivalence evidence" } + } + } +} + +/** Conservative compatibility mapping; the engine supplies additional evidence when available. */ +internal fun ReasonCode.defaultAssessment(): ChangeAssessment = when (this) { + ReasonCode.UNREACHABLE -> ChangeAssessment(ControlClassification.CONTROLLED, emptyList(), + AcceptanceDecision.ACCEPTED, AcceptanceBasis.CONTROL_COVERAGE) + ReasonCode.FOLDED_EQUIVALENT -> ChangeAssessment(ControlClassification.UNPROVEN, emptyList(), + AcceptanceDecision.ACCEPTED, AcceptanceBasis.REPRESENTATION_EQUIVALENCE) + ReasonCode.BUILD_OUTPUT_UNCHANGED -> ChangeAssessment(ControlClassification.UNCONTROLLED, emptyList(), + AcceptanceDecision.ACCEPTED, AcceptanceBasis.INPUT_IDENTITY) + ReasonCode.SURVIVING_DIFFERENCE -> ChangeAssessment(ControlClassification.UNCONTROLLED, emptyList(), + AcceptanceDecision.REJECTED, AcceptanceBasis.SURVIVING_DIFFERENCE) + ReasonCode.ANALYSIS_INCOMPLETE, ReasonCode.STATIC_SAFETY_PROVEN -> + ChangeAssessment(ControlClassification.UNPROVEN, emptyList(), + AcceptanceDecision.REJECTED, AcceptanceBasis.INCOMPLETE_PROOF) +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessments.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessments.kt new file mode 100644 index 00000000..51e2b075 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessments.kt @@ -0,0 +1,65 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.snapshot.ProgramSnapshot +import io.johnsonlee.graphfold.engine.source.ProofObligation + +/** Combines independent proof domains without changing the structural differences they explain. */ +internal class ComparisonAssessments( + base: ProgramSnapshot, head: ProgramSnapshot, alignedNames: Map, + safety: (() -> WholeChangeSafety)?, private val selectedControls: Boolean?, + unfolded: (() -> SnapshotComparison)?, private val removedByControl: (ProofObligation) -> Boolean, + private val foldedBodies: (SnapshotComparison) -> Set, +) { + private val safetyProof by lazy { safety?.invoke() } + private val controlBaseline by lazy { unfolded?.invoke() } + private val unchangedDeclarations = base.methods.keys.intersect(head.methods.keys).filterTo(hashSetOf()) { + base.declarations[it] != null && base.declarations[it] == head.declarations[it] && + it !in alignedNames && it !in alignedNames.values + } + + fun safety(differences: List, gaps: List): ChangeAssessment? { + return if (differences.isEmpty()) null else safetyProof?.let { proof -> + proof.assess(differences, gaps, unchangedDeclarations, changedByFolding(differences)) + } + } + + private fun changedByFolding(differences: List): Set { + if (selectedControls != true) return emptySet() + val original = controlBaseline + return if (original == null) differences.toSet() else foldedBodies(original) + } + + fun equivalence(obligation: ProofObligation?): ChangeAssessment { + val classification = when { + selectedControls == false -> ControlClassification.UNCONTROLLED + obligation?.let(removedByControl) == true -> ControlClassification.CONTROLLED + else -> baselineClassification(obligation) + } + val basis = if (classification == ControlClassification.CONTROLLED) AcceptanceBasis.CONTROL_COVERAGE + else AcceptanceBasis.REPRESENTATION_EQUIVALENCE + return ChangeAssessment(classification, emptyList(), AcceptanceDecision.ACCEPTED, basis) + } + + private fun baselineClassification(obligation: ProofObligation?): ControlClassification { + val original = controlBaseline + return when { + original == null || original.analysisGaps(obligation).isNotEmpty() -> ControlClassification.UNPROVEN + (if (obligation == null) original.wholeProgramDifferences() else original.differences(obligation)).isEmpty() -> + ControlClassification.UNCONTROLLED + else -> ControlClassification.CONTROLLED + } + } +} + +internal fun SnapshotComparison.safetyAssessment(differences: List, gaps: List): ChangeAssessment? = + assessments.safety(differences, gaps) + +/** A before-fold difference must independently witness control coverage; equivalence alone cannot do so. */ +internal fun SnapshotComparison.equivalenceAssessment(obligation: ProofObligation? = null): ChangeAssessment = + assessments.equivalence(obligation) + +/** Original summaries cannot override selected folds in this method or a transitive callee. */ +internal fun SnapshotComparison.foldedBodies(original: SnapshotComparison): Set = + revisionMethods.zip(original.revisionMethods).flatMap { (folded, unfolded) -> + (folded.keys + unfolded.keys).filter { folded[it]?.canonical != unfolded[it]?.canonical } + }.toSet() diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/FoldDocument.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/FoldDocument.kt index d6d8358c..59e5e60f 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/FoldDocument.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/FoldDocument.kt @@ -58,7 +58,6 @@ object FoldDocument { val name = requireNotNull(scenario["name"] as? String) { "$path.name must be a string" } require(name.isNotBlank()) { "$path.name must not be blank" } val folds = requireNotNull(scenario["folds"] as? List<*>) { "$path.folds must be a list" } - require(folds.isNotEmpty()) { "$path.folds requires at least one fold rule" } return FoldScenario(name, folds.mapIndexed { index, rule -> parseRule(rule, "$path.folds[$index]") }) } diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/GraphFoldEngine.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/GraphFoldEngine.kt index c8b5f272..52bfb5af 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/GraphFoldEngine.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/GraphFoldEngine.kt @@ -1,6 +1,7 @@ package io.johnsonlee.graphfold.engine import io.johnsonlee.graphfold.engine.inputs.StaticScope +import io.johnsonlee.graphfold.engine.bytecode.asm.StaticSafetyAnalysis import io.johnsonlee.graphfold.engine.inputs.PreparedRevision import io.johnsonlee.graphfold.engine.inputs.ContentInventory import io.johnsonlee.graphfold.engine.git.ChangedFile @@ -344,28 +345,26 @@ class GraphFoldEngine(private val configuration: EngineConfiguration) { val analysisGaps = if (resourcesProved) comparison.resourceAnalysisGaps() else comparison.analysisGaps() analysisGaps.forEach { state.diagnostics += Diagnostic(AnalysisStage.GRAPH, DiagnosticCode.INCOMPLETE_GRAPH, it) } val differences = comparison.wholeProgramDifferences() - return if (proof.attributedSourceDifference(differences, analysisGaps, confirmedDifferences)) { + val assessment = comparison.safetyAssessment(differences, analysisGaps) + val survivingAssessment = assessment ?: ReasonCode.SURVIVING_DIFFERENCE.defaultAssessment() + return if (assessment?.decision == AcceptanceDecision.ACCEPTED) { + state.reasons += proof.reasons(ReasonCode.STATIC_SAFETY_PROVEN, scenario, + "Every surviving production difference has a complete static equivalence proof; the change remains uncontrolled", + assessment) + true + } else if (proof.attributedSourceDifference(differences, analysisGaps, confirmedDifferences)) { state.reasons += proof.reasons(ReasonCode.SURVIVING_DIFFERENCE, scenario, - "Actual production differences are covered by verified source findings: ${differences.joinToString()}") + "Actual production differences are covered by verified source findings: ${differences.joinToString()}", + survivingAssessment) false } else if (differences.isNotEmpty() && analysisGaps.isEmpty()) { // Complete original JVM differences need no source identity or compiler-origin attribution. val message = "Production class differences survive selected control folding: ${differences.joinToString()}" - state.reasons += proof.reasons(ReasonCode.SURVIVING_DIFFERENCE, scenario, message) + state.reasons += proof.reasons(ReasonCode.SURVIVING_DIFFERENCE, scenario, message, + survivingAssessment) state.diagnostics += Diagnostic(AnalysisStage.COMPARE, DiagnosticCode.PRODUCTION_OUTPUT_CHANGED, message) false - } else { - val proved = differences.isEmpty() && analysisGaps.isEmpty() - val message = if (proved) "Production outputs are unchanged or changed resource keys have no surviving reader; " + - "the entire analyzed production program is equivalent after folding" - else "Folded production program has differences or gaps; build input effects cannot be independently attributed: " + - (differences + analysisGaps).joinToString() - state.reasons += proof.reasons( - if (proved) ReasonCode.FOLDED_EQUIVALENT else ReasonCode.ANALYSIS_INCOMPLETE, scenario, message, - ) - if (!proved) state.diagnostics += Diagnostic(AnalysisStage.COMPARE, DiagnosticCode.UNVERIFIED_BUILD_INPUT, message) - proved - } + } else recordInputEquivalence(proof, comparison, scenario, state, InputComparison(differences, analysisGaps, assessment)) } private fun compareScenario( @@ -386,7 +385,19 @@ class GraphFoldEngine(private val configuration: EngineConfiguration) { } } state.stage = AnalysisStage.COMPARE - return SnapshotComparison(old, new, originsVerified) + return SnapshotComparison(old, new, originsVerified, + safety = { + WholeChangeSafety( + StaticSafetyAnalysis(base.compiled.classes, base.compiled.dependencies).use { it.summaries() }, + StaticSafetyAnalysis(head.compiled.classes, head.compiled.dependencies).use { it.summaries() }, + ) + }, selectedControls = scenario.rules.isNotEmpty(), + unfolded = if (scenario.rules.isEmpty()) null else { + { + SnapshotComparison(GraphiteAnalyzer().analyzeOriginProject(base, emptyList(), dependencyResources), + GraphiteAnalyzer().analyzeOriginProject(head, emptyList(), dependencyResources), originsVerified) + } + }) } private fun checkObligation( @@ -396,9 +407,12 @@ class GraphFoldEngine(private val configuration: EngineConfiguration) { val gaps = comparison.analysisGaps(obligation) gaps.forEach { state.diagnostics += Diagnostic(AnalysisStage.GRAPH, DiagnosticCode.INCOMPLETE_GRAPH, it) } val differences = comparison.differences(obligation) - val proved = differences.isEmpty() && gaps.isEmpty() + val assessment = comparison.safetyAssessment(differences, gaps) + val safe = assessment?.decision == AcceptanceDecision.ACCEPTED + val proved = differences.isEmpty() && gaps.isEmpty() || safe val reason = when { gaps.isNotEmpty() -> ReasonCode.ANALYSIS_INCOMPLETE + safe -> ReasonCode.STATIC_SAFETY_PROVEN !proved -> ReasonCode.SURVIVING_DIFFERENCE comparison.isUnreachable(obligation) -> ReasonCode.UNREACHABLE else -> ReasonCode.FOLDED_EQUIVALENT @@ -406,26 +420,54 @@ class GraphFoldEngine(private val configuration: EngineConfiguration) { state.reasons += VerdictReason( obligation.changes, reason, if (gaps.isEmpty()) reasonMessage(reason, differences) else gaps.joinToString(), obligation.locations, scenario, obligation.basePath, obligation.headPath, + assessment ?: if (proved) comparison.equivalenceAssessment(obligation) else reason.defaultAssessment(), + ) + if (!proved && differences.isNotEmpty()) recordSurvivingDifference( + obligation, comparison, scenario, state, differences, confirmedDifferences, ) - if (differences.isNotEmpty() && obligation.locations.isEmpty()) { - state.diagnostics += Diagnostic(AnalysisStage.COMPARE, DiagnosticCode.UNLOCATED_MOVE_DIFFERENCE, - "Moving ${diagnosticLabel(obligation.basePath)} to ${diagnosticLabel(obligation.headPath)} " + - "changes JVM representation: ${differences.joinToString()}") - } else if (differences.isNotEmpty()) { - val prefix = if (comparison.originsVerified) "Surviving program representation differs" else - "Surviving bytecode differs under original JVM identity; compiler-origin mappings are unverified" - val message = "$prefix: ${differences.joinToString()}" - state.findings += Finding( - digest("$scenario|${obligation.locations}|$message"), FindingKind.UNVERIFIABLE_CHANGE, - "folded-program-equivalence", message, obligation.locations, - differences.map { Evidence("Remaining difference in $it") }, listOf(scenario), - ) - if (comparison.originsVerified && gaps.isEmpty()) confirmedDifferences.addAll(differences) - } return proved } } +private fun recordSurvivingDifference( + obligation: ProofObligation, comparison: SnapshotComparison, scenario: String, state: RunState, + differences: List, confirmedDifferences: MutableSet, +) { + if (obligation.locations.isEmpty()) { + state.diagnostics += Diagnostic(AnalysisStage.COMPARE, DiagnosticCode.UNLOCATED_MOVE_DIFFERENCE, + "Moving ${diagnosticLabel(obligation.basePath)} to ${diagnosticLabel(obligation.headPath)} " + + "changes JVM representation: ${differences.joinToString()}") + } else { + val prefix = if (comparison.originsVerified) "Surviving program representation differs" else + "Surviving bytecode differs under original JVM identity; compiler-origin mappings are unverified" + val message = "$prefix: ${differences.joinToString()}" + state.findings += Finding( + digest("$scenario|${obligation.locations}|$message"), FindingKind.UNVERIFIABLE_CHANGE, + "folded-program-equivalence", message, obligation.locations, + differences.map { Evidence("Remaining difference in $it") }, listOf(scenario), + ) + if (comparison.originsVerified && comparison.analysisGaps(obligation).isEmpty()) confirmedDifferences.addAll(differences) + } +} + +private data class InputComparison(val differences: List, val gaps: List, val assessment: ChangeAssessment?) + +private fun recordInputEquivalence( + proof: InputChangeProof, comparison: SnapshotComparison, scenario: String, state: RunState, result: InputComparison, +): Boolean { + val proved = result.differences.isEmpty() && result.gaps.isEmpty() + val message = if (proved) "Production outputs are unchanged or changed resource keys have no surviving reader; " + + "the entire analyzed production program is equivalent after folding" + else "Folded production program has differences or gaps; build input effects cannot be independently attributed: " + + (result.differences + result.gaps).joinToString() + state.reasons += proof.reasons( + if (proved) ReasonCode.FOLDED_EQUIVALENT else ReasonCode.ANALYSIS_INCOMPLETE, scenario, message, + result.assessment ?: if (proved) comparison.equivalenceAssessment() else ReasonCode.ANALYSIS_INCOMPLETE.defaultAssessment(), + ) + if (!proved) state.diagnostics += Diagnostic(AnalysisStage.COMPARE, DiagnosticCode.UNVERIFIED_BUILD_INPUT, message) + return proved +} + private fun recordUnchangedInputs(proof: InputChangeProof, scenarios: List, state: RunState, message: String) { state.proved = proof.changes.size scenarios.forEach { state.reasons += proof.reasons(ReasonCode.BUILD_OUTPUT_UNCHANGED, it.name, message) } @@ -474,6 +516,7 @@ private fun source(root: Path, path: String?, revision: Revision, project: Origi private fun reasonMessage(reason: ReasonCode, differences: List): String = when (reason) { ReasonCode.UNREACHABLE -> "Selected controls removed every static path to this change from the same input before folding" ReasonCode.FOLDED_EQUIVALENT -> "No surviving difference in the analyzed representation" + ReasonCode.STATIC_SAFETY_PROVEN -> "Every remaining difference has a complete static equivalence proof; the change remains uncontrolled" else -> "Surviving differences: ${differences.joinToString()}" } diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/InputChangeProof.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/InputChangeProof.kt index 85447c2a..c03944ec 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/InputChangeProof.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/InputChangeProof.kt @@ -75,9 +75,11 @@ internal class InputChangeProof( } } - fun reasons(code: ReasonCode, scenario: String, message: String): List = changes.map { + fun reasons( + code: ReasonCode, scenario: String, message: String, assessment: ChangeAssessment = code.defaultAssessment(), + ): List = changes.map { VerdictReason(if (it in sourceChanges) emptySet() else setOf(ChangeKind.BUILD_INPUT_CHANGED), - code, message, emptyList(), scenario, it.basePath, it.headPath) + code, message, emptyList(), scenario, it.basePath, it.headPath, assessment) } /** Classifies an already failing comparison; every difference needs complete, located declaration evidence. */ diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/SnapshotComparison.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/SnapshotComparison.kt index addc48c8..6b35bc24 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/SnapshotComparison.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/SnapshotComparison.kt @@ -12,9 +12,16 @@ import io.johnsonlee.graphfold.engine.source.SymbolKind /** Conservative structural proof over each obligation's dependencies. Unknown frontend edges block upstream. */ internal class SnapshotComparison( private val base: ProgramSnapshot, originalHead: ProgramSnapshot, val originsVerified: Boolean = true, + private val safety: (() -> WholeChangeSafety)? = null, + private val selectedControls: Boolean? = null, + private val unfolded: (() -> SnapshotComparison)? = null, ) { private val alignment = PrivateMemberAlignment(if (originsVerified) base else originalHead, originalHead) private val head = alignment.aligned() + val assessments = ComparisonAssessments(base, originalHead, alignment.names, safety, selectedControls, unfolded, + ::isUnreachable, ::foldedBodies) + + internal val revisionMethods get() = listOf(base.methods, head.methods) val resourceDependenciesAnalyzed: Boolean get() = base.resourceDependenciesAnalyzed && head.resourceDependenciesAnalyzed private val entryPoints = base.methods.values.filter { MethodModifier.PRIVATE !in it.modifiers && !it.identity.substringBefore('(').endsWith(".") && diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafety.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafety.kt new file mode 100644 index 00000000..01beec6d --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafety.kt @@ -0,0 +1,54 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.PropertyProof + +/** A relational proof over every remaining difference, independent of selected controls. */ +internal class WholeChangeSafety( + private val base: Map, + private val head: Map, +) { + fun assess( + differences: List, gaps: List, unchangedDeclarations: Set, changedByFolding: Set = emptySet(), + ): ChangeAssessment { + require(differences.isNotEmpty()) { "Safety comparison requires a surviving change obligation" } + val properties = differences.flatMap { identity -> + listOfNotNull(base[identity]?.let { "BASE" to it }, head[identity]?.let { "HEAD" to it }) + .flatMap { (revision, summary) -> evidence("$revision $identity", summary) } + } + val equivalents = differences.mapNotNull { equivalent(it, unchangedDeclarations, changedByFolding) }.toMap() + val missing = differences.filterNot { it in equivalents } + val complete = missing.isEmpty() && gaps.isEmpty() + val justification = if (complete) equivalents.map { (identity, expression) -> + "BASE and HEAD $identity have identical declarations and total, effect-free return expressions: $expression" + } else missing.map { "No complete relational proof for $it" } + gaps.map { "Unresolved analysis obligation: $it" } + val whole = SafetyEvidence(SafetyClaim.WHOLE_CHANGE_EQUIVALENT, + if (complete) SafetyStatus.PROVEN else SafetyStatus.UNPROVEN, + differences.joinToString(), justification, properties.flatMap { it.assumptions }.distinct()) + return ChangeAssessment(ControlClassification.UNCONTROLLED, properties + whole, + if (complete) AcceptanceDecision.ACCEPTED else AcceptanceDecision.REJECTED, + if (complete) AcceptanceBasis.STATIC_SAFETY else AcceptanceBasis.INCOMPLETE_PROOF) + } + + private fun equivalent(identity: String, unchangedDeclarations: Set, changedByFolding: Set): Pair? { + val before = base[identity] + val after = head[identity] + if (before == null || after == null || identity !in unchangedDeclarations) return null + val expression = before.semanticKey + val untouched = (before.dependencies + after.dependencies + identity).none { it in changedByFolding } + val total = untouched && listOf(before, after).all { summary -> + listOf(summary.effectFree, summary.nonThrowing, summary.terminating).all { it.status == ProofStatus.PROVEN } + } + return if (expression != null && expression == after.semanticKey && total) identity to expression else null + } + + private fun evidence(subject: String, summary: MethodSafetySummary): List = listOf( + property(subject, SafetyClaim.EFFECT_FREE, summary.effectFree), + property(subject, SafetyClaim.NONTHROWING, summary.nonThrowing), + property(subject, SafetyClaim.TERMINATING, summary.terminating), + ) + + private fun property(subject: String, claim: SafetyClaim, proof: PropertyProof) = + SafetyEvidence(claim, SafetyStatus.valueOf(proof.status.name), subject, proof.evidence, proof.assumptions) +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/BytecodeInventory.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/BytecodeInventory.kt index 4b2c4097..1787edb6 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/BytecodeInventory.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/BytecodeInventory.kt @@ -60,6 +60,9 @@ internal class BytecodeInventory( val node = ClassNode() val flags = ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES ClassReader(bytes).accept(node, flags) + // javac may reorder these ownership records when a method starts referencing a nested class. + // Preserve every record and its flags; table order is not a declaration change. + node.innerClasses.sortBy { it.name } disposablePrimitiveMethods.addAll(disposablePrimitiveMethods(node)) initialization.add(node) val name = node.name.replace('/', '.') diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFiles.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFiles.kt new file mode 100644 index 00000000..a967af59 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFiles.kt @@ -0,0 +1,56 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import java.nio.file.Files +import java.nio.file.Path +import java.util.zip.ZipFile +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodNode + +/** Packaged application bytes and declared dependencies only; never host classpath fallback. */ +internal class SafetyClassFiles(artifact: Path, private val dependencies: List) : AutoCloseable { + private val archives = mutableMapOf() + private val classes = mutableMapOf() + val applications: Map = readApplication(artifact) + + fun find(owner: String): ClassNode? = if (owner in applications) applications.getValue(owner) else classes.getOrPut(owner) { + dependencies.firstNotNullOfOrNull { path -> bytes(path, "$owner.class")?.let(::decode) } + } + + fun method(owner: String, name: String, descriptor: String, seen: Set = emptySet()): Pair? { + val type = if (owner in seen) null else find(owner) + if (type == null) return null + val found = type.methods.singleOrNull { it.name == name && it.desc == descriptor } + return if (found != null) type to found else type.superName?.let { method(it, name, descriptor, seen + owner) } + } + + fun supportsContract(owner: String): Boolean = when { + owner in applications -> false + owner.startsWith("java/") -> find(owner) == null + owner == "kotlin/jvm/internal/Intrinsics" -> + EngineRuntimeClasses.matches(owner, dependencies.mapNotNull { bytes(it, "$owner.class") }) + else -> false + } + + private fun readApplication(artifact: Path): Map { + val bodies = if (Files.isDirectory(artifact)) Files.walk(artifact).use { paths -> + paths.filter { Files.isRegularFile(it) && it.toString().endsWith(".class") }.map { Files.readAllBytes(it) }.toList() + } else archive(artifact).entries().asSequence().filter { !it.isDirectory && it.name.endsWith(".class") } + .map { archive(artifact).getInputStream(it).use { input -> input.readBytes() } }.toList() + return bodies.map(::decode).associateBy { it.name } + } + + private fun bytes(path: Path, name: String): ByteArray? = if (Files.isDirectory(path)) { + path.resolve(name).takeIf(Files::isRegularFile)?.let(Files::readAllBytes) + } else archiveBytes(path, name) + + private fun archiveBytes(path: Path, name: String): ByteArray? { + val zip = archive(path) + val entry = zip.getEntry(name) + return entry?.let { zip.getInputStream(it).use { input -> input.readBytes() } } + } + + private fun archive(path: Path) = archives.getOrPut(path) { ZipFile(path.toFile()) } + private fun decode(bytes: ByteArray) = ClassNode().also { ClassReader(bytes).accept(it, ClassReader.SKIP_FRAMES) } + override fun close() { archives.values.forEach(ZipFile::close) } +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyCondition.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyCondition.kt new file mode 100644 index 00000000..f5316850 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyCondition.kt @@ -0,0 +1,98 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import org.objectweb.asm.Opcodes + +internal data class SafetyCondition(val opcode: Int, val left: SafetyValue, val right: SafetyValue) { + private val equality get() = opcode in setOf(Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IF_ICMPEQ, Opcodes.IF_ICMPNE, + Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE, Opcodes.IFNULL, Opcodes.IFNONNULL) + private val negated get() = opcode in setOf(Opcodes.IFNE, Opcodes.IF_ICMPNE, Opcodes.IF_ACMPNE, Opcodes.IFNONNULL) + private val reference get() = opcode in setOf(Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE, Opcodes.IFNULL, Opcodes.IFNONNULL) + val stable get() = left.stable && right.stable + val key: String get() = if (equality) "eq(${listOf(left.expression, right.expression).sorted().joinToString(",")})" + else "cmp:$opcode(${left.expression},${right.expression})" + + fun known(): Boolean? = if (equality) equalKnown()?.let { if (negated) !it else it } + else left.integer?.let { a -> right.integer?.let { b -> compare(a, b) } } + + private fun equalKnown(): Boolean? = when { + left.expression == right.expression -> true + reference && left.nullness == ReturnNullness.NULL -> nullableEquality(right.nullness) + reference && right.nullness == ReturnNullness.NULL -> nullableEquality(left.nullness) + left.integer != null && right.integer != null -> left.integer == right.integer + else -> null + } + + fun refine(original: SafetyState, taken: Boolean): SafetyState? { + val state = original.fork() + val equal = taken != negated + return when { + reference -> refineReference(state, equal) + equality -> refineInteger(state, equal) + else -> state.also { it.feasible = false } + } + } + + private fun refineReference(state: SafetyState, equal: Boolean): SafetyState { + val subject = when { + left.nullness == ReturnNullness.NULL -> right + right.nullness == ReturnNullness.NULL -> left + else -> null + } + if (subject != null) { + state.nullFacts[subject.expression] = if (equal) ReturnNullness.NULL else ReturnNullness.NON_NULL + if (!subject.expression.startsWith("arg:")) state.feasible = false + } else state.feasible = false + return state + } + + private fun refineInteger(state: SafetyState, equal: Boolean): SafetyState? { + val subject = if (left.integer == null && right.integer != null) left else right + val constant = left.integer ?: right.integer + if (constant == null) state.feasible = false + else if (equal) { + if (constant in state.excludedIntegers[subject.expression].orEmpty()) return null + state.integerFacts[subject.expression] = constant + } else state.excludedIntegers[subject.expression] = state.excludedIntegers[subject.expression].orEmpty() + constant + if (!subject.expression.startsWith("arg:")) state.feasible = false + return state + } + + fun combine(taken: SafetyEvaluation?, otherwise: SafetyEvaluation?): SafetyEvaluation = when { + taken == null -> requireNotNull(otherwise) + otherwise == null -> taken + else -> SafetyEvaluation(combineValues(taken.value, otherwise.value), taken.facts.merge(otherwise.facts)) + } + + private fun combineValues(first: SafetyValue?, second: SafetyValue?): SafetyValue? = when { + first == null || second == null -> null + first.expression == second.expression && first.type == second.type -> joinedValue(first, second) + else -> { + val positive = if (negated) second else first + val negative = if (negated) first else second + joinedValue(first, second).copy(expression = "if($key,${positive.expression},${negative.expression})", + integer = null, string = null, fresh = false, boxed = null, stable = stable && first.stable && second.stable) + } + } + + private fun compare(a: Int, b: Int): Boolean? = when (opcode) { + Opcodes.IFLT, Opcodes.IF_ICMPLT -> a < b + Opcodes.IFLE, Opcodes.IF_ICMPLE -> a <= b + Opcodes.IFGT, Opcodes.IF_ICMPGT -> a > b + Opcodes.IFGE, Opcodes.IF_ICMPGE -> a >= b + else -> null + } + + private fun nullableEquality(value: ReturnNullness): Boolean? = when (value) { + ReturnNullness.NULL -> true + ReturnNullness.NON_NULL -> false + else -> null + } +} + +private fun joinedValue(first: SafetyValue, second: SafetyValue): SafetyValue = first.copy( + stable = first.stable && second.stable, + nullness = if (first.nullness == second.nullness) first.nullness else ReturnNullness.UNKNOWN, + integer = first.integer.takeIf { it == second.integer }, string = first.string.takeIf { it == second.string }, + exactType = first.exactType.takeIf { it == second.exactType }, boxed = first.boxed.takeIf { it == second.boxed }, +) diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContracts.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContracts.kt new file mode 100644 index 00000000..654f0e45 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContracts.kt @@ -0,0 +1,72 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.MethodInsnNode + +/** Explicit JVM library contracts, never inferred from names or nullability annotations. */ +internal fun safetyContract(call: MethodInsnNode, receiver: SafetyValue?, arguments: List): SafetyEvaluation? { + val signature = "${call.owner}.${call.name}${call.desc}" + return when { + signature == "java/lang/Object.()V" && call.opcode == Opcodes.INVOKESPECIAL -> + SafetyEvaluation(SafetyValue.VOID, SafetyFacts()) + call.owner == "java/lang/String" && receiver != null -> stringContract(call, receiver, arguments) + signature in UNBOXING && receiver != null -> SafetyEvaluation(receiver.boxed ?: SafetyValue( + "unbox:$signature(${receiver.expression})", Type.getReturnType(call.desc), stable = receiver.stable), SafetyFacts()) + signature in BOXING && call.opcode == Opcodes.INVOKESTATIC -> SafetyEvaluation(SafetyValue( + "box:$signature(${arguments.single().expression})", Type.getReturnType(call.desc), ReturnNullness.NON_NULL, + exactType = call.owner, stable = arguments.single().stable, boxed = arguments.single()), SafetyFacts()) + signature in KOTLIN_NULL_CHECKS && call.opcode == Opcodes.INVOKESTATIC -> nullCheck(arguments.first(), signature) + else -> null + } +} + +private fun stringContract(call: MethodInsnNode, receiver: SafetyValue, arguments: List): SafetyEvaluation? { + val value = when (call.name + call.desc) { + "length()I" -> if (receiver.string != null) SafetyValue.integer(receiver.string.length) + else SafetyValue("strlen(${receiver.expression})", Type.INT_TYPE, stable = receiver.stable) + "equals(Ljava/lang/Object;)Z" -> stringEquality(receiver, arguments.single()) + else -> null + } + return value?.let { SafetyEvaluation(it, SafetyFacts()) } +} + +private fun stringEquality(receiver: SafetyValue, argument: SafetyValue): SafetyValue { + val constant = when { + argument.nullness == ReturnNullness.NULL -> false + receiver.string != null && argument.string != null -> receiver.string == argument.string + argument.exactType != null && argument.exactType != "java/lang/String" -> false + else -> null + } + return if (constant != null) SafetyValue.integer(if (constant) 1 else 0) + else SafetyValue("stringEquals(${receiver.expression},${argument.expression})", Type.INT_TYPE, + stable = receiver.stable && argument.stable) +} + +private fun nullCheck(value: SafetyValue, identity: String): SafetyEvaluation { + val proof = when (value.nullness) { + ReturnNullness.NON_NULL -> SafetyFacts.proved("$identity receives a statically non-null argument") + ReturnNullness.NULL -> SafetyFacts.refuted("$identity receives null") + else -> SafetyFacts.unproved("$identity argument nullness is not established") + } + return SafetyEvaluation(if (value.nullness == ReturnNullness.NULL) null else SafetyValue.VOID, SafetyFacts(exceptions = proof)) +} + +private val UNBOXING = setOf( + "java/lang/Integer.intValue()I", "java/lang/Boolean.booleanValue()Z", "java/lang/Byte.byteValue()B", + "java/lang/Short.shortValue()S", "java/lang/Character.charValue()C", "java/lang/Long.longValue()J", + "java/lang/Float.floatValue()F", "java/lang/Double.doubleValue()D", +) +private val BOXING = setOf( + "java/lang/Integer.valueOf(I)Ljava/lang/Integer;", "java/lang/Boolean.valueOf(Z)Ljava/lang/Boolean;", + "java/lang/Byte.valueOf(B)Ljava/lang/Byte;", "java/lang/Short.valueOf(S)Ljava/lang/Short;", + "java/lang/Character.valueOf(C)Ljava/lang/Character;", "java/lang/Long.valueOf(J)Ljava/lang/Long;", + "java/lang/Float.valueOf(F)Ljava/lang/Float;", "java/lang/Double.valueOf(D)Ljava/lang/Double;", +) +private val KOTLIN_NULL_CHECKS = setOf( + "kotlin/jvm/internal/Intrinsics.checkNotNullParameter(Ljava/lang/Object;Ljava/lang/String;)V", + "kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue(Ljava/lang/Object;Ljava/lang/String;)V", + "kotlin/jvm/internal/Intrinsics.checkNotNull(Ljava/lang/Object;)V", + "kotlin/jvm/internal/Intrinsics.checkNotNull(Ljava/lang/Object;Ljava/lang/String;)V", +) diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyIntegerOperation.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyIntegerOperation.kt new file mode 100644 index 00000000..cb33f3ef --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyIntegerOperation.kt @@ -0,0 +1,47 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type + +internal enum class SafetyIntegerOperation(val opcode: Int, val symbol: String) { + ADD(Opcodes.IADD, "iadd"), SUBTRACT(Opcodes.ISUB, "isub"), MULTIPLY(Opcodes.IMUL, "imul"), + DIVIDE(Opcodes.IDIV, "idiv"), REMAINDER(Opcodes.IREM, "irem"), AND(Opcodes.IAND, "iand"), + OR(Opcodes.IOR, "ior"), XOR(Opcodes.IXOR, "ixor"), SHIFT_LEFT(Opcodes.ISHL, "ishl"), + SHIFT_RIGHT(Opcodes.ISHR, "ishr"), UNSIGNED_SHIFT_RIGHT(Opcodes.IUSHR, "iushr"); + + fun evaluate(left: SafetyValue, right: SafetyValue): SafetyValue { + val a = left.integer + val b = right.integer + val result = if (a != null && b != null) integerConstant(this, a, b) else null + if (result != null) return SafetyValue.integer(result) + val identity = integerIdentity(opcode, left, right) + return if (identity != null) identity else + SafetyValue("$symbol(${left.expression},${right.expression})", Type.INT_TYPE, stable = left.stable && right.stable) + } +} + +private fun integerIdentity(opcode: Int, left: SafetyValue, right: SafetyValue): SafetyValue? = when (opcode) { + Opcodes.IADD -> when { left.integer == 0 -> right; right.integer == 0 -> left; else -> null } + Opcodes.ISUB -> left.takeIf { right.integer == 0 } + Opcodes.IMUL -> when { left.integer == 1 -> right; right.integer == 1 -> left; else -> null } + else -> null +} + +private fun integerConstant(operation: SafetyIntegerOperation, left: Int, right: Int): Int? = when (operation) { + SafetyIntegerOperation.ADD -> left + right + SafetyIntegerOperation.SUBTRACT -> left - right + SafetyIntegerOperation.MULTIPLY -> left * right + SafetyIntegerOperation.DIVIDE -> divide(left, right, false) + SafetyIntegerOperation.REMAINDER -> divide(left, right, true) + SafetyIntegerOperation.AND -> left and right + SafetyIntegerOperation.OR -> left or right + SafetyIntegerOperation.XOR -> left xor right + SafetyIntegerOperation.SHIFT_LEFT -> left shl right + SafetyIntegerOperation.SHIFT_RIGHT -> left shr right + SafetyIntegerOperation.UNSIGNED_SHIFT_RIGHT -> left ushr right +} + +internal val INTEGER_OPERATIONS = SafetyIntegerOperation.entries.associateBy { it.opcode } + +private fun divide(left: Int, right: Int, remainder: Boolean): Int? = + if (right == 0) null else if (remainder) left % right else left / right diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyInterpreter.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyInterpreter.kt new file mode 100644 index 00000000..63b7ee5e --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyInterpreter.kt @@ -0,0 +1,269 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.IincInsnNode +import org.objectweb.asm.tree.InsnNode +import org.objectweb.asm.tree.IntInsnNode +import org.objectweb.asm.tree.JumpInsnNode +import org.objectweb.asm.tree.LabelNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.LookupSwitchInsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.TableSwitchInsnNode +import org.objectweb.asm.tree.TypeInsnNode +import org.objectweb.asm.tree.VarInsnNode + +/** Path-sensitive interpreter of original JVM instructions. Unsupported behavior never yields a semantic key. */ +internal class SafetyInterpreter( + private val analysis: StaticSafetyAnalysis, private val owner: ClassNode, private val method: MethodNode, + private val active: Set, +) { + private val instructions = method.instructions.toArray() + private val labels = instructions.withIndex().filter { it.value is LabelNode }.associate { it.value to it.index } + private val identity = identity(owner.name, method.name, method.desc) + private var remaining = MAX_STEPS + private val visitedInstructions = mutableSetOf() + + fun evaluate(receiver: SafetyValue?, arguments: List): SafetyEvaluation { + val locals = mutableMapOf() + var slot = 0 + if (receiver != null) locals[slot++] = receiver + arguments.forEach { locals[slot] = it; slot += it.type.size } + val result = run(SafetyState(locals), 0, emptySet()) + val residual = SafetyResidualBytecode(method).fingerprint(visitedInstructions) + return if (residual == null) result else result.copy(facts = result.facts.retainResidual(identity, residual)) + } + + private fun run(state: SafetyState, start: Int, previous: Set): SafetyEvaluation { + var index = start + var result: SafetyEvaluation? = null + val visited = previous.toMutableSet() + while (index < instructions.size && result == null) { + val instruction = instructions[index] + visitedInstructions.add(index) + if (--remaining < 0 || !visited.add(index)) { + result = state.unknown("$identity: loop or analysis budget prevents termination proof") + } else { + val next = advance(state, instruction, index, visited) + index = next.first + result = next.second + } + } + return result ?: state.unknown("$identity: no supported return") + } + + private fun advance( + state: SafetyState, instruction: AbstractInsnNode, index: Int, visited: Set, + ): Pair = when { + instruction.opcode < 0 -> index + 1 to null + instruction is JumpInsnNode -> if (instruction.opcode == Opcodes.GOTO) labels.getValue(instruction.label) to null + else index to branch(state, instruction, index, visited) + instruction is LookupSwitchInsnNode -> index to switch(state, instruction.keys, instruction.labels, instruction.dflt, visited) + instruction is TableSwitchInsnNode -> index to switch(state, + (instruction.min..instruction.max).toList(), instruction.labels, instruction.dflt, visited) + else -> index + 1 to step(state, instruction, index) + } + + private fun branch(state: SafetyState, instruction: JumpInsnNode, index: Int, visited: Set): SafetyEvaluation { + if (instruction.opcode !in BRANCH_OPERATIONS) return state.unknown("$identity: unsupported branch ${instruction.opcode}") + val unary = instruction.opcode in UNARY_INTEGER_BRANCHES || + instruction.opcode == Opcodes.IFNULL || instruction.opcode == Opcodes.IFNONNULL + val right = if (unary) { + if (instruction.opcode == Opcodes.IFNULL || instruction.opcode == Opcodes.IFNONNULL) SafetyValue.NULL + else SafetyValue.integer(0) + } else state.pop() + val condition = SafetyCondition(instruction.opcode, state.pop(), right) + val target = labels.getValue(instruction.label) + val known = condition.known() + return if (known != null) run(state, if (known) target else index + 1, visited) + else condition.combine(condition.refine(state, true)?.let { run(it, target, visited) }, + condition.refine(state, false)?.let { run(it, index + 1, visited) }) + } + + private fun switch( + state: SafetyState, keys: List, targets: List, default: LabelNode, visited: Set, + ): SafetyEvaluation { + val selector = state.pop() + val value = selector.integer + if (value != null) return run(state, labels.getValue(targets.getOrNull(keys.indexOf(value)) ?: default), visited) + var otherwise = state.fork() + val cases = keys.mapIndexed { index, key -> + val condition = SafetyCondition(Opcodes.IF_ICMPEQ, selector, SafetyValue.integer(key)) + val accepted = condition.refine(otherwise, true)?.let { run(it, labels.getValue(targets[index]), visited) } + otherwise = otherwise.excludingInteger(selector, key) + condition to accepted + } + return cases.asReversed().fold(run(otherwise, labels.getValue(default), visited)) { result, (condition, accepted) -> + condition.combine(accepted, result) + } + } + + private fun step(state: SafetyState, instruction: AbstractInsnNode, index: Int): SafetyEvaluation? = when (instruction) { + is VarInsnNode -> variable(state, instruction) + is IincInsnNode -> { + val value = state.locals[instruction.`var`] + if (value == null) state.unknown("$identity: undefined local") else { + state.locals[instruction.`var`] = SafetyIntegerOperation.ADD.evaluate(state.resolve(value), + SafetyValue.integer(instruction.incr)) + null + } + } + is IntInsnNode -> if (instruction.opcode == Opcodes.BIPUSH || instruction.opcode == Opcodes.SIPUSH) { + state.push(SafetyValue.integer(instruction.operand)); null + } else state.unknown("$identity: unsupported allocation opcode ${instruction.opcode}") + is LdcInsnNode -> constant(state, instruction.cst) + is MethodInsnNode -> call(state, instruction, index) + is FieldInsnNode -> field(state, instruction, index) + is TypeInsnNode -> type(state, instruction, index) + is InsnNode -> simple(state, instruction.opcode) + else -> state.unknown("$identity: unsupported instruction ${instruction.opcode}") + } + + private fun variable(state: SafetyState, instruction: VarInsnNode): SafetyEvaluation? = when (instruction.opcode) { + Opcodes.ILOAD, Opcodes.LLOAD, Opcodes.FLOAD, Opcodes.DLOAD, Opcodes.ALOAD -> { + val value = state.locals[instruction.`var`] + if (value == null) state.unknown("$identity: undefined local") else { state.push(value); null } + } + Opcodes.ISTORE, Opcodes.LSTORE, Opcodes.FSTORE, Opcodes.DSTORE, Opcodes.ASTORE -> { + state.locals[instruction.`var`] = state.pop(); null + } + else -> state.unknown("$identity: unsupported local instruction") + } + + private fun constant(state: SafetyState, value: Any): SafetyEvaluation? { + val constant = when (value) { + is Int -> SafetyValue.integer(value) + is String -> SafetyValue.string(value) + is Long -> SafetyValue("long:$value", Type.LONG_TYPE) + is Float -> SafetyValue("float:${value.toRawBits()}", Type.FLOAT_TYPE) + is Double -> SafetyValue("double:${value.toRawBits()}", Type.DOUBLE_TYPE) + else -> null + } + return if (constant == null) state.unknown("$identity: unsupported constant") else { state.push(constant); null } + } + + private fun call(state: SafetyState, call: MethodInsnNode, index: Int): SafetyEvaluation? { + val arguments = Type.getArgumentTypes(call.desc).map { state.pop() }.asReversed() + val receiver = if (call.opcode == Opcodes.INVOKESTATIC) null else state.pop() + if (receiver != null && !state.receiver(receiver, "$identity@$index")) return SafetyEvaluation(null, state.facts) + val result = analysis.invoke(call, receiver, arguments, owner.name, active) + state.facts = state.facts.merge(result.facts.conditional(state.feasible)) + if (result.facts.exceptions.status != ProofStatus.PROVEN) state.feasible = false + return if (result.value == null) SafetyEvaluation(null, state.facts) else { + if (Type.getReturnType(call.desc).sort != Type.VOID) state.push(result.value) + null + } + } + + private fun field(state: SafetyState, field: FieldInsnNode, index: Int): SafetyEvaluation? { + val declaration = analysis.field(field.owner, field.name, field.desc) + ?: return state.unknown("$identity: missing field declaration ${field.owner}.${field.name}") + val static = field.opcode == Opcodes.GETSTATIC || field.opcode == Opcodes.PUTSTATIC + val write = field.opcode == Opcodes.PUTFIELD || field.opcode == Opcodes.PUTSTATIC + if (static && field.owner != owner.name) initialize(state, field.owner) + if (write) state.pop() + val receiver = if (static) null else state.pop() + return if (receiver != null && !state.receiver(receiver, "$identity@$index")) SafetyEvaluation(null, state.facts) else { + if (declaration.access and Opcodes.ACC_VOLATILE != 0) state.effect("$identity@$index: volatile field access") + if (write) { + if (receiver?.fresh != true) state.effect("$identity@$index: write to externally observable state") + } else state.push(SafetyValue.unknown(Type.getType(field.desc), analysis.readIdentity("$identity@$index"))) + null + } + } + + private fun initialize(state: SafetyState, initializedOwner: String) { + val initialized = analysis.initialization(initializedOwner, active) + state.facts = state.facts.merge(initialized.conditional(state.feasible)) + if (initialized.exceptions.status != ProofStatus.PROVEN) state.feasible = false + } + + private fun type(state: SafetyState, instruction: TypeInsnNode, index: Int): SafetyEvaluation? = when (instruction.opcode) { + Opcodes.NEW -> { + initialize(state, instruction.desc) + state.facts = state.facts.merge(SafetyFacts( + effects = SafetyFacts.unproved("$identity@$index: allocation lifetime and escape closure are not established"))) + state.push(SafetyValue(analysis.readIdentity("new:$identity@$index"), Type.getObjectType(instruction.desc), + ReturnNullness.NON_NULL, exactType = instruction.desc, stable = false, fresh = true)); null + } + Opcodes.CHECKCAST -> { + val value = state.pop() + val safe = value.nullness == ReturnNullness.NULL || value.exactType == instruction.desc || + instruction.desc == "java/lang/Object" + if (!safe) state.exception("$identity@$index: cast target is not established") + state.push(value.copy(type = Type.getObjectType(instruction.desc))); null + } + Opcodes.INSTANCEOF -> { + val value = state.pop() + val answer = when { value.nullness == ReturnNullness.NULL -> 0; value.exactType == instruction.desc -> 1; else -> null } + val result = if (answer != null) SafetyValue.integer(answer) + else SafetyValue("instanceof:${instruction.desc}(${value.expression})", Type.INT_TYPE, stable = value.stable) + state.push(result); null + } + else -> state.unknown("$identity: unsupported type operation ${instruction.opcode}") + } + + private fun simple(state: SafetyState, opcode: Int): SafetyEvaluation? = when (opcode) { + Opcodes.IRETURN, Opcodes.LRETURN, Opcodes.FRETURN, Opcodes.DRETURN, Opcodes.ARETURN -> SafetyEvaluation(state.pop(), state.facts) + Opcodes.RETURN -> SafetyEvaluation(SafetyValue.VOID, state.facts) + Opcodes.ATHROW -> { state.pop(); state.exception("$identity: explicit throw", true); SafetyEvaluation(null, state.facts) } + Opcodes.ARRAYLENGTH -> { + val array = state.pop() + if (!state.receiver(array, identity)) SafetyEvaluation(null, state.facts) + else { state.push(SafetyValue("length(${array.expression})", Type.INT_TYPE, stable = array.stable)); null } + } + Opcodes.INEG -> { + val value = state.pop() + val result = if (value.integer != null) SafetyValue.integer(-value.integer) + else SafetyValue("ineg(${value.expression})", Type.INT_TYPE, stable = value.stable) + state.push(result); null + } + else -> { + val operation = INTEGER_OPERATIONS[opcode] + if (operation == null) stackOperation(state, opcode) else arithmetic(state, operation) + } + } + + private fun stackOperation(state: SafetyState, opcode: Int): SafetyEvaluation? = when (opcode) { + Opcodes.NOP -> null + Opcodes.ACONST_NULL -> { state.push(SafetyValue.NULL); null } + Opcodes.ICONST_M1, Opcodes.ICONST_0, Opcodes.ICONST_1, Opcodes.ICONST_2, + Opcodes.ICONST_3, Opcodes.ICONST_4, Opcodes.ICONST_5 -> { state.push(SafetyValue.integer(opcode - Opcodes.ICONST_0)); null } + Opcodes.POP -> { state.pop(); null } + Opcodes.DUP -> { state.push(state.stack.last()); null } + Opcodes.SWAP -> { val first = state.pop(); val second = state.pop(); state.push(first); state.push(second); null } + Opcodes.LCONST_0, Opcodes.LCONST_1 -> constant(state, (opcode - Opcodes.LCONST_0).toLong()) + Opcodes.FCONST_0, Opcodes.FCONST_1, Opcodes.FCONST_2 -> constant(state, (opcode - Opcodes.FCONST_0).toFloat()) + Opcodes.DCONST_0, Opcodes.DCONST_1 -> constant(state, (opcode - Opcodes.DCONST_0).toDouble()) + else -> state.unknown("$identity: unsupported opcode $opcode") + } + + private fun arithmetic(state: SafetyState, operation: SafetyIntegerOperation): SafetyEvaluation? { + val right = state.pop() + val left = state.pop() + if (operation == SafetyIntegerOperation.DIVIDE || operation == SafetyIntegerOperation.REMAINDER) { + val knownZero = right.integer == 0 + val nonzero = if (right.integer != null) !knownZero + else 0 in state.excludedIntegers[right.expression].orEmpty() + if (!nonzero) state.exception("$identity: divisor may be zero", knownZero) + if (knownZero) return SafetyEvaluation(null, state.facts) + } + state.push(operation.evaluate(left, right)) + return null + } + + private companion object { + const val MAX_STEPS = 10000 + val UNARY_INTEGER_BRANCHES = setOf(Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFLE, Opcodes.IFGT, Opcodes.IFGE) + val BRANCH_OPERATIONS = UNARY_INTEGER_BRANCHES + setOf(Opcodes.IF_ICMPEQ, Opcodes.IF_ICMPNE, Opcodes.IF_ICMPLT, + Opcodes.IF_ICMPLE, Opcodes.IF_ICMPGT, Opcodes.IF_ICMPGE, Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE, + Opcodes.IFNULL, Opcodes.IFNONNULL) + } +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecode.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecode.kt new file mode 100644 index 00000000..d2ae1519 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecode.kt @@ -0,0 +1,94 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import java.security.MessageDigest +import org.objectweb.asm.ConstantDynamic +import org.objectweb.asm.Handle +import org.objectweb.asm.Type +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.IincInsnNode +import org.objectweb.asm.tree.IntInsnNode +import org.objectweb.asm.tree.InvokeDynamicInsnNode +import org.objectweb.asm.tree.JumpInsnNode +import org.objectweb.asm.tree.LabelNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.LookupSwitchInsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.MultiANewArrayInsnNode +import org.objectweb.asm.tree.TableSwitchInsnNode +import org.objectweb.asm.tree.TypeInsnNode +import org.objectweb.asm.tree.VarInsnNode + +/** Unselected production instructions never disappear because a path proof did not traverse them. */ +internal class SafetyResidualBytecode(private val method: MethodNode) { + private val all = method.instructions.toArray() + private val code = all.filter { it.opcode >= 0 } + private val ordinals = code.withIndex().associate { it.value to it.index } + private val leaders = (listOf(0) + code.flatMap { instruction -> + val next = ordinals.getValue(instruction) + 1 + when (instruction) { + is JumpInsnNode -> listOf(target(instruction.label), next) + is LookupSwitchInsnNode -> (instruction.labels + instruction.dflt).map(::target) + next + is TableSwitchInsnNode -> (instruction.labels + instruction.dflt).map(::target) + next + else -> if (instruction.opcode in RETURN_OR_THROW) listOf(next) else emptyList() + } + }).distinct().sorted() + + fun fingerprint(visited: Set): String? { + val residual = all.withIndex().filter { it.value.opcode >= 0 && it.index !in visited }.map { (_, instruction) -> + encode(listOf(block(ordinals.getValue(instruction)), instruction.opcode, operands(instruction))) + } + return residual.takeIf { it.isNotEmpty() }?.let { + val bytes = encode(it).toByteArray(Charsets.UTF_8) + val hash = MessageDigest.getInstance("SHA-256").digest(bytes).joinToString("") { byte -> "%02x".format(byte) } + "${it.size}:$hash" + } + } + + private fun operands(instruction: AbstractInsnNode): Any? = when (instruction) { + is IntInsnNode -> instruction.operand + is VarInsnNode -> instruction.`var` + is TypeInsnNode -> instruction.desc + is FieldInsnNode -> listOf(instruction.owner, instruction.name, instruction.desc) + is MethodInsnNode -> listOf(instruction.owner, instruction.name, instruction.desc, instruction.itf) + is InvokeDynamicInsnNode -> listOf(instruction.name, instruction.desc, instruction.bsm, instruction.bsmArgs.toList()) + is JumpInsnNode -> block(target(instruction.label)) + is LdcInsnNode -> instruction.cst + is IincInsnNode -> listOf(instruction.`var`, instruction.incr) + is TableSwitchInsnNode -> listOf(instruction.min, instruction.max, block(target(instruction.dflt)), + instruction.labels.map { block(target(it)) }) + is LookupSwitchInsnNode -> listOf(instruction.keys, block(target(instruction.dflt)), instruction.labels.map { block(target(it)) }) + is MultiANewArrayInsnNode -> listOf(instruction.desc, instruction.dims) + else -> null + } + + private fun target(label: LabelNode): Int { + var next: AbstractInsnNode? = label + while (next != null && next.opcode < 0) next = next.next + return next?.let(ordinals::getValue) ?: code.size + } + + private fun block(ordinal: Int): Int = leaders.indexOfLast { it <= ordinal } + + private fun encode(value: Any?): String { + val body = when (value) { + null -> "null" + is List<*> -> value.joinToString("") { encode(it) } + is Type -> "type:${value.descriptor}" + is Handle -> encode(listOf(value.tag, value.owner, value.name, value.desc, value.isInterface)) + is ConstantDynamic -> encode(listOf(value.name, value.descriptor, value.bootstrapMethod, + (0 until value.bootstrapMethodArgumentCount).map(value::getBootstrapMethodArgument))) + is Float -> "float:${value.toRawBits()}" + is Double -> "double:${value.toRawBits()}" + else -> "${value.javaClass.name}:$value" + } + return "${body.length}:$body" + } + + private companion object { + val RETURN_OR_THROW = setOf(org.objectweb.asm.Opcodes.IRETURN, org.objectweb.asm.Opcodes.LRETURN, + org.objectweb.asm.Opcodes.FRETURN, org.objectweb.asm.Opcodes.DRETURN, org.objectweb.asm.Opcodes.ARETURN, + org.objectweb.asm.Opcodes.RETURN, org.objectweb.asm.Opcodes.ATHROW) + } +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyValues.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyValues.kt new file mode 100644 index 00000000..528c205a --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyValues.kt @@ -0,0 +1,102 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.PropertyProof +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import org.objectweb.asm.Type + +internal data class SafetyValue( + val expression: String, val type: Type, val nullness: ReturnNullness = ReturnNullness.NOT_APPLICABLE, + val integer: Int? = null, val string: String? = null, val exactType: String? = null, + val stable: Boolean = true, val fresh: Boolean = false, val boxed: SafetyValue? = null, +) { + companion object { + fun integer(value: Int) = SafetyValue("int:$value", Type.INT_TYPE, integer = value) + fun string(value: String) = SafetyValue("string:${value.length}:$value", Type.getObjectType("java/lang/String"), + ReturnNullness.NON_NULL, string = value, exactType = "java/lang/String") + fun unknown(type: Type, identity: String, stable: Boolean = false) = SafetyValue(identity, type, + if (type.sort == Type.OBJECT || type.sort == Type.ARRAY) ReturnNullness.UNKNOWN else ReturnNullness.NOT_APPLICABLE, + stable = stable) + val NULL = SafetyValue("null", Type.getObjectType("java/lang/Object"), ReturnNullness.NULL) + val VOID = SafetyValue("void", Type.VOID_TYPE) + } +} + +internal data class SafetyFacts( + val effects: PropertyProof = proved("No externally visible effect in the supported instructions"), + val exceptions: PropertyProof = proved("All evaluated instructions are nonthrowing under the stated JVM model"), + val termination: PropertyProof = proved("Finite acyclic control flow and terminating resolved callees"), + val dependencies: Set = emptySet(), val residuals: Set = emptySet(), +) { + val normalCompletionProven: Boolean + get() = exceptions.status == ProofStatus.PROVEN && termination.status == ProofStatus.PROVEN + + fun merge(other: SafetyFacts) = SafetyFacts(combine(effects, other.effects), combine(exceptions, other.exceptions), + combine(termination, other.termination), dependencies + other.dependencies, residuals + other.residuals) + fun retainResidual(identity: String, fingerprint: String): SafetyFacts { + val evidence = "Unselected instructions outside the traversed paths are retained exactly: $identity [$fingerprint]" + fun property(value: PropertyProof) = value.copy(evidence = value.evidence + evidence) + return copy(effects = property(effects), exceptions = property(exceptions), termination = property(termination), + residuals = residuals + "$identity:$fingerprint") + } + fun conditional(feasible: Boolean): SafetyFacts { + fun property(value: PropertyProof) = if (!feasible && value.status == ProofStatus.REFUTED) + value.copy(status = ProofStatus.UNPROVEN) else value + return copy(effects = property(effects), exceptions = property(exceptions)) + } + fun unknown(reason: String) = merge(SafetyFacts(unproved(reason), unproved(reason), unproved(reason))) + companion object { + private const val JVM_MODEL = "Supplied classpath and ordinary JVM execution; VM resource exhaustion and linkage failures excluded" + fun proved(reason: String) = PropertyProof(ProofStatus.PROVEN, listOf(reason), listOf(JVM_MODEL)) + fun unproved(reason: String) = PropertyProof(ProofStatus.UNPROVEN, listOf(reason)) + fun refuted(reason: String) = PropertyProof(ProofStatus.REFUTED, listOf(reason)) + private fun combine(first: PropertyProof, second: PropertyProof): PropertyProof { + val status = when { + first.status == ProofStatus.REFUTED || second.status == ProofStatus.REFUTED -> ProofStatus.REFUTED + first.status == ProofStatus.UNPROVEN || second.status == ProofStatus.UNPROVEN -> ProofStatus.UNPROVEN + else -> ProofStatus.PROVEN + } + return PropertyProof(status, (first.evidence + second.evidence).distinct(), (first.assumptions + second.assumptions).distinct()) + } + } +} + +internal data class SafetyEvaluation(val value: SafetyValue?, val facts: SafetyFacts) { + fun semanticKey(): String? = value?.takeIf { + it.stable && listOf(facts.effects, facts.exceptions, facts.termination).all { property -> property.status == ProofStatus.PROVEN } + }?.let { "return:${it.type.descriptor}:${it.expression}|residuals:${facts.residuals.sorted()}" } +} + +internal data class SafetyState( + val locals: MutableMap, val stack: MutableList = mutableListOf(), + var facts: SafetyFacts = SafetyFacts(), val nullFacts: MutableMap = mutableMapOf(), + val integerFacts: MutableMap = mutableMapOf(), + val excludedIntegers: MutableMap> = mutableMapOf(), var feasible: Boolean = true, +) { + fun fork() = copy(locals = locals.toMutableMap(), stack = stack.toMutableList(), nullFacts = nullFacts.toMutableMap(), + integerFacts = integerFacts.toMutableMap(), excludedIntegers = excludedIntegers.toMutableMap()) + fun resolve(value: SafetyValue): SafetyValue = value.copy(nullness = nullFacts[value.expression] ?: value.nullness, + integer = integerFacts[value.expression] ?: value.integer) + fun pop(): SafetyValue = resolve(stack.removeAt(stack.lastIndex)) + fun push(value: SafetyValue) { stack.add(resolve(value)) } + fun unknown(reason: String): SafetyEvaluation = SafetyEvaluation(null, facts.unknown(reason)) + fun exception(reason: String, definite: Boolean = false) { + val proof = if (definite && feasible) SafetyFacts.refuted(reason) else SafetyFacts.unproved(reason) + facts = facts.merge(SafetyFacts(exceptions = proof)) + feasible = false // A later witness needs independently established normal completion of this operation. + } + fun receiver(value: SafetyValue, site: String): Boolean { + if (value.nullness != ReturnNullness.NON_NULL) exception("$site: receiver ${value.expression} may be null", + value.nullness == ReturnNullness.NULL) + return value.nullness != ReturnNullness.NULL + } + fun effect(reason: String) { + facts = facts.merge(SafetyFacts(effects = if (feasible) SafetyFacts.refuted(reason) else SafetyFacts.unproved(reason))) + } +} + +/** Switch defaults exclude a distinct constant; this cannot contradict an unknown selector's positive fact. */ +internal fun SafetyState.excludingInteger(selector: SafetyValue, key: Int): SafetyState = fork().also { result -> + result.excludedIntegers[selector.expression] = result.excludedIntegers[selector.expression].orEmpty() + key + if (!selector.expression.startsWith("arg:")) result.feasible = false +} diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysis.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysis.kt new file mode 100644 index 00000000..79683117 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysis.kt @@ -0,0 +1,116 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import java.nio.file.Path +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode + +/** Original, unfurled JVM semantics. No selected-control or runtime-reachability assumptions enter these summaries. */ +internal class StaticSafetyAnalysis(artifact: Path, dependencies: List) : AutoCloseable { + private val files = SafetyClassFiles(artifact, dependencies) + private var readSequence = 0 + internal fun readIdentity(site: String): String = "$site:read${readSequence++}" + + fun summaries(): Map = files.applications.values.flatMap { owner -> owner.methods.map { method -> + val identity = identity(owner.name, method.name, method.desc) + val arguments = Type.getArgumentTypes(method.desc).mapIndexed { index, type -> SafetyValue.unknown(type, "arg:$index", true) } + val receiver = if (method.access and Opcodes.ACC_STATIC == 0) SafetyValue("this", Type.getObjectType(owner.name), + ReturnNullness.NON_NULL, exactType = owner.name.takeIf { owner.access and Opcodes.ACC_FINAL != 0 }) else null + val result = evaluate(owner, method, receiver, arguments, emptySet()) + identity to MethodSafetySummary(identity, result.facts.effects, result.facts.exceptions, result.facts.termination, + result.semanticKey(), result.facts.dependencies, if (result.value == null) ReturnNullness.UNKNOWN else result.value.nullness) + } }.toMap() + + internal fun evaluate( + owner: ClassNode, method: MethodNode, receiver: SafetyValue?, arguments: List, active: Set, + ): SafetyEvaluation { + val identity = identity(owner.name, method.name, method.desc) + val unsupported = when { + identity in active || active.size >= MAX_CALL_DEPTH -> "Recursive or excessively deep call graph: $identity" + method.access and (Opcodes.ACC_NATIVE or Opcodes.ACC_ABSTRACT) != 0 -> "No inspectable method body: $identity" + method.tryCatchBlocks.isNotEmpty() -> "Exceptional control-flow handlers are unsupported: $identity" + method.access and Opcodes.ACC_SYNCHRONIZED != 0 -> "Synchronized method effects are unsupported: $identity" + else -> null + } + val result = if (unsupported != null) SafetyEvaluation(null, SafetyFacts().unknown(unsupported)) + else SafetyInterpreter(this, owner, method, active + identity).evaluate(receiver, arguments) + // Retain the inspected declaration, which may differ from the call site's symbolic owner. + return result.copy(facts = result.facts.copy(dependencies = result.facts.dependencies + identity)) + } + + internal fun invoke( + instruction: MethodInsnNode, receiver: SafetyValue?, arguments: List, caller: String, active: Set, + ): SafetyEvaluation { + val called = identity(instruction.owner, instruction.name, instruction.desc) + val contract = contract(instruction, receiver, arguments, called) + if (contract != null) return contract + val receiverType = receiver?.exactType + val lookupOwner = lookupOwner(instruction, receiverType) + val target = files.method(lookupOwner, instruction.name, instruction.desc) + val exact = target != null && (instruction.opcode == Opcodes.INVOKESTATIC || instruction.opcode == Opcodes.INVOKESPECIAL || + receiverType != null || target.first.access and Opcodes.ACC_FINAL != 0 || + target.second.access and (Opcodes.ACC_FINAL or Opcodes.ACC_PRIVATE) != 0) + val result = if (target == null || !exact) SafetyEvaluation(null, SafetyFacts().unknown("Unresolved call alternatives: $called")) + else { + val initialized = if (instruction.opcode == Opcodes.INVOKESTATIC && target.first.name != caller) + initialization(target.first.name, active) else SafetyFacts() + val evaluated = evaluate(target.first, target.second, receiver, arguments, active) + evaluated.copy(facts = initialized.merge(evaluated.facts.conditional(initialized.normalCompletionProven))) + } + return result.copy(facts = result.facts.copy(dependencies = result.facts.dependencies + called)) + } + + private fun lookupOwner(instruction: MethodInsnNode, receiverType: String?): String = when (instruction.opcode) { + Opcodes.INVOKEVIRTUAL, Opcodes.INVOKEINTERFACE -> + if (receiverType == null) instruction.owner else receiverType + else -> instruction.owner + } + + private fun contract( + instruction: MethodInsnNode, receiver: SafetyValue?, arguments: List, called: String, + ): SafetyEvaluation? { + val result = if (files.supportsContract(instruction.owner)) safetyContract(instruction, receiver, arguments) else null + return result?.let { proof -> + val value = proof.value?.let { if (it.boxed != null) + it.copy(expression = readIdentity("boxed:$called"), stable = false) else it } + proof.copy(value = value, facts = proof.facts.copy(dependencies = proof.facts.dependencies + called)) + } + } + + internal fun initialization(owner: String, active: Set): SafetyFacts { + val type = files.find(owner) + return when { + owner == "java/lang/Object" -> SafetyFacts() + "$owner." in active -> SafetyFacts().unknown("Cyclic initialization: $owner") + type == null -> SafetyFacts().unknown("Missing class initialization inventory: $owner") + else -> initialized(type, active) + } + } + + private fun initialized(type: ClassNode, active: Set): SafetyFacts { + val initializer = type.methods.singleOrNull { it.name == "" } + val own = if (initializer == null) SafetyFacts() else evaluate(type, initializer, null, emptyList(), active).facts + val inherited = if (type.superName == null) SafetyFacts().unknown("Missing superclass inventory: ${type.name}") + else initialization(type.superName, active + "${type.name}.") + val interfaces = if (type.interfaces.isEmpty()) SafetyFacts() + else SafetyFacts().unknown("Interface initialization closure: ${type.name}") + val prerequisites = inherited.merge(interfaces) + return prerequisites.merge(own.conditional(prerequisites.normalCompletionProven)) + } + + internal fun field(owner: String, name: String, descriptor: String): FieldNode? { + val declaration = files.find(owner) ?: return null + return declaration.fields.singleOrNull { it.name == name && it.desc == descriptor } + } + + override fun close() { files.close() } + + private companion object { const val MAX_CALL_DEPTH = 32 } +} + +internal fun identity(owner: String, name: String, descriptor: String): String = "${owner.replace('/', '.')}.$name$descriptor" diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/DisposableExposure.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/DisposableExposure.kt index 322440bd..f5874a65 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/DisposableExposure.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/DisposableExposure.kt @@ -19,6 +19,7 @@ import sootup.core.model.Body /** Erase only dead total computations after folding makes both conditional successors identical. */ internal fun disposableExposureStatements( body: Body, identity: String, primitiveMethods: Set, authorized: Set, + totalMethods: Set = emptySet(), ): Set { val removed = linkedSetOf() val producers = mutableSetOf() @@ -36,7 +37,7 @@ internal fun disposableExposureStatements( val redundant = exposureBranchKey(statement) in allowed && successors.size == 1 && totalExposureCondition(statement.asJIfStmt().condition) val assignment = unusedExposureAssignment(statement, retained, producers) - val disposable = assignment != null && disposableValue(assignment.rightOp, identity, primitiveMethods, body) + val disposable = assignment != null && disposableValue(assignment.rightOp, identity, primitiveMethods, totalMethods, body) val dispensable = redundant || disposable if (noException && successors.size == 1 && dispensable) { producers.addAll(statement.uses.filterIsInstance()) @@ -55,19 +56,24 @@ private fun unusedExposureAssignment(statement: Stmt, retained: List, prod return assignment.takeIf { it.leftOp is Local && it.leftOp in producers && !used } } -private fun disposableValue(value: Value, caller: String, primitiveMethods: Set, body: Body): Boolean { +private fun disposableValue( + value: Value, caller: String, primitiveMethods: Set, totalMethods: Set, body: Body, +): Boolean { val localValue = value is Local || value is StringConstant || value is NullConstant return localValue || totalPrimitive(value) || literalStringPredicate(value, body) || - disposablePrimitiveCall(value, caller, primitiveMethods) + disposableCall(value, caller, primitiveMethods, totalMethods) } -private fun disposablePrimitiveCall(value: Value, caller: String, primitiveMethods: Set): Boolean { +private fun disposableCall(value: Value, caller: String, primitiveMethods: Set, totalMethods: Set): Boolean { val call = value as? JStaticInvokeExpr ?: return false val signature = call.methodSignature val owner = caller.substringBefore('(').substringBeforeLast('.') val method = "${signature.declClassType.fullyQualifiedName}.${signature.name}" + jvmMethodDescriptor(signature.parameterTypes.map { it.toString() }, signature.type.toString()) - return signature.declClassType.fullyQualifiedName == owner && method in primitiveMethods && call.args.all(::totalPrimitive) + // Same-owner static calls introduce neither receiver dereferences nor a new class initialization. + val totalArguments = call.args.all { it is Local || it is StringConstant || it is NullConstant || totalPrimitive(it) } + val total = method in primitiveMethods && call.args.all(::totalPrimitive) || method in totalMethods && totalArguments + return signature.declClassType.fullyQualifiedName == owner && total } /** JVM reference identity tests neither dereference operands nor dispatch application equality methods. */ diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/MethodSnapshots.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/MethodSnapshots.kt index dd191947..ab0b0683 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/MethodSnapshots.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/bytecode/sootup/MethodSnapshots.kt @@ -17,7 +17,9 @@ internal class MethodSnapshots( private val dependencyResources: Boolean, private val trustedIntrinsics: Boolean, private val primitiveMethods: Set, + totalMethods: () -> Set, ) { + private val disposableMethods by lazy(totalMethods) val methods = linkedMapOf() val discardedCalls = linkedMapOf>() val resourceCalls = linkedMapOf>() @@ -32,7 +34,8 @@ internal class MethodSnapshots( resourceInitializers[identity] = body?.let(::dependencyResourceInitializers).orEmpty() } val disposable = body?.let { - disposableExposureStatements(it, identity, primitiveMethods, authorized) + disposableExposureStatements(it, identity, primitiveMethods, authorized, + if (authorized.isEmpty()) emptySet() else disposableMethods) }.orEmpty() val calls = body?.stmts.orEmpty().filterNot { it in disposable }.mapNotNull(::invokedIdentity).toSet() discardedCalls[identity] = disposable.mapNotNull(::invokedIdentity).toSet() - calls diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/GraphiteAnalyzer.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/GraphiteAnalyzer.kt index 97a99208..be1e29fb 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/GraphiteAnalyzer.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/GraphiteAnalyzer.kt @@ -4,6 +4,8 @@ import io.johnsonlee.graphfold.engine.bytecode.asm.LeafApplicationDispatch import io.johnsonlee.graphfold.engine.bytecode.asm.BytecodeInventory import io.johnsonlee.graphfold.engine.bytecode.asm.ResourceDeclarations import io.johnsonlee.graphfold.engine.bytecode.asm.inlineControlRemovedMethods +import io.johnsonlee.graphfold.engine.bytecode.asm.StaticSafetyAnalysis +import io.johnsonlee.graphfold.engine.safety.ProofStatus import io.johnsonlee.graphfold.engine.FoldReachability import io.johnsonlee.graphfold.engine.snapshot.MethodSnapshot @@ -64,7 +66,7 @@ class GraphiteAnalyzer internal constructor(private val onAudit: (Int, RuleAudit val resources = if (dependencyResources) ResourceDeclarations(listOf(artifact) + dependencies) else null return AnalysisInputs(artifact, dependencies).use { inputs -> val snapshot = analyzeInputs(inputs.path, rules, inventory, RuleAuditCapture(onAudit), resources, - AnalysisContext()) + AnalysisContext(totalMethods = { totalMethods(artifact, dependencies, it) })) snapshot } } @@ -112,7 +114,7 @@ class GraphiteAnalyzer internal constructor(private val onAudit: (Int, RuleAudit val snapshot = analyzeInputs(inputs.path, selected, inventory, audit, resources, AnalysisContext(exposure, reachability, if (unchangedOwners && project.catalog.diagnostics.isEmpty()) project.catalog.inlineEvidence else emptyList(), - project.sourceClasses)) + project.sourceClasses, { totalMethods(compiled.classes, dependencies, it) })) .copy(originalOwners = project.owners.entries.associate { (original, normalized) -> normalized to original }) snapshot } @@ -154,7 +156,7 @@ class GraphiteAnalyzer internal constructor(private val onAudit: (Int, RuleAudit } else rules val sink = SnapshotSink(if (resources == null) inventory.classes else resources.classes, resources != null, resources != null && resources.trustedIntrinsics, - inventory.disposablePrimitiveMethods, exposure) + inventory.disposablePrimitiveMethods, exposure, { context.totalMethods(audit.selectedCallees) }) val config = LoaderConfig( folding = resolved.takeIf { it.isNotEmpty() }?.let { FoldPlan(it) }, verbose = log, @@ -336,6 +338,7 @@ private fun requirePreparedClasses(classes: Path) { private data class AnalysisContext( val selection: ExposureProvenance? = null, val reachability: FoldReachability = FoldReachability(), val inlineEvidence: List = emptyList(), val inlineClasses: Path? = null, + val totalMethods: (Set) -> Set = { emptySet() }, ) { fun removedInlineMethods(snapshot: ProgramSnapshot, rules: List, applied: Map>): Set = if (inlineClasses != null && snapshot.diagnostics.isEmpty() && snapshot.methodDiagnostics.values.all { it.isEmpty() }) { @@ -343,6 +346,15 @@ private data class AnalysisContext( } else emptySet() } +/** General callee properties; authorization, argument evaluation and redundancy remain separate IR obligations. */ +private fun totalMethods(artifact: Path, dependencies: List, selected: Set): Set = + StaticSafetyAnalysis(artifact, dependencies).use { analysis -> + analysis.summaries().filterValues { summary -> + val independent = (summary.dependencies + summary.identity).none { it in selected } + independent && listOf(summary.effectFree, summary.nonThrowing, summary.terminating).all { it.status == ProofStatus.PROVEN } + }.keys + } + private fun OriginProject.requiresOriginAnalysis(): Boolean = owners.isNotEmpty() || methodDiagnostics.isNotEmpty() || catalog.inlineEvidence.isNotEmpty() diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/RuleAuditCapture.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/RuleAuditCapture.kt index 205143f2..bc51afd9 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/RuleAuditCapture.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/RuleAuditCapture.kt @@ -15,8 +15,10 @@ internal data class RuleAuditUpdate( /** Publish progress immediately, so later query/frontend failures cannot erase earlier selection evidence. */ internal class RuleAuditCapture(private val publish: (Int, RuleAuditUpdate) -> Unit) { private val records = mutableMapOf() + val selectedCallees = mutableSetOf() fun selected(index: Int, sites: List) { + selectedCallees.addAll(sites.map { it.callee.identity }) update(index, RuleAuditUpdate(RuleSelectionStatus.SELECTED, sites.distinctBy { it.id }.sortedBy { it.id.value }.map { SelectedCallSite(it.id.value, SelectedMethod(it.caller.signature, it.caller.descriptor), SelectedMethod(it.callee.signature, it.callee.descriptor), it.ordinal, it.origin?.value, it.lineNumber) diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/SnapshotSink.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/SnapshotSink.kt index db3b9656..9606ab53 100644 --- a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/SnapshotSink.kt +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/graphite/SnapshotSink.kt @@ -18,10 +18,11 @@ internal class SnapshotSink( private val trustedIntrinsics: Boolean = false, private val primitiveMethods: Set = emptySet(), private val exposure: ExposureProvenance? = null, + totalMethods: () -> Set = { emptySet() }, ) { val observations = linkedMapOf() val appliedRules = linkedMapOf>() - private val bodies = MethodSnapshots(applicationOwners, dependencyResources, trustedIntrinsics, primitiveMethods) + private val bodies = MethodSnapshots(applicationOwners, dependencyResources, trustedIntrinsics, primitiveMethods, totalMethods) val methods get() = bodies.methods val discardedCalls get() = bodies.discardedCalls val resourceCalls get() = bodies.resourceCalls diff --git a/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/safety/MethodSafetySummary.kt b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/safety/MethodSafetySummary.kt new file mode 100644 index 00000000..d7e79b19 --- /dev/null +++ b/engine/src/main/kotlin/io/johnsonlee/graphfold/engine/safety/MethodSafetySummary.kt @@ -0,0 +1,21 @@ +package io.johnsonlee.graphfold.engine.safety + +/** A failed proof is distinct from a statically demonstrated counterexample. */ +internal enum class ProofStatus { PROVEN, REFUTED, UNPROVEN } + +internal data class PropertyProof( + val status: ProofStatus, val evidence: List, val assumptions: List = emptyList(), +) + +internal enum class ReturnNullness { NULL, NON_NULL, UNKNOWN, NOT_APPLICABLE } + +/** Revision-local, input-derived proof. A semantic key is available only for a complete whole-method proof. */ +internal data class MethodSafetySummary( + val identity: String, + val effectFree: PropertyProof, + val nonThrowing: PropertyProof, + val terminating: PropertyProof, + val semanticKey: String?, + val dependencies: Set, + val returnNullness: ReturnNullness = ReturnNullness.UNKNOWN, +) diff --git a/engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json b/engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json index e1683997..de0cb7f4 100644 --- a/engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json +++ b/engine/src/main/resources/io/johnsonlee/graphfold/engine/verdict.schema.json @@ -1,8 +1,8 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "urn:io.johnsonlee.graphfold:verdict:1", - "title": "GraphFold analysis verdict (version 1)", - "description": "The AnalysisReports.json serialization contract, independent of output filename (verdict.json or report.json). Verdict is derived by the engine: PASS requires no findings, no blocking diagnostics, and all obligations proved. Cross-field verdict and count equality are enforced by the engine, not this structural schema.", + "$id": "urn:io.johnsonlee.graphfold:verdict:2", + "title": "GraphFold analysis verdict (version 2)", + "description": "The AnalysisReports.json serialization contract, independent of output filename (verdict.json or report.json). Verdict is derived by the engine: PASS requires no findings, no blocking diagnostics, no rejected assessments, and all obligations proved. Cross-field verdict and count equality are enforced by the engine, not this structural schema.", "type": "object", "additionalProperties": false, "required": [ @@ -17,8 +17,8 @@ "properties": { "schemaVersion": { "type": "integer", - "const": 1, - "description": "Version of the serialized verdict contract; version 1 corresponds to this schema. This is independent of the GraphFold release version." + "const": 2, + "description": "Version of the serialized verdict contract; version 2 corresponds to this schema. This is independent of the GraphFold release version." }, "verdict": { "type": "string", @@ -26,7 +26,7 @@ "PASS", "FAIL" ], - "description": "Engine-derived gate decision: PASS requires no findings, no blocking diagnostics, and equality of obligations and provedObligations; otherwise FAIL." + "description": "Engine-derived gate decision: PASS requires no findings, no blocking diagnostics, no rejected assessments, and equality of obligations and provedObligations; otherwise FAIL." }, "findings": { "type": "array", @@ -319,7 +319,8 @@ "locations", "scenario", "basePath", - "headPath" + "headPath", + "assessment" ], "properties": { "changes": { @@ -358,10 +359,11 @@ "BUILD_OUTPUT_UNCHANGED", "FOLDED_EQUIVALENT", "UNREACHABLE", + "STATIC_SAFETY_PROVEN", "SURVIVING_DIFFERENCE", "ANALYSIS_INCOMPLETE" ], - "description": "FOLDED_EQUIVALENT means no analyzed folded difference survives; UNREACHABLE means selected controls removed every static path to the change from the same input before folding; SURVIVING_DIFFERENCE means a difference remains; ANALYSIS_INCOMPLETE means a gap prevents a safety conclusion." + "description": "FOLDED_EQUIVALENT means no analyzed folded difference survives; UNREACHABLE means selected controls removed every static path to the change from the same input before folding; SURVIVING_DIFFERENCE means a difference remains; ANALYSIS_INCOMPLETE means a gap prevents a safety conclusion. STATIC_SAFETY_PROVEN requires an explicit whole-change proof for static acceptance; absence of evidence is not proof." }, "message": { "type": "string", @@ -391,9 +393,48 @@ "null" ], "description": "Repository-relative new file path, or null when the head path is absent or unspecified." + }, + "assessment": { + "$ref": "#/$defs/changeAssessment" } }, - "description": "Explanation of a change obligation outcome under one folding scenario, independent of whether it produces a finding." + "description": "Explanation of a change obligation outcome under one folding scenario, independent of whether it produces a finding.", + "allOf": [ + { + "if": { + "properties": { + "code": { + "const": "STATIC_SAFETY_PROVEN" + }, + "assessment": { + "properties": { + "decision": { + "const": "ACCEPTED" + } + }, + "required": [ + "decision" + ] + } + }, + "required": [ + "code", + "assessment" + ] + }, + "then": { + "properties": { + "assessment": { + "properties": { + "basis": { + "const": "STATIC_SAFETY" + } + } + } + } + } + } + ] }, "selector": { "type": "object", @@ -610,6 +651,150 @@ } } ] + }, + "safetyEvidence": { + "type": "object", + "additionalProperties": false, + "required": [ + "claim", + "status", + "subject", + "evidence", + "assumptions" + ], + "properties": { + "claim": { + "type": "string", + "enum": [ + "EFFECT_FREE", + "NONTHROWING", + "TERMINATING", + "WHOLE_CHANGE_EQUIVALENT" + ], + "description": "Whole-change equivalence is separate from individual method safety properties." + }, + "status": { + "type": "string", + "enum": [ + "PROVEN", + "REFUTED", + "UNPROVEN" + ], + "description": "UNPROVEN does not assert a demonstrated hazard." + }, + "subject": { + "type": "string", + "pattern": "\\S" + }, + "evidence": { + "type": "array", + "items": { + "type": "string", + "pattern": "\\S" + } + }, + "assumptions": { + "type": "array", + "items": { + "type": "string", + "pattern": "\\S" + } + } + }, + "description": "Cited static proof evidence and explicit assumptions for a particular subject." + }, + "changeAssessment": { + "type": "object", + "additionalProperties": false, + "required": [ + "control", + "safety", + "decision", + "basis" + ], + "properties": { + "control": { + "type": "string", + "enum": [ + "CONTROLLED", + "UNCONTROLLED", + "UNPROVEN" + ], + "description": "Control coverage is independent of acceptance; static safety does not establish control coverage." + }, + "safety": { + "type": "array", + "items": { + "$ref": "#/$defs/safetyEvidence" + } + }, + "decision": { + "type": "string", + "enum": [ + "ACCEPTED", + "REJECTED" + ], + "description": "Per-obligation acceptance decision, independent of control classification." + }, + "basis": { + "type": "string", + "enum": [ + "CONTROL_COVERAGE", + "STATIC_SAFETY", + "REPRESENTATION_EQUIVALENCE", + "INPUT_IDENTITY", + "INCOMPLETE_PROOF", + "SURVIVING_DIFFERENCE" + ], + "description": "Specific proof or unresolved difference supporting this decision." + } + }, + "description": "Independent control classification, safety evidence, and acceptance.", + "allOf": [ + { + "if": { + "properties": { + "basis": { + "const": "STATIC_SAFETY" + }, + "decision": { + "const": "ACCEPTED" + } + }, + "required": [ + "basis", + "decision" + ] + }, + "then": { + "properties": { + "control": { + "const": "UNCONTROLLED" + }, + "safety": { + "contains": { + "properties": { + "claim": { + "const": "WHOLE_CHANGE_EQUIVALENT" + }, + "status": { + "const": "PROVEN" + }, + "evidence": { + "minItems": 1 + } + }, + "required": [ + "claim", + "status", + "evidence" + ] + } + } + } + } + } + ] } } } diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessmentReportTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessmentReportTest.kt new file mode 100644 index 00000000..aaa4f02e --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ChangeAssessmentReportTest.kt @@ -0,0 +1,140 @@ +package io.johnsonlee.graphfold.engine + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.node.ObjectNode +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.networknt.schema.JsonSchemaFactory +import com.networknt.schema.SpecVersion +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue +import kotlin.test.assertIs + +class ChangeAssessmentReportTest { + private val mapper = jacksonObjectMapper() + private val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012) + .getSchema(AnalysisReports.schema()) + + @Test + fun `compatibility assessments do not relabel equivalent or identical inputs as controlled`() { + val expected = mapOf( + ReasonCode.UNREACHABLE to ControlClassification.CONTROLLED, + ReasonCode.FOLDED_EQUIVALENT to ControlClassification.UNPROVEN, + ReasonCode.BUILD_OUTPUT_UNCHANGED to ControlClassification.UNCONTROLLED, + ReasonCode.SURVIVING_DIFFERENCE to ControlClassification.UNCONTROLLED, + ReasonCode.ANALYSIS_INCOMPLETE to ControlClassification.UNPROVEN, + ReasonCode.STATIC_SAFETY_PROVEN to ControlClassification.UNPROVEN, + ) + expected.forEach { (code, control) -> + val reason = reason(code) + assertEquals(control, reason.assessment.control) + assertTrue(reason.assessment.safety.isEmpty()) + assertValid(result(reason)) + } + assertEquals(Verdict.FAIL, result(reason(ReasonCode.STATIC_SAFETY_PROVEN)).verdict) + assertEquals(AcceptanceDecision.REJECTED, reason(ReasonCode.STATIC_SAFETY_PROVEN).assessment.decision) + assertEquals(AcceptanceBasis.INCOMPLETE_PROOF, reason(ReasonCode.STATIC_SAFETY_PROVEN).assessment.basis) + } + + @Test + fun `legacy reason decoding uses conservative defaults without inventing safety evidence`() { + val original = reason(ReasonCode.FOLDED_EQUIVALENT) + val json = mapper.valueToTree(original) + json.remove("assessment") + assertEquals(original, mapper.treeToValue(json, VerdictReason::class.java)) + } + + @Test + fun `static acceptance retains uncontrolled classification and cites whole change certificate`() { + val assessment = accepted() + val report = result(reason(ReasonCode.STATIC_SAFETY_PROVEN).copy(assessment = assessment)) + assertValid(report) + assertEquals(Verdict.PASS, report.verdict) + val json = mapper.readTree(AnalysisReports.json(report))["reasons"][0]["assessment"] + assertEquals("UNCONTROLLED", json["control"].asText()) + assertEquals("ACCEPTED", json["decision"].asText()) + assertEquals("WHOLE_CHANGE_EQUIVALENT", json["safety"][0]["claim"].asText()) + val text = AnalysisReports.text(report) + assertTrue(text.contains("UNCONTROLLED: ACCEPTED via STATIC_SAFETY")) + assertTrue(text.contains("WHOLE_CHANGE_EQUIVALENT PROVEN: Example.value()I")) + assertTrue(text.contains("evidence: Both revisions return the identical constant")) + assertTrue(text.contains("assumption: Complete declared static inputs")) + } + + @Test + fun `method safety alone does not accept changed return value or fabricate whole change proof`() { + val properties = SafetyClaim.entries.filter { it != SafetyClaim.WHOLE_CHANGE_EQUIVALENT } + .map { proof().copy(claim = it) } + assertFailsWith { accepted(properties) } + val incomplete = ChangeAssessment(ControlClassification.UNCONTROLLED, properties, + AcceptanceDecision.REJECTED, AcceptanceBasis.INCOMPLETE_PROOF) + assertValid(result(reason(ReasonCode.ANALYSIS_INCOMPLETE).copy(assessment = incomplete))) + assertFailsWith { + reason(ReasonCode.STATIC_SAFETY_PROVEN).copy(assessment = + reason(ReasonCode.FOLDED_EQUIVALENT).assessment) + } + assertFailsWith { accepted(emptyList()) } + assertFailsWith { accepted().copy(control = ControlClassification.CONTROLLED) } + assertFailsWith { accepted().copy(control = ControlClassification.UNPROVEN) } + } + + @Test + fun `missing and refuted evidence cannot masquerade as proven static acceptance`() { + val invalid = listOf( + proof().copy(status = SafetyStatus.UNPROVEN), proof().copy(status = SafetyStatus.REFUTED), + proof().copy(subject = " "), proof().copy(evidence = emptyList()), + proof().copy(evidence = listOf(" ")), + ) + invalid.forEach { evidence -> assertFailsWith { accepted(listOf(evidence)) } } + SafetyStatus.entries.forEach { status -> + val assessment = ChangeAssessment(ControlClassification.UNPROVEN, listOf(proof().copy(status = status)), + AcceptanceDecision.REJECTED, AcceptanceBasis.STATIC_SAFETY) + assertValid(result(reason(ReasonCode.ANALYSIS_INCOMPLETE).copy(assessment = assessment))) + assertTrue(AnalysisReports.text(result(reason(ReasonCode.ANALYSIS_INCOMPLETE) + .copy(assessment = assessment))).contains("WHOLE_CHANGE_EQUIVALENT $status")) + } + } + + @Test + fun `schema independently rejects empty certificates unknown axes and missing evidence fields`() { + val original = mapper.readTree(AnalysisReports.json(result(reason(ReasonCode.STATIC_SAFETY_PROVEN) + .copy(assessment = accepted())))) + val mutations = mapOf( + "/control" to "\"CONTROLLED\"", "/decision" to "\"UNKNOWN\"", "/basis" to "\"REPRESENTATION_EQUIVALENCE\"", + "/safety" to "[]", "/safety/0/claim" to "\"EFFECT_FREE\"", + "/safety/0/status" to "\"UNPROVEN\"", "/safety/0/evidence" to "[]", + "/safety/0/subject" to "\" \"", "/safety/0/assumptions" to "[\" \u0020\"]", + ) + mutations.forEach { (path, value) -> + val copy = original.deepCopy() + val full = "/reasons/0/assessment$path" + assertIs(copy.at(full.substringBeforeLast('/'))) + .set(full.substringAfterLast('/'), mapper.readTree(value)) + assertTrue(schema.validate(copy).isNotEmpty(), path) + } + listOf("claim", "status", "subject", "evidence", "assumptions").forEach { field -> + val copy = original.deepCopy() + assertIs(copy.at("/reasons/0/assessment/safety/0")).remove(field) + assertTrue(schema.validate(copy).isNotEmpty(), field) + } + } + + private fun proof() = SafetyEvidence(SafetyClaim.WHOLE_CHANGE_EQUIVALENT, SafetyStatus.PROVEN, + "Example.value()I", listOf("Both revisions return the identical constant"), + listOf("Complete declared static inputs")) + + private fun accepted(evidence: List = listOf(proof())) = ChangeAssessment( + ControlClassification.UNCONTROLLED, evidence, AcceptanceDecision.ACCEPTED, AcceptanceBasis.STATIC_SAFETY, + ) + + private fun reason(code: ReasonCode) = VerdictReason(emptySet(), code, "Static comparison", emptyList(), "baseline") + + private fun result(reason: VerdictReason) = AnalysisResult(emptyList(), emptyList(), + AnalysisMetadata("base", "head", listOf("baseline"), 1, 1), listOf(reason)) + + private fun assertValid(result: AnalysisResult) { + val errors = schema.validate(mapper.readTree(AnalysisReports.json(result))) + assertTrue(errors.isEmpty(), errors.joinToString("\n")) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessmentBoundaryTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessmentBoundaryTest.kt new file mode 100644 index 00000000..5500907d --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/ComparisonAssessmentBoundaryTest.kt @@ -0,0 +1,50 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.PropertyProof +import io.johnsonlee.graphfold.engine.snapshot.MethodSnapshot +import io.johnsonlee.graphfold.engine.snapshot.ProgramSnapshot +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class ComparisonAssessmentBoundaryTest { + private val identity = "Example.value()I" + + @Test + fun `neither side of private member alignment can borrow original identity safety evidence`() { + val before = snapshot("before") + val after = snapshot("after") + val property = PropertyProof(ProofStatus.PROVEN, listOf("test mechanism")) + val summary = MethodSafetySummary(identity, property, property, property, "return:1", emptySet()) + val safety = { WholeChangeSafety(mapOf(identity to summary), mapOf(identity to summary)) } + for (alignment in listOf(mapOf(identity to "Example.other()I"), mapOf("Example.other()I" to identity))) { + val assessment = ComparisonAssessments(before, after, alignment, safety, false, null, { false }, { emptySet() }) + .safety(listOf(identity), emptyList()) + assertEquals(AcceptanceDecision.REJECTED, assessment?.decision) + assertTrue(requireNotNull(assessment).safety.any { + it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT && it.status == SafetyStatus.UNPROVEN + }) + } + val unchangedIdentity = ComparisonAssessments(before, after, emptyMap(), safety, false, null, { false }, { emptySet() }) + assertEquals(AcceptanceDecision.ACCEPTED, unchangedIdentity.safety(listOf(identity), emptyList())?.decision) + } + + @Test + fun `fold dependency inventory includes bodies removed or added on either revision`() { + val empty = ProgramSnapshot(emptyMap(), emptyMap(), emptyList(), emptyList()) + val body = snapshot("body") + val absent = SnapshotComparison(empty, empty) + for (comparison in listOf(SnapshotComparison(body, empty), SnapshotComparison(empty, body), SnapshotComparison(body, body))) { + assertEquals(setOf(identity), comparison.foldedBodies(absent)) + assertEquals(setOf(identity), absent.foldedBodies(comparison)) + assertTrue(comparison.foldedBodies(comparison).isEmpty()) + } + } + + private fun snapshot(body: String) = ProgramSnapshot( + mapOf(identity to MethodSnapshot(identity, body, emptySet(), emptySet(), emptySet())), + mapOf("Example" to "class", identity to "method"), emptyList(), emptyList(), + ) +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/EngineIntegrationTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/EngineIntegrationTest.kt index 41cfb4e6..a4108531 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/EngineIntegrationTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/EngineIntegrationTest.kt @@ -69,7 +69,7 @@ class EngineIntegrationTest { @Test fun `invalid fold is input failure rather than empty successful report`() { - val result = engine().analyze(AnalysisRequest("missing", "a", "b", "folds: []")) + val result = engine().analyze(AnalysisRequest("missing", "a", "b", "folds: [null]")) assertEquals(Verdict.FAIL, result.verdict) assertEquals(AnalysisStage.INPUT, result.diagnostics.single().stage) } diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/FoldDocumentTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/FoldDocumentTest.kt index b3fc6740..2d9dcafc 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/FoldDocumentTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/FoldDocumentTest.kt @@ -16,6 +16,17 @@ class FoldDocumentTest { assertEquals(1, scenarios.single().rules.size) } + @Test + fun `explicit empty controls request an independent static safety analysis`() { + val baseline = FoldDocument.parse("version: 1\nfolds: []", 1).single() + assertEquals("baseline", baseline.name) + assertTrue(baseline.rules.isEmpty()) + val named = FoldDocument.parse("scenarios: [{name: safety, folds: []}]", 1).single() + assertEquals("safety", named.name) + assertTrue(named.rules.isEmpty()) + assertFails { FoldDocument.parse("version: 1", 1) } + } + @Test fun `explicit scenarios are preserved as complete independent assumptions`() { val scenarios = FoldDocument.parse(""" @@ -105,7 +116,7 @@ class FoldDocumentTest { val invalid = listOf( "scenarios: [{name: A, typo: true, folds: [$rule]}]", "scenarios: []", "scenarios: nope", "scenarios: [1]", "scenarios: []\nextra: 1", - "folds: []", "folds: nope", "folds: [1]", "folds: [$rule]\nversion: 2", + "folds: nope", "folds: [1]", "folds: [$rule]\nversion: 2", "folds: [$rule]\nextra: 1", "name: ignored\nfolds: [$rule]", "scenarios: [{folds: [$rule]}]", "scenarios: [{name: '', folds: [$rule]}]", "scenarios: [{name: same, folds: [$rule]}, {name: same, folds: [$rule]}]", diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureArgumentGoldenTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureArgumentGoldenTest.kt new file mode 100644 index 00000000..5d314078 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureArgumentGoldenTest.kt @@ -0,0 +1,74 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.graphite.GraphiteAnalyzer +import io.johnsonlee.graphfold.engine.source.ProofObligation +import io.johnsonlee.graphfold.engine.source.SymbolKind +import io.johnsonlee.graphite.input.ConstantFold +import java.nio.file.Files +import java.nio.file.Path +import javax.tools.ToolProvider +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** Java argument evaluation remains an obligation independently of a helper's total body. */ +class GeneralExposureArgumentGoldenTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest(name = "{0}") + @MethodSource("arguments") + fun `only supported total argument producers disappear with the selected branch`(case: ArgumentCase) { + val base = compile("base", case, "return 10;") + val head = compile("head", case, "if (eligible(${case.argument}) && enabled()) return 20; return 10;") + val obligation = ProofObligation("Exposure.quote(Ljava/lang/String;)I", SymbolKind.METHOD, "Exposure", emptyList()) + val before = GraphiteAnalyzer().analyze(base, emptyList()) + assertTrue(before.diagnostics.isEmpty(), before.diagnostics.toString()) + for (enabled in listOf(false, true)) { + val rule = ConstantFold.parse(mapOf("match" to mapOf("CallSite" to mapOf( + "callee_class" to "Exposure", "callee_name" to "enabled")), "value" to enabled)) + val after = GraphiteAnalyzer().analyze(head, listOf(rule)) + assertEquals(listOf(1), after.matchedRules) + assertTrue(after.diagnostics.isEmpty(), after.diagnostics.toString()) + val comparison = SnapshotComparison(before, after) + assertTrue(comparison.analysisGaps(obligation).isEmpty(), comparison.analysisGaps(obligation).toString()) + assertEquals(!enabled && case.disposable, comparison.differences(obligation).isEmpty(), + "$case, enabled=$enabled: ${comparison.differences(obligation)}") + } + } + + private fun compile(name: String, case: ArgumentCase, body: String): Path { + val root = Files.createDirectories(directory.resolve(name)) + val source = root.resolve("Exposure.java") + Files.writeString(source, """ + public class Exposure { + public static boolean flag; + static boolean enabled() { return flag; } + private static boolean eligible(${case.parameter}) { ${case.helper} } + public static int quote(String text) { $body } + } + """.trimIndent()) + val classes = Files.createDirectories(root.resolve("classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, + "-g", "-d", classes.toString(), source.toString())) + return classes + } + + data class ArgumentCase( + val name: String, val parameter: String, val argument: String, val helper: String, val disposable: Boolean, + ) { + override fun toString(): String = name + } + + companion object { + @JvmStatic fun arguments(): List = listOf( + ArgumentCase("nullable reference local", "String value", "text", "return value != null;", true), + ArgumentCase("literal String", "String value", "\"ready\"", "return value != null;", true), + ArgumentCase("literal null", "String value", "null", "return value == null;", true), + ArgumentCase("integer literal", "int value", "7", "return value > 0;", true), + ArgumentCase("long literal remains conservatively unsupported", "long value", "7L", "return true;", false), + ArgumentCase("class literal has separate resolution evidence", "Class value", "Exposure.class", "return true;", false), + ) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureSafetyGoldenTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureSafetyGoldenTest.kt new file mode 100644 index 00000000..3c348d94 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/GeneralExposureSafetyGoldenTest.kt @@ -0,0 +1,110 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.graphite.GraphiteAnalyzer +import io.johnsonlee.graphfold.engine.source.ProofObligation +import io.johnsonlee.graphfold.engine.source.SymbolKind +import io.johnsonlee.graphite.input.ConstantFold +import java.nio.file.Files +import java.nio.file.Path +import javax.tools.ToolProvider +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** Actual compiler and graph evidence; no fixture execution is used to establish control coverage. */ +class GeneralExposureSafetyGoldenTest { + @TempDir lateinit var directory: Path + + @Test fun `literal receiver equality is safe for every nullable argument`() = verify( + "return \"ready\".equals(text);", controlledOff = true, + ) + + @Test fun `short circuit null guard protects a String operation inside the helper`() = verify( + "return text != null && text.length() > 0;", controlledOff = true, + ) + + @Test fun `guarding a stable local nullable helper result establishes whole helper safety`() = verify( + "String value = nullable(text); return value != null && value.length() > 0;", controlledOff = true, + ) + + @Test fun `unguarded nullable receiver keeps the exposure obligation`() = verify( + "return text.length() > 0;", controlledOff = false, + ) + + @Test fun `guard on input does not establish nonnullness of a nullable intermediate`() = verify( + "return text != null && nullable(text).length() > 0;", controlledOff = false, + ) + + @Test fun `effectful predicate cannot disappear with the selected branch`() = verify( + "count++; return text != null;", controlledOff = false, + ) + + @Test fun `unsupported monitor operations are not a complete helper proof`() = verify( + "synchronized (text) { return true; }", controlledOff = false, + ) + + @Test fun `recursive predicates have no termination proof`() = verify( + "return eligible(text);", controlledOff = false, + ) + + @Test fun `safe helper does not excuse an effectful argument producer`() = verify( + "return \"ready\".equals(text);", controlledOff = false, + body = "if (eligible(produced(text)) && enabled()) return 20; return 10;", + ) + + @Test fun `predicate result reused in unrelated fallback remains an obligation`() = verify( + "return \"ready\".equals(text);", controlledOff = false, + body = "boolean value = eligible(text); if (value && enabled()) return 20; return value ? 30 : 10;", + ) + + @Test fun `selected folding inside a previously safe helper cannot erase its newly exposed nullable receiver`() = verify( + "if (enabled()) return true; return text.length() > 0;", controlledOff = false, + enabledBody = "return true;", expectedMatches = 2, + ) + + private fun verify( + helper: String, controlledOff: Boolean, + body: String = "if (eligible(text) && enabled()) return 20; return 10;", + enabledBody: String = "return flag;", expectedMatches: Int = 1, + ) { + val base = compile("base", helper, "return 10;", enabledBody) + val head = compile("head", helper, body, enabledBody) + val obligation = ProofObligation("Exposure.quote(Ljava/lang/String;)I", SymbolKind.METHOD, "Exposure", emptyList()) + for (enabled in listOf(false, true)) { + val fold = ConstantFold.parse(mapOf("match" to mapOf("CallSite" to mapOf( + "callee_class" to "Exposure", "callee_name" to "enabled")), "value" to enabled)) + val analyzer = GraphiteAnalyzer() + val before = analyzer.analyze(base, emptyList()) + val after = analyzer.analyze(head, listOf(fold)) + assertEquals(listOf(expectedMatches), after.matchedRules) + assertTrue(before.diagnostics.isEmpty(), before.diagnostics.toString()) + assertTrue(after.diagnostics.isEmpty(), after.diagnostics.toString()) + val comparison = SnapshotComparison(before, after) + assertTrue(comparison.analysisGaps(obligation).isEmpty(), comparison.analysisGaps(obligation).toString()) + assertEquals(!enabled && controlledOff, comparison.differences(obligation).isEmpty(), + "selected enabled=$enabled; helper=$helper; differences=${comparison.differences(obligation)}\n" + + after.methods.getValue(obligation.identity).canonical) + } + } + + private fun compile(name: String, helper: String, body: String, enabledBody: String): Path { + val root = Files.createDirectories(directory.resolve(name)) + val source = root.resolve("Exposure.java") + Files.writeString(source, """ + public class Exposure { + public static boolean flag; + public static int count; + static boolean enabled() { $enabledBody } + private static String nullable(String text) { return text == null || text.length() == 0 ? null : text; } + private static String produced(String text) { count++; return text; } + private static boolean eligible(String text) { $helper } + public static int quote(String text) { $body } + } + """.trimIndent()) + val classes = Files.createDirectories(root.resolve("classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, + "-g", "-d", classes.toString(), source.toString())) + return classes + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparationFailureReportTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparationFailureReportTest.kt index 88b37f2f..1a545d84 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparationFailureReportTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparationFailureReportTest.kt @@ -36,7 +36,7 @@ class PreparationFailureReportTest { } @Test fun `invalid control document takes precedence over preparation failure`() { - val invalid = "folds: []" + val invalid = "folds: [null]" assertFailsWith { FoldDocument.validate(invalid, 1) } val result = AnalysisReports.preparationFailure("base", "head", invalid, 1, unavailable) assertEquals(Verdict.FAIL, result.verdict) diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparedInputGoldenTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparedInputGoldenTest.kt index 93a775a3..a3b4b8b1 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparedInputGoldenTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/PreparedInputGoldenTest.kt @@ -85,6 +85,47 @@ class PreparedInputGoldenTest { assertEquals(expected == Verdict.FAIL, result.findings.isNotEmpty(), result.toString()) assertEquals(2, result.ruleAudits.size) assertTrue(result.ruleAudits.all { it.matchedCallCount == 1 }, result.toString()) + assertPrivateChangeAssessment(mode, result) + } + + private fun assertPrivateChangeAssessment(mode: String, result: AnalysisResult) { + val modes = setOf("unused", "added-unused", "lambda", "stream", "controlled", "added-controlled") + if (mode !in modes) return + val callback = mode == "lambda" || mode == "stream" + val line = if (callback) 4 else 5 + val changed = result.reasons.filter { reason -> reason.locations.any { + it.path == "Example.java" && it.line == SourceRange(line, line) + } } + assertTrue(changed.isNotEmpty(), "$mode: changed source obligation needs its own assessment: $result") + val controlled = mode == "controlled" || mode == "added-controlled" + changed.forEach { reason -> + val assessment = reason.assessment + assertEquals(if (controlled) ControlClassification.CONTROLLED else ControlClassification.UNCONTROLLED, + assessment.control, "$mode: $reason") + assertEquals(if (controlled) AcceptanceDecision.ACCEPTED else AcceptanceDecision.REJECTED, + assessment.decision, "$mode: $reason") + assertEquals(if (controlled) AcceptanceBasis.CONTROL_COVERAGE else AcceptanceBasis.INCOMPLETE_PROOF, + assessment.basis, "$mode: $reason") + if (controlled) { + assertTrue(assessment.safety.isEmpty(), "Control coverage must not invent a safety certificate: $reason") + } else { + assertUncontrolledSafetyEvidence(mode, reason) + } + } + } + + private fun assertUncontrolledSafetyEvidence(mode: String, reason: VerdictReason) { + val whole = reason.assessment.safety.single { it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT } + assertEquals(SafetyStatus.UNPROVEN, whole.status, "$mode: $reason") + val identity = when (mode) { + "lambda" -> "Example.lambda\$value\$0()I" + "stream" -> "Example.lambda\$value\$0(Ljava/lang/Integer;)Ljava/lang/Integer;" + else -> "Example.unused()I" + } + assertEquals(identity, whole.subject, "$mode: changed obligation: $reason") + assertTrue(whole.evidence.isNotEmpty() && whole.evidence.all(String::isNotBlank), "$mode: $reason") + assertTrue(reason.assessment.safety.none { it.status == SafetyStatus.REFUTED }, + "Missing equivalence proof does not establish an exception or effect hazard: $reason") } @ParameterizedTest diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/UncontrolledSafetyGoldenTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/UncontrolledSafetyGoldenTest.kt new file mode 100644 index 00000000..339556f6 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/UncontrolledSafetyGoldenTest.kt @@ -0,0 +1,354 @@ +package io.johnsonlee.graphfold.engine + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import io.johnsonlee.graphfold.engine.git.changedFile +import io.johnsonlee.graphfold.engine.inputs.archive.ArchiveInput +import io.johnsonlee.graphfold.engine.inputs.archive.ArchiveEntryInput +import io.johnsonlee.graphfold.engine.inputs.archive.prepareArchiveInput +import java.util.jar.JarEntry +import java.util.jar.JarOutputStream +import org.junit.jupiter.params.provider.MethodSource +import io.johnsonlee.graphfold.engine.inputs.ContentInventory +import io.johnsonlee.graphfold.engine.inputs.PreparedRevision +import io.johnsonlee.graphfold.engine.inputs.archive.InputProject +import io.johnsonlee.graphfold.engine.inputs.archive.toStaticScope +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler +import io.johnsonlee.graphfold.engine.testing.SourceFixtureCompiler +import java.nio.file.Files +import java.nio.file.Path +import java.time.Duration +import org.eclipse.jgit.diff.Edit +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import org.objectweb.asm.ClassReader +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodInsnNode +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** Real compiler output supplies both static properties and the independent relational proof. */ +class UncontrolledSafetyGoldenTest { + @TempDir lateinit var temporary: Path + + @ParameterizedTest + @CsvSource("java,true", "java,false", "kotlin,true", "kotlin,false") + fun `retained pure arithmetic is accepted without relabeling uncontrolled changes`(language: String, mapped: Boolean) { + val before = revision("base", language, body(language, false), provenance = mapped) + val after = revision("head", language, body(language, true), provenance = mapped) + assertTrue(hasAddition(after), "Compiler must retain the additional arithmetic for this proof case") + assertFalse(hasAddition(before)) + val result = analyze(before, after, language, mapped) + assertEquals(Verdict.PASS, result.verdict, result.toString()) + assertTrue(result.findings.isEmpty() && result.diagnostics.isEmpty(), result.toString()) + assertTrue(result.ruleAudits.isEmpty(), "An empty control set must not invent control selection") + val reasons = result.reasons.filter { it.code == ReasonCode.STATIC_SAFETY_PROVEN } + assertTrue(reasons.isNotEmpty(), result.toString()) + reasons.forEach { reason -> + assertEquals(ControlClassification.UNCONTROLLED, reason.assessment.control) + assertEquals(AcceptanceDecision.ACCEPTED, reason.assessment.decision) + assertEquals(AcceptanceBasis.STATIC_SAFETY, reason.assessment.basis) + assertTrue(reason.assessment.safety.any { it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT && + it.status == SafetyStatus.PROVEN && it.evidence.isNotEmpty() }, reason.toString()) + } + } + + @ParameterizedTest + @ValueSource(strings = ["java", "kotlin"]) + fun `return price changed to zero fails despite all three local safety properties`(language: String) { + val before = revision("base", language, body(language, false)) + val after = revision("head", language, body(language, false).replace("return price", "return 0")) + val result = analyze(before, after, language, true) + assertEquals(Verdict.FAIL, result.verdict, result.toString()) + val evidence = result.reasons.flatMap { it.assessment.safety } + listOf(SafetyClaim.EFFECT_FREE, SafetyClaim.NONTHROWING, SafetyClaim.TERMINATING).forEach { claim -> + val properties = evidence.filter { it.claim == claim } + assertTrue(properties.any { it.subject.startsWith("BASE ") }, result.toString()) + assertTrue(properties.any { it.subject.startsWith("HEAD ") }, result.toString()) + assertTrue(properties.all { it.status == SafetyStatus.PROVEN }, result.toString()) + } + assertTrue(evidence.any { it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT && + it.status != SafetyStatus.PROVEN }, result.toString()) + assertTrue(result.reasons.none { it.assessment.control == ControlClassification.CONTROLLED }) + } + + @ParameterizedTest + @CsvSource("dereference,String text,int ignored = text.length();", + "argument,int price,int ignored = helper(1 / price);", + "unboxing,Integer price,int ignored = price;", + "literal-null,String text,String target = null; int ignored = target.length();", + "unsupported,int price,double ignored = Math.sin(price);") + fun `hazardous or unsupported discarded computations cannot use equal return values`( + mode: String, parameter: String, computation: String, + ) { + val helper = "static int helper(int input) { return input; }" + val base = "public class Example {\n public static int value($parameter) { return 1; }\n $helper\n}" + val head = base.replace("return 1;", "$computation return 1;") + val result = analyze(revision("base-$mode", "java", base), revision("head-$mode", "java", head), "java", true) + assertEquals(Verdict.FAIL, result.verdict, result.toString()) + assertTrue(result.reasons.none { it.code == ReasonCode.STATIC_SAFETY_PROVEN }, result.toString()) + val expected = if (mode == "literal-null") SafetyStatus.REFUTED else SafetyStatus.UNPROVEN + assertTrue(result.reasons.flatMap { it.assessment.safety }.any { + it.claim == SafetyClaim.NONTHROWING && it.status == expected && it.subject.startsWith("HEAD ") + }, result.toString()) + } + + @ParameterizedTest + @ValueSource(strings = ["class", "field", "metadata", "resource"]) + fun `safe arithmetic does not discharge unrelated whole input differences`(mode: String) { + val base = body("java", false) + val changed = when (mode) { + "class" -> body("java", true) + "\nclass Additional {}" + "field" -> body("java", true).replace("public class Example {", "public class Example { public int added;") + "metadata" -> body("java", true).replace("public static int", "@Deprecated public static int") + else -> body("java", true) + } + val before = revision("base", "java", base, resource = "before") + val after = revision("head", "java", changed, resource = if (mode == "resource") "after" else "before") + assertTrue(hasAddition(after)) + val result = analyze(before, after, "java", false) + assertEquals(Verdict.FAIL, result.verdict, result.toString()) + assertTrue(result.diagnostics.any { it.blocking } || result.findings.isNotEmpty(), result.toString()) + } + + @ParameterizedTest(name = "{0}") + @MethodSource("packagedCases") + fun `packaged changes satisfy the explicit independent safety contract`(case: SafetyGoldenCase) { + val before = revision("base", case.language, packagedSource(case, false), provenance = false) + val after = revision("head", case.language, packagedSource(case, true), provenance = false) + val identities = listOf(before, after).map { revision -> + applicationClasses(revision).flatMap { node -> + node.methods.filter { it.name == "value" } + .map { "${node.name.replace('/', '.')}.${it.name}${it.desc}" } + }.single() + } + assertEquals(identities.first(), identities.last(), "$case: the changed obligation must retain its identity") + val beforeInstructions = applicationClasses(before).flatMap { it.methods }.filter { it.name == "value" } + .flatMap { it.instructions.toList() }.map { it.opcode }.filter { it >= 0 } + val afterInstructions = applicationClasses(after).flatMap { it.methods }.filter { it.name == "value" } + .flatMap { it.instructions.toList() }.map { it.opcode }.filter { it >= 0 } + assertTrue(beforeInstructions != afterInstructions, "$case: compiler must retain changed executable instructions") + if (case.change.startsWith("helper-")) { + assertFalse(hasHelperCall(before), "BASE must not already call the helper") + assertTrue(hasHelperCall(after), "The actual compiler must retain HEAD's helper invocation") + } + val result = analyze(packaged(before, case.inputVariant), packaged(after, case.inputVariant), case.language, false) + assertEquals(case.expected.verdict, result.verdict, result.toString()) + assertTrue(result.ruleAudits.isEmpty(), result.toString()) + assertTrue(result.reasons.isNotEmpty(), result.toString()) + assertEquals(case.expected.diagnosticCodes, result.diagnostics.map { it.code }, result.toString()) + if (case.change in setOf("helper-opaque", "opaque-alternative")) { + val nativeIdentity = applicationClasses(after).flatMap { node -> + node.methods.filter { it.access and Opcodes.ACC_NATIVE != 0 } + .map { "${node.name.replace('/', '.')}.${it.name}${it.desc}" } + }.single() + val inventoryGap = "Native/JNI method prevents proving private callback reachability: $nativeIdentity" + val expectedMessages = listOf("base: $inventoryGap", "head: $inventoryGap", + "Graphite NATIVE: $nativeIdentity", "Graphite observed NATIVE: $nativeIdentity") + assertEquals(expectedMessages, result.diagnostics.take(4).map { it.message }, case.id) + } + assertTrue(result.diagnostics.all { it.blocking }, result.toString()) + assertTrue(result.findings.isEmpty(), "Source-free packaged inputs must not invent source findings") + if (case.expected.decision == AcceptanceDecision.ACCEPTED) { + assertTrue(result.diagnostics.isEmpty(), result.toString()) + assertTrue(result.metadata.obligations > 0, result.toString()) + assertEquals(result.metadata.obligations, result.metadata.provedObligations, result.toString()) + } + result.reasons.forEach { reason -> assertContract(case, identities.first(), reason.assessment) } + } + + private fun packagedSource(case: SafetyGoldenCase, changed: Boolean): String { + case.sourcePair?.let { pair -> + val resource = if (changed) pair.headResource else pair.baseResource + return requireNotNull(javaClass.getResource(resource)).readText() + } + return if (case.change.startsWith("helper-")) helperSource(case, changed) else when { + !changed -> body(case.language, false) + case.change == "safe" -> body(case.language, true) + case.change == "return-change" -> body(case.language, false).replace("return price", "return 0") + case.language == "java" -> body("java", false) + .replace("return price;", "String target = null; int ignored = target.length(); return price;") + else -> body("kotlin", false) + .replace("return price", "val target: String? = null; val ignored = target!!.length; return price") + } + } + + private fun helperSource(case: SafetyGoldenCase, changed: Boolean): String { + val result = if (changed) "helper(price)" else "price" + return if (case.language == "java") { + val helper = when (case.change) { + "helper-pure" -> "private static int helper(int price) { return price + 0; }" + "helper-effect" -> "private static int count; private static int helper(int price) { count++; return price; }" + else -> "private static native int helper(int price);" + } + "public class Example {\n public static int value(int price) { return $result; }\n $helper\n}" + } else { + val helper = when (case.change) { + "helper-pure" -> "private fun helper(price: Int): Int = price + 0" + "helper-effect" -> "private var count: Int = 0\nprivate fun helper(price: Int): Int { count++; return price }" + else -> "private external fun helper(price: Int): Int" + } + "fun value(price: Int): Int = $result\n$helper\n" + } + } + + private fun hasHelperCall(revision: PreparedRevision): Boolean = applicationClasses(revision).any { node -> + node.methods.filter { it.name == "value" }.flatMap { it.instructions.toList() } + .filterIsInstance().any { it.name == "helper" } + } + + private fun assertContract(case: SafetyGoldenCase, identity: String, assessment: ChangeAssessment) { + assertEquals(case.expected.control, assessment.control, case.id) + assertEquals(case.expected.decision, assessment.decision, case.id) + assertEquals(case.expected.basis, assessment.basis, case.id) + val expectedProofs = case.expected.claims.flatMap { (claim, status) -> + if (claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT) listOf(Triple(claim, identity, status)) + else listOf(Triple(claim, "BASE $identity", SafetyStatus.PROVEN), Triple(claim, "HEAD $identity", status)) + } + val actualProofs = assessment.safety.map { Triple(it.claim, it.subject, it.status) } + assertEquals(7, expectedProofs.size, case.id) + assertEquals(expectedProofs.size, actualProofs.size, case.id) + assertEquals(expectedProofs.toSet(), actualProofs.toSet(), case.id) + assertEquals(setOf("NONEMPTY_SUBJECT", "NONEMPTY_CITATIONS", "NONEMPTY_ASSUMPTIONS"), + case.evidenceRequirements.toSet()) + assessment.safety.forEach { proof -> + assertTrue(proof.subject.isNotBlank(), case.id) + assertTrue(proof.evidence.isNotEmpty() && proof.evidence.all(String::isNotBlank), case.id) + assertTrue(proof.assumptions.isNotEmpty() && proof.assumptions.all(String::isNotBlank), case.id) + } + } + + private fun packaged(revision: PreparedRevision, variant: String): PreparedRevision { + val scope = revision.scopes.getValue("application") + val prefix = if (variant == "boot") "BOOT-INF" else "WEB-INF" + val archive = temporary.resolve("${revision.sha}.${if (variant == "boot") "jar" else "war"}") + val entries = Files.walk(scope.classes).use { paths -> paths.filter(Files::isRegularFile).toList() } + .associate { "$prefix/classes/${scope.classes.relativize(it)}" to Files.readAllBytes(it) }.toMutableMap() + scope.dependencies.forEachIndexed { index, path -> entries["$prefix/lib/dependency-$index.jar"] = Files.readAllBytes(path) } + entries["$prefix/classes/config.txt"] = "stable resource".toByteArray() + if (variant == "war") entries["META-INF/context.xml"] = "".toByteArray() + JarOutputStream(Files.newOutputStream(archive)).use { jar -> + entries.toSortedMap().forEach { (name, bytes) -> + jar.putNextEntry(JarEntry(name)); jar.write(bytes); jar.closeEntry() + } + } + val output = temporary.resolve("${revision.sha}-packaged") + val emptyEvidence = InputProject(Files.createDirectories(output.resolve("empty-compiler")), emptyList(), emptyList()) + val input = prepareArchiveInput(ArchiveInput("selected", listOf(ArchiveEntryInput(archive))), + emptyEvidence, output.resolve("prepared"), variantThreshold = 17) + val normalized = input.toStaticScope(output.resolve("dependencies"), variantThreshold = 17) + assertTrue(normalized.sourceRoots.isEmpty() && normalized.originDirectory == null) + return PreparedRevision(revision.sha, temporary.resolve("missing-${revision.sha}"), + mapOf("application" to normalized), input.production) + } + + private fun analyze(base: PreparedRevision, head: PreparedRevision, language: String, mapped: Boolean): AnalysisResult { + val path = sourcePath(language) + val change = if (mapped) changedFile(path, path, edits = listOf(Edit(0, 3, 0, 3))) + else changedFile("generator.txt", "generator.txt") + return GraphFoldEngine(EngineConfiguration(temporary.resolve("analysis"))).analyze( + PreparedAnalysisRequest(if (mapped) base else withoutSourceEvidence(base), + if (mapped) head else withoutSourceEvidence(head), listOf(change), "version: 1\nfolds: []")) + } + + private fun withoutSourceEvidence(revision: PreparedRevision) = revision.copy( + sourceDirectory = temporary.resolve("absent-${revision.sha}"), + scopes = revision.scopes.mapValues { (_, scope) -> scope.copy(sourceRoots = emptyList(), + originDirectory = null, originRunId = null, sourceOutputs = emptyMap()) }, + ) + + private fun revision( + name: String, language: String, source: String, resource: String? = null, provenance: Boolean = true, + ): PreparedRevision { + val checkout = Files.createDirectories(temporary.resolve(name).resolve("source")) + val file = checkout.resolve(sourcePath(language)) + Files.createDirectories(file.parent) + Files.writeString(file, source) + val output = temporary.resolve(name).resolve("output") + val compiled = if (language == "kotlin" && !provenance) compilePlainKotlin(file, output) + else SourceFixtureCompiler(Duration.ofMinutes(1)).compile(checkout, output) + val entries = Files.walk(compiled.classes).use { paths -> paths.filter(Files::isRegularFile).toList() } + .associate { "class/${compiled.classes.relativize(it)}" to digest(it) } + val resources = resource?.let { + mapOf("resource/data.txt" to digest(Files.writeString(temporary.resolve("$name-data.txt"), it))) + }.orEmpty() + val inventory = ContentInventory(entries + resources, resources) + val scope = compiled.toStaticScope(output.resolve("normalized-dependencies"), variantThreshold = 17) + .copy(production = inventory) + return PreparedRevision(name, checkout, mapOf("application" to scope), inventory) + } + + private fun compilePlainKotlin(file: Path, output: Path): InputProject { + val classes = Files.createDirectories(output.resolve("classes")) + val dependencies = listOf(Unit::class.java, org.jetbrains.annotations.NotNull::class.java) + .map { Path.of(it.protectionDomain.codeSource.location.toURI()) } + val messages = ByteArrayOutputStream() + val status = PrintStream(messages).use { stream -> + K2JVMCompiler().exec(stream, "-no-stdlib", "-no-reflect", "-jvm-target", "17", + "-classpath", dependencies.joinToString(java.io.File.pathSeparator), + "-d", classes.toString(), file.toString()) + } + assertEquals(ExitCode.OK, status, messages.toString()) + return InputProject(classes, listOf(file.parent), dependencies) + } + + private fun body(language: String, extra: Boolean): String = if (language == "java") { + "public class Example {\n public static int value(int price) { " + + (if (extra) "int ignored = price + 1; " else "") + "return price; }\n}" + } else { + "fun value(price: Int): Int {\n " + (if (extra) "val ignored = price + 1; " else "") + "return price\n}" + } + + private fun sourcePath(language: String) = if (language == "java") "src/main/java/Example.java" + else "src/main/kotlin/Example.kt" + + private fun hasAddition(revision: PreparedRevision): Boolean = applicationClasses(revision).any { node -> + node.methods.any { method -> method.name == "value" && method.instructions.any { it.opcode == Opcodes.IADD } } + } + + private fun applicationClasses(revision: PreparedRevision): List { + val classes = revision.scopes.getValue("application").classes + return Files.walk(classes).use { paths -> paths.filter { it.toString().endsWith(".class") }.toList() } + .map { path -> ClassNode().also { ClassReader(Files.readAllBytes(path)).accept(it, 0) } } + } + companion object { + @JvmStatic + fun packagedCases(): List = requireNotNull( + UncontrolledSafetyGoldenTest::class.java.getResourceAsStream("/safety/manifest.json"), + ).use { jacksonObjectMapper().readValue(it) } + } + +} + +/** Golden expectations describe independent axes; proof citations must come from the engine. */ +data class SafetyGoldenCase( + val id: String, + val language: String, + val inputVariant: String, + val change: String, + val expected: SafetyGoldenExpectation, + val evidenceRequirements: List, + val sourcePair: SafetyGoldenSourcePair? = null, +) { + override fun toString(): String = id +} + +/** Expected verdict does not substitute for either control classification or individual proof status. */ +data class SafetyGoldenExpectation( + val control: ControlClassification, + val claims: Map, + val decision: AcceptanceDecision, + val basis: AcceptanceBasis, + val verdict: Verdict, + val diagnosticCodes: List, +) + +/** Actual compiler source fixtures, independently stored for each revision. */ +data class SafetyGoldenSourcePair(val baseResource: String, val headResource: String) diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/VerdictSchemaTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/VerdictSchemaTest.kt index ea67a2c8..3f6d9034 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/VerdictSchemaTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/VerdictSchemaTest.kt @@ -19,8 +19,8 @@ class VerdictSchemaTest { fun `packaged schema identifies the versioned draft 2020-12 contract`() { val document = mapper.readTree(AnalysisReports.schema()) assertEquals("https://json-schema.org/draft/2020-12/schema", document["\$schema"].asText()) - assertEquals("urn:io.johnsonlee.graphfold:verdict:1", document["\$id"].asText()) - assertEquals(1, report(passing())["schemaVersion"].asInt()) + assertEquals("urn:io.johnsonlee.graphfold:verdict:2", document["\$id"].asText()) + assertEquals(2, report(passing())["schemaVersion"].asInt()) } @Test @@ -52,6 +52,11 @@ class VerdictSchemaTest { "$definitions/diagnostic/properties/code/enum" to DiagnosticCode.entries, "$definitions/reason/properties/changes/items/enum" to ChangeKind.entries, "$definitions/reason/properties/code/enum" to ReasonCode.entries, + "$definitions/changeAssessment/properties/control/enum" to ControlClassification.entries, + "$definitions/changeAssessment/properties/decision/enum" to AcceptanceDecision.entries, + "$definitions/changeAssessment/properties/basis/enum" to AcceptanceBasis.entries, + "$definitions/safetyEvidence/properties/claim/enum" to SafetyClaim.entries, + "$definitions/safetyEvidence/properties/status/enum" to SafetyStatus.entries, "$definitions/selector/properties/kind/enum" to RuleSelectorKind.entries, "$definitions/ruleAudit/properties/selectionStatus/enum" to RuleSelectionStatus.entries, ) @@ -110,7 +115,9 @@ class VerdictSchemaTest { "/metadata" to "provedObligations", "/findings/0" to "locations", "/findings/0/evidence/0" to "location", "/findings/0/locations/0" to "revision", "/findings/0/locations/0/line" to "start", "/diagnostics/0" to "blocking", - "/reasons/0" to "changes", "/ruleAudits/0" to "selectionStatus", + "/reasons/0" to "assessment", "/reasons/0/assessment" to "control", + "/reasons/0/assessment" to "safety", "/reasons/0/assessment" to "decision", + "/reasons/0/assessment" to "basis", "/reasons/0" to "changes", "/ruleAudits/0" to "selectionStatus", "/ruleAudits/0" to "selectedCount", "/ruleAudits/0" to "matchedCallCount", "/ruleAudits/0/selector" to "query", "/ruleAudits/0/selectedNodes/0" to "nodeId", "/ruleAudits/0/selectedNodes/0/caller" to "descriptor", @@ -125,7 +132,7 @@ class VerdictSchemaTest { @Test fun `schema rejects invalid enums ranges types and nullable count states`() { val mutations = listOf( - "/schemaVersion" to "2", "/schemaVersion" to "\"1\"", "/verdict" to "\"UNKNOWN\"", + "/schemaVersion" to "1", "/schemaVersion" to "\"1\"", "/verdict" to "\"UNKNOWN\"", "/metadata/obligations" to "-1", "/metadata/provedObligations" to "1.5", "/findings/0/kind" to "\"OTHER\"", "/findings/0/id" to "\" \"", "/findings/0/locations" to "[]", "/findings/0/locations/0/revision" to "\"WORKTREE\"", diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafetyTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafetyTest.kt new file mode 100644 index 00000000..80a5b209 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/WholeChangeSafetyTest.kt @@ -0,0 +1,320 @@ +package io.johnsonlee.graphfold.engine + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.PropertyProof +import io.johnsonlee.graphfold.engine.source.ProofObligation +import io.johnsonlee.graphfold.engine.source.SymbolKind +import io.johnsonlee.graphfold.engine.snapshot.MethodAnalysisGap +import io.johnsonlee.graphfold.engine.snapshot.MethodSnapshot +import io.johnsonlee.graphfold.engine.snapshot.ProgramSnapshot +import org.junit.jupiter.api.Test +import sootup.core.model.MethodModifier +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** Comparator mechanics only: these hand-built summaries are not engine-produced safety evidence. */ +class WholeChangeSafetyTest { + private val identity = "Example.value()I" + private val safe = summary(identity) + + @Test + fun `complete relational proof accepts while retaining uncontrolled classification and cited assumptions`() { + val result = assess(safe, safe) + assertEquals(ControlClassification.UNCONTROLLED, result.control) + assertEquals(AcceptanceDecision.ACCEPTED, result.decision) + assertEquals(AcceptanceBasis.STATIC_SAFETY, result.basis) + assertEquals(7, result.safety.size) + for (revision in listOf("BASE", "HEAD")) { + val properties = result.safety.filter { it.subject == "$revision $identity" } + assertEquals(setOf(SafetyClaim.EFFECT_FREE, SafetyClaim.NONTHROWING, SafetyClaim.TERMINATING), + properties.map { it.claim }.toSet()) + properties.forEach { + assertEquals(SafetyStatus.PROVEN, it.status) + assertEquals(listOf("fixture evidence"), it.evidence) + assertEquals(listOf("fixture assumption"), it.assumptions) + } + } + val whole = whole(result) + assertEquals(SafetyStatus.PROVEN, whole.status) + assertEquals(listOf("fixture assumption"), whole.assumptions) + assertTrue(whole.evidence.single().contains(identity)) + assertTrue(whole.evidence.single().contains("return:1")) + } + + @Test + fun `every property status in either revision participates independently in acceptance`() { + for (revision in listOf("BASE", "HEAD")) { + for (property in listOf(SafetyClaim.EFFECT_FREE, SafetyClaim.NONTHROWING, SafetyClaim.TERMINATING)) { + ProofStatus.entries.forEach { status -> + assertPropertyStatus(revision, property, status) + } + } + } + } + + @Test + fun `different pure total return values are not whole change equivalence or a demonstrated hazard`() { + val result = assess(safe, safe.copy(semanticKey = "return:2")) + rejected(result) + assertTrue(result.safety.filter { it.claim != SafetyClaim.WHOLE_CHANGE_EQUIVALENT } + .all { it.status == SafetyStatus.PROVEN }) + assertFalse(result.safety.any { it.status == SafetyStatus.REFUTED }) + } + + @Test + fun `null semantic keys on either or both revisions cannot establish equivalence`() { + listOf(safe.copy(semanticKey = null) to safe, safe to safe.copy(semanticKey = null), + safe.copy(semanticKey = null) to safe.copy(semanticKey = null)).forEach { (base, head) -> + rejected(assess(base, head)) + } + } + + @Test + fun `missing added and removed method summaries leave whole change proof incomplete`() { + for ((base, head) in listOf(emptyMap() to mapOf(identity to safe), + mapOf(identity to safe) to emptyMap(), emptyMap() to emptyMap())) { + val result = WholeChangeSafety(base, head).assess(listOf(identity), emptyList(), setOf(identity)) + rejected(result) + assertTrue(whole(result).evidence.any { identity in it }) + } + } + + @Test + fun `declaration changes and unresolved gaps block otherwise complete summaries`() { + val proof = WholeChangeSafety(mapOf(identity to safe), mapOf(identity to safe)) + rejected(proof.assess(listOf(identity), emptyList(), emptySet())) + val gap = "Unknown external observation" + val result = proof.assess(listOf(identity), listOf(gap), setOf(identity)) + rejected(result) + assertTrue(whole(result).evidence.any { gap in it }) + } + + @Test + fun `every changed method class field and initialization obligation must be covered`() { + val unsafe = summary("Example.other()I").copy(semanticKey = "return:2") + val base = mapOf(identity to safe, unsafe.identity to unsafe.copy(semanticKey = "return:1")) + val head = mapOf(identity to safe, unsafe.identity to unsafe) + val proof = WholeChangeSafety(base, head) + listOf(unsafe.identity, "Example", "Example.field:I", "Class initialization dependencies of Example changed").forEach { + rejected(proof.assess(listOf(identity, it), emptyList(), setOf(identity, it))) + } + } + + @Test + fun `whole change assumptions include both revisions without discarding property citations`() { + val base = safe.copy(effectFree = safe.effectFree.copy(assumptions = listOf("BASE assumption", "shared"))) + val head = safe.copy(terminating = safe.terminating.copy(assumptions = listOf("HEAD assumption", "shared"))) + val result = assess(base, head) + assertEquals(setOf("BASE assumption", "HEAD assumption", "shared", "fixture assumption"), whole(result).assumptions.toSet()) + assertEquals(listOf("BASE assumption", "shared"), result.safety.single { + it.subject == "BASE $identity" && it.claim == SafetyClaim.EFFECT_FREE + }.assumptions) + assertEquals(listOf("HEAD assumption", "shared"), result.safety.single { + it.subject == "HEAD $identity" && it.claim == SafetyClaim.TERMINATING + }.assumptions) + } + + @Test + fun `empty differences are not a surviving uncontrolled change`() { + val proof = WholeChangeSafety(mapOf(identity to safe), mapOf(identity to safe)) + assertFailsWith { proof.assess(emptyList(), emptyList(), setOf(identity)) } + var evaluations = 0 + val comparison = SnapshotComparison(snapshot(identity), snapshot(identity), safety = { evaluations++; proof }) + assertNull(comparison.safetyAssessment(emptyList(), emptyList())) + assertEquals(0, evaluations) + assertNotNull(comparison.safetyAssessment(listOf(identity), emptyList())) + assertNotNull(comparison.safetyAssessment(listOf(identity), emptyList())) + assertEquals(1, evaluations) + assertNull(SnapshotComparison(snapshot(identity), snapshot(identity)).safetyAssessment(listOf(identity), emptyList())) + } + + @Test + fun `snapshot gate requires actual identical declarations in both unaligned inputs`() { + val base = snapshot(identity) + val proof = WholeChangeSafety(mapOf(identity to safe), mapOf(identity to safe)) + val variants = listOf(base.copy(declarations = emptyMap()), base.copy(declarations = mapOf(identity to "changed")), + base.copy(methods = emptyMap())) + variants.forEach { changed -> + rejected(assertNotNull(SnapshotComparison(base, changed, safety = { proof }) + .safetyAssessment(listOf(identity), emptyList()))) + rejected(assertNotNull(SnapshotComparison(changed, base, safety = { proof }) + .safetyAssessment(listOf(identity), emptyList()))) + } + assertEquals(AcceptanceDecision.ACCEPTED, assertNotNull(SnapshotComparison(base, base, safety = { proof }) + .safetyAssessment(listOf(identity), emptyList())).decision) + } + + @Test + fun `private member alignment does not manufacture matching safety declarations`() { + val renamed = "Example.renamed()I" + val base = snapshot(identity, MethodModifier.PRIVATE) + val head = snapshot(renamed, MethodModifier.PRIVATE) + // Deliberately provide summaries under either alias: input inventory still has to match literally. + val summaries = mapOf(identity to safe, renamed to safe.copy(identity = renamed)) + val proof = WholeChangeSafety(summaries, summaries) + val comparison = SnapshotComparison(base, head, safety = { proof }) + rejected(assertNotNull(comparison.safetyAssessment(listOf(identity), emptyList()))) + rejected(assertNotNull(comparison.safetyAssessment(listOf(renamed), emptyList()))) + } + + @Test + fun `equivalent folded output needs an independent ungapped pre fold difference to establish controls`() { + val same = snapshot(identity) + val changed = same.copy(methods = mapOf(identity to same.methods.getValue(identity) + .copy(canonical = "return 2", referenceCanonical = "return 2"))) + val incomplete = changed.copy(methodDiagnostics = mapOf(identity to listOf(MethodAnalysisGap("missing proof")))) + val cases = listOf( + SnapshotComparison(same, same) to ControlClassification.UNCONTROLLED, + SnapshotComparison(same, changed) to ControlClassification.CONTROLLED, + SnapshotComparison(same, incomplete) to ControlClassification.UNPROVEN, + ) + val obligation = ProofObligation(identity, SymbolKind.METHOD, "Example", emptyList()) + for ((baseline, expected) in cases) { + val comparison = SnapshotComparison(same, same, selectedControls = true, unfolded = { baseline }) + for (selected in listOf(null, obligation)) { + val result = comparison.equivalenceAssessment(selected) + assertEquals(expected, result.control) + assertEquals(AcceptanceDecision.ACCEPTED, result.decision) + assertEquals(if (expected == ControlClassification.CONTROLLED) AcceptanceBasis.CONTROL_COVERAGE + else AcceptanceBasis.REPRESENTATION_EQUIVALENCE, result.basis) + } + } + assertEquals(ControlClassification.UNPROVEN, SnapshotComparison(same, same, selectedControls = true) + .equivalenceAssessment(obligation).control) + } + + @Test + fun `zero selected controls never establish control coverage even with a contradictory removal certificate`() { + val removed = snapshot(identity, MethodModifier.PRIVATE).copy(controlRemovedMethods = setOf(identity)) + val obligation = ProofObligation(identity, SymbolKind.METHOD, "Example", emptyList()) + val controlled = SnapshotComparison(removed, removed, selectedControls = true) + assertTrue(controlled.isUnreachable(obligation)) + assertEquals(ControlClassification.CONTROLLED, controlled.equivalenceAssessment(obligation).control) + val uncontrolled = SnapshotComparison(removed, removed, selectedControls = false) + assertEquals(ControlClassification.UNCONTROLLED, uncontrolled.equivalenceAssessment(obligation).control) + assertEquals(ControlClassification.UNCONTROLLED, uncontrolled.equivalenceAssessment().control) + } + + @Test + fun `baseline comparison is lazy and only evaluated when needed for classification`() { + val same = snapshot(identity) + var evaluations = 0 + val baseline = { evaluations++; SnapshotComparison(same, same) } + SnapshotComparison(same, same, selectedControls = false, unfolded = baseline).equivalenceAssessment() + assertEquals(0, evaluations) + val removed = snapshot(identity, MethodModifier.PRIVATE).copy(controlRemovedMethods = setOf(identity)) + val obligation = ProofObligation(identity, SymbolKind.METHOD, "Example", emptyList()) + SnapshotComparison(removed, removed, selectedControls = true, unfolded = baseline).equivalenceAssessment(obligation) + assertEquals(0, evaluations) + val comparison = SnapshotComparison(same, same, selectedControls = true, unfolded = baseline) + comparison.equivalenceAssessment() + comparison.equivalenceAssessment(obligation) + assertEquals(1, evaluations) + } + + private fun assertPropertyStatus(revision: String, claim: SafetyClaim, status: ProofStatus) { + val property = PropertyProof(status, listOf("$revision $claim evidence"), listOf("$revision assumption")) + val changed = when (claim) { + SafetyClaim.EFFECT_FREE -> safe.copy(effectFree = property) + SafetyClaim.NONTHROWING -> safe.copy(nonThrowing = property) + SafetyClaim.TERMINATING -> safe.copy(terminating = property) + else -> error("Method property required") + } + val result = if (revision == "BASE") assess(changed, safe) else assess(safe, changed) + assertEquals(SafetyStatus.valueOf(status.name), result.safety.single { + it.subject == "$revision $identity" && it.claim == claim + }.status) + if (status == ProofStatus.PROVEN) assertEquals(AcceptanceDecision.ACCEPTED, result.decision) else rejected(result) + } + + private fun assess(base: MethodSafetySummary, head: MethodSafetySummary): ChangeAssessment = + WholeChangeSafety(mapOf(identity to base), mapOf(identity to head)).assess(listOf(identity), emptyList(), setOf(identity)) + + private fun whole(result: ChangeAssessment): SafetyEvidence = result.safety.single { it.claim == SafetyClaim.WHOLE_CHANGE_EQUIVALENT } + + private fun rejected(result: ChangeAssessment) { + assertEquals(ControlClassification.UNCONTROLLED, result.control) + assertEquals(AcceptanceDecision.REJECTED, result.decision) + assertEquals(AcceptanceBasis.INCOMPLETE_PROOF, result.basis) + assertEquals(SafetyStatus.UNPROVEN, whole(result).status) + } + + private fun summary(name: String): MethodSafetySummary { + val property = PropertyProof(ProofStatus.PROVEN, listOf("fixture evidence"), listOf("fixture assumption")) + return MethodSafetySummary(name, property, property, property, "return:1", emptySet()) + } + + private fun snapshot(name: String, modifier: MethodModifier = MethodModifier.PUBLIC): ProgramSnapshot = ProgramSnapshot( + mapOf(name to MethodSnapshot(name, "return 1", emptySet(), setOf(modifier), emptySet())), + mapOf("Example" to "class declaration", name to "unchanged declaration"), emptyList(), emptyList(), + ) +} + +/** Original-bytecode summaries cannot override selected folding in the method or its transitive proof dependencies. */ +class WholeChangeFoldSafetyTest { + private val identity = "Example.value()I" + private val dependency = "Example.helper()I" + private val transitive = "Example.enabled()Z" + + @Test + fun `folding the method itself invalidates its original bytecode relational proof`() { + val summaries = mapOf(identity to summary(emptySet())) + val result = WholeChangeSafety(summaries, summaries).assess( + listOf(identity), emptyList(), setOf(identity), setOf(identity), + ) + assertEquals(AcceptanceDecision.REJECTED, result.decision) + assertEquals(SafetyStatus.UNPROVEN, result.safety.last().status) + assertEquals(ControlClassification.UNCONTROLLED, result.control) + } + + @Test + fun `a folded transitive dependency in either revision invalidates the original summary`() { + val independent = summary(emptySet()) + val dependent = summary(setOf(dependency, transitive)) + for ((base, head) in listOf(independent to dependent, dependent to independent)) { + val proof = WholeChangeSafety(mapOf(identity to base), mapOf(identity to head)) + for (changed in listOf(dependency, transitive)) { + val rejected = proof.assess(listOf(identity), emptyList(), setOf(identity), setOf(changed)) + assertEquals(AcceptanceDecision.REJECTED, rejected.decision) + assertEquals(SafetyStatus.UNPROVEN, rejected.safety.last().status) + } + val unrelated = proof.assess(listOf(identity), emptyList(), setOf(identity), setOf("Other.enabled()Z")) + assertEquals(AcceptanceDecision.ACCEPTED, unrelated.decision) + assertEquals(AcceptanceBasis.STATIC_SAFETY, unrelated.basis) + assertEquals(ControlClassification.UNCONTROLLED, unrelated.control) + } + } + + @Test + fun `snapshot comparison guards both revision bodies and every retained summary dependency`() { + val summary = summary(setOf(dependency, transitive)) + val proof = WholeChangeSafety(mapOf(identity to summary), mapOf(identity to summary)) + val methods = listOf(identity, dependency, transitive).associateWith { + MethodSnapshot(it, "original $it", emptySet(), setOf(MethodModifier.PUBLIC), emptySet()) + } + val original = ProgramSnapshot(methods, methods.mapValues { "declaration" }, emptyList(), emptyList()) + val baseline = { SnapshotComparison(original, original) } + for (changed in listOf(identity, dependency, transitive)) { + val folded = original.copy(methods = methods + (changed to methods.getValue(changed).copy(canonical = "folded"))) + for ((base, head) in listOf(folded to original, original to folded)) { + val comparison = SnapshotComparison(base, head, safety = { proof }, selectedControls = true, unfolded = baseline) + val result = assertNotNull(comparison.safetyAssessment(listOf(identity), emptyList())) + assertEquals(AcceptanceDecision.REJECTED, result.decision) + } + } + val unknown = SnapshotComparison(original, original, safety = { proof }, selectedControls = true) + assertEquals(AcceptanceDecision.REJECTED, assertNotNull(unknown.safetyAssessment(listOf(identity), emptyList())).decision) + val unchanged = SnapshotComparison(original, original, safety = { proof }, selectedControls = true, unfolded = baseline) + assertEquals(AcceptanceDecision.ACCEPTED, assertNotNull(unchanged.safetyAssessment(listOf(identity), emptyList())).decision) + } + + private fun summary(dependencies: Set): MethodSafetySummary { + val property = PropertyProof(ProofStatus.PROVEN, listOf("fixture evidence")) + return MethodSafetySummary(identity, property, property, property, "return:42", dependencies) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/KotlinStaticSafetyAnalysisTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/KotlinStaticSafetyAnalysisTest.kt new file mode 100644 index 00000000..a0d03d7f --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/KotlinStaticSafetyAnalysisTest.kt @@ -0,0 +1,110 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import java.nio.file.Files +import java.nio.file.Path +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +/** Kotlin compiler output is analyzed directly; no fixture method is executed as evidence. */ +class KotlinStaticSafetyAnalysisTest { + @TempDir lateinit var temporary: Path + private val summaries by lazy { compile() } + + @Test fun `ordinary Kotlin refactorings carry whole method proofs without a selected control`() { + val plain = summary("identity", "(I)I") + val refactored = summary("plusZero", "(I)I") + assertComplete(refactored) + assertEquals(plain.semanticKey, refactored.semanticKey) + val changed = summary("zero", "(I)I") + assertComplete(changed) + assertNotEquals(plain.semanticKey, changed.semanticKey) + } + + @Test fun `Kotlin null guards preserve nullable parameter and boxed value evidence`() { + assertComplete(summary("guarded", "(Ljava/lang/String;)I")) + assertComplete(summary("guardedBox", "(Ljava/lang/Integer;)I")) + assertComplete(summary("shortCircuit", "(Ljava/lang/String;)Z")) + assertEquals(ProofStatus.UNPROVEN, summary("unsafe", "(Ljava/lang/String;)I").nonThrowing.status) + assertEquals(ProofStatus.UNPROVEN, summary("unbox", "(Ljava/lang/Integer;)I").nonThrowing.status) + assertEquals(ProofStatus.REFUTED, summary("nullCrash", "()I").nonThrowing.status) + } + + @Test fun `Kotlin compiler null checks require actual caller facts rather than annotations`() { + assertEquals(ProofStatus.UNPROVEN, summary("helper", "(Ljava/lang/String;)I").nonThrowing.status) + val caller = summary("safeCaller", "()I") + assertComplete(caller) + assertEquals(summary("two", "()I").semanticKey, caller.semanticKey) + assertComplete(summary("nullableEquals", "(Ljava/lang/String;)Z")) + assertComplete(summary("nullableReturnEquals", "()Z")) + assertEquals(ProofStatus.REFUTED, summary("internalNull", "()I").nonThrowing.status) + } + + @Test fun `Kotlin alternate branches cannot borrow a null guard from the safe path`() { + val join = summary("unsafeJoin", "(Ljava/lang/String;Z)I") + assertEquals(ProofStatus.UNPROVEN, join.nonThrowing.status) + assertNull(join.semanticKey) + assertComplete(summary("guardedJoin", "(Ljava/lang/String;Z)I")) + } + + private fun summary(name: String, descriptor: String) = summaries.getValue("KotlinSafety.$name$descriptor") + + private fun assertComplete(summary: MethodSafetySummary) { + assertEquals(ProofStatus.PROVEN, summary.effectFree.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.nonThrowing.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.terminating.status, summary.identity) + assertNotNull(summary.semanticKey, summary.identity) + } + + private fun compile(): Map { + val file = temporary.resolve("KotlinSafety.kt") + Files.writeString(file, SOURCE) + val classes = Files.createDirectory(temporary.resolve("classes")) + val library = Path.of(Unit::class.java.protectionDomain.codeSource.location.toURI()) + val output = ByteArrayOutputStream() + val result = PrintStream(output).use { stream -> + K2JVMCompiler().exec(stream, "-no-stdlib", "-no-reflect", "-classpath", library.toString(), + "-jvm-target", "17", "-d", classes.toString(), file.toString()) + } + assertEquals(ExitCode.OK, result, output.toString()) + return StaticSafetyAnalysis(classes, listOf(library)).use { it.summaries() } + } + + private companion object { + val SOURCE = """ + class KotlinSafety { + fun identity(x: Int): Int = x + fun plusZero(x: Int): Int = x + 0 + fun zero(x: Int): Int = 0 + fun guarded(x: String?): Int { if (x == null) return 0; return x.length } + fun guardedBox(x: Int?): Int { if (x == null) return 0; return x } + fun shortCircuit(x: String?): Boolean = x != null && x.length > 0 + fun unsafe(x: String?): Int = x!!.length + fun unbox(x: Int?): Int = x!! + fun nullCrash(): Int { val x: String? = null; return x!!.length } + private fun helper(x: String): Int = x.length + fun safeCaller(): Int = helper("ok") + fun two(): Int = 2 + fun nullableEquals(x: String?): Boolean = "Android".equals(x) + private fun platform(): String? = null + fun nullableReturnEquals(): Boolean = "Android".equals(platform()) + fun internalNull(): Int = platform()!!.length + fun unsafeJoin(x: String?, b: Boolean): Int { val v = if(b) x else "ok"; return v!!.length } + fun guardedJoin(x: String?, b: Boolean): Int { + if (x == null) return 0 + val v = if(b) x else "ok" + return v.length + } + } + """.trimIndent() + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyArithmeticTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyArithmeticTest.kt new file mode 100644 index 00000000..3df96fdd --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyArithmeticTest.kt @@ -0,0 +1,101 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import org.junit.jupiter.api.Test +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class SafetyArithmeticTest { + @Test + fun `integer constants preserve JVM wraparound division remainder and bit semantics`() { + val cases = listOf( + Operation(Opcodes.IADD, Int.MAX_VALUE, 1, Int.MIN_VALUE), + Operation(Opcodes.ISUB, Int.MIN_VALUE, 1, Int.MAX_VALUE), + Operation(Opcodes.IMUL, Int.MAX_VALUE, 2, -2), + Operation(Opcodes.IDIV, -7, 3, -2), Operation(Opcodes.IDIV, 7, -3, -2), + Operation(Opcodes.IDIV, Int.MIN_VALUE, -1, Int.MIN_VALUE), + Operation(Opcodes.IREM, -7, 3, -1), Operation(Opcodes.IREM, 7, -3, 1), + Operation(Opcodes.IREM, Int.MIN_VALUE, -1, 0), + Operation(Opcodes.IAND, 0b1100, 0b1010, 0b1000), + Operation(Opcodes.IOR, 0b1100, 0b1010, 0b1110), + Operation(Opcodes.IXOR, 0b1100, 0b1010, 0b0110), + Operation(Opcodes.ISHL, 1, 32, 1), Operation(Opcodes.ISHL, 1, -1, Int.MIN_VALUE), + Operation(Opcodes.ISHR, Int.MIN_VALUE, 31, -1), Operation(Opcodes.ISHR, -8, 33, -4), + Operation(Opcodes.IUSHR, -1, 1, Int.MAX_VALUE), Operation(Opcodes.IUSHR, Int.MIN_VALUE, 31, 1), + ) + cases.forEach { case -> + assertEquals(SafetyValue.integer(case.expected), safetyIntegerOperation(case.opcode, + SafetyValue.integer(case.left), SafetyValue.integer(case.right)), case.toString()) + } + } + + @Test + fun `zero divisor never fabricates an integer result for either division or remainder`() { + for (opcode in listOf(Opcodes.IDIV, Opcodes.IREM)) { + val result = assertNotNull(safetyIntegerOperation(opcode, SafetyValue.integer(7), SafetyValue.integer(0))) + assertNull(result.integer, "The interpreter must discharge the divisor obligation separately") + assertNotEquals(SafetyValue.integer(0), result) + } + } + + @Test + fun `neutral identities preserve the original value including unstable read identity`() { + val inputs = listOf(SafetyValue.unknown(Type.INT_TYPE, "arg:0", true), + SafetyValue.unknown(Type.INT_TYPE, "field:read1", false)) + inputs.forEach { value -> + val zero = SafetyValue.integer(0) + val one = SafetyValue.integer(1) + assertEquals(value, safetyIntegerOperation(Opcodes.IADD, zero, value)) + assertEquals(value, safetyIntegerOperation(Opcodes.IADD, value, zero)) + assertEquals(value, safetyIntegerOperation(Opcodes.ISUB, value, zero)) + assertEquals(value, safetyIntegerOperation(Opcodes.IMUL, one, value)) + assertEquals(value, safetyIntegerOperation(Opcodes.IMUL, value, one)) + } + } + + @Test + fun `symbolic operations preserve operator argument order and stability of both operands`() { + val left = SafetyValue.unknown(Type.INT_TYPE, "arg:0", true) + val right = SafetyValue.unknown(Type.INT_TYPE, "arg:1", true) + val results = INTEGER_OPERATIONS.keys.map { opcode -> + val result = assertNotNull(safetyIntegerOperation(opcode, left, right)) + assertNull(result.integer) + assertEquals(true, result.stable) + assertNotEquals(left, result) + assertNotEquals(right, result) + assertEquals(false, assertNotNull(safetyIntegerOperation(opcode, left.copy(stable = false), right)).stable) + assertEquals(false, assertNotNull(safetyIntegerOperation(opcode, left, right.copy(stable = false))).stable) + result.expression + } + assertEquals(INTEGER_OPERATIONS.size, results.distinct().size) + for (opcode in listOf(Opcodes.ISUB, Opcodes.IDIV, Opcodes.IREM, Opcodes.ISHL)) { + assertNotEquals(safetyIntegerOperation(opcode, left, right), safetyIntegerOperation(opcode, right, left)) + } + assertNotEquals(left, safetyIntegerOperation(Opcodes.IMUL, left, SafetyValue.integer(0))) + for (opcode in listOf(Opcodes.IADD, Opcodes.IMUL, Opcodes.ISUB)) { + val constant = SafetyValue.integer(2) + val mixed = assertNotNull(safetyIntegerOperation(opcode, constant, right)) + assertNull(mixed.integer) + assertNotEquals(constant, mixed) + assertNotEquals(right, mixed) + assertNotEquals(safetyIntegerOperation(opcode, SafetyValue.integer(3), right), mixed) + assertNotEquals(left, safetyIntegerOperation(opcode, left, constant)) + } + } + + @Test + fun `unsupported numeric operations remain unsupported with constant or symbolic operands`() { + val values = listOf(SafetyValue.integer(1), SafetyValue.unknown(Type.INT_TYPE, "arg:0", true)) + for (left in values) for (right in values) { + assertNull(safetyIntegerOperation(Opcodes.FADD, left, right)) + } + } + + private fun safetyIntegerOperation(opcode: Int, left: SafetyValue, right: SafetyValue): SafetyValue? = + INTEGER_OPERATIONS[opcode]?.evaluate(left, right) + + private data class Operation(val opcode: Int, val left: Int, val right: Int, val expected: Int) +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFilesBoundaryTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFilesBoundaryTest.kt new file mode 100644 index 00000000..a2cf44c9 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyClassFilesBoundaryTest.kt @@ -0,0 +1,131 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import java.nio.file.Files +import java.nio.file.Path +import java.util.jar.JarEntry +import java.util.jar.JarOutputStream +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Opcodes +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class SafetyClassFilesBoundaryTest { + @TempDir lateinit var temporary: Path + + @Test + fun `application definitions take precedence and declared dependency ordering is preserved`() { + val application = directory("application", mapOf("App" to definition("App"))) + val first = directory("first", mapOf("Library" to definition("Library", "First"))) + val second = archive("second", mapOf("Library.class" to definition("Library", "Second"), + "App.class" to definition("App", "Shadow"))) + SafetyClassFiles(application, listOf(first, second)).use { files -> + assertEquals("java/lang/Object", files.find("App")?.superName) + assertEquals("First", files.find("Library")?.superName) + assertEquals("First", files.find("Library")?.superName) + assertNull(files.find("kotlin/Unit"), "An available host class is not a declared dependency") + assertNull(files.find("missing/Type")) + } + SafetyClassFiles(application, listOf(second, first)).use { files -> + assertEquals("Second", files.find("Library")?.superName) + } + } + + @Test + fun `archive application inventory ignores resources and directories and resolves inherited exact signatures`() { + val app = archive("application", mapOf("folder/" to byteArrayOf(), "notes.txt" to byteArrayOf(1), + "Child.class" to definition("Child", "Parent"))) + val dependencies = directory("dependencies", mapOf( + "Parent" to definition("Parent", method = "value"), + "java/lang/Object" to definition("java/lang/Object", null), "Cycle" to definition("Cycle", "Cycle"))) + SafetyClassFiles(app, listOf(dependencies)).use { files -> + assertEquals(setOf("Child"), files.applications.keys) + assertEquals("Parent", files.method("Child", "value", "()I")?.first?.name) + assertNull(files.method("Child", "value", "()J")) + assertNull(files.method("Child", "other", "()I")) + assertNull(files.method("Cycle", "missing", "()V")) + assertNull(files.method("Absent", "missing", "()V")) + } + } + + @Test + fun `a declared abstract override does not borrow its parent implementation`() { + val writer = ClassWriter(0) + writer.visit(Opcodes.V17, Opcodes.ACC_PUBLIC or Opcodes.ACC_ABSTRACT, "Child", null, "Parent", null) + writer.visitMethod(Opcodes.ACC_PUBLIC or Opcodes.ACC_ABSTRACT, "value", "()I", null, null).visitEnd() + writer.visitEnd() + val app = directory("application", mapOf("Child" to writer.toByteArray())) + val parent = directory("parent", mapOf("Parent" to definition("Parent", method = "value", static = false))) + SafetyClassFiles(app, listOf(parent)).use { files -> + val resolved = assertNotNull(files.method("Child", "value", "()I")) + assertEquals("Child", resolved.first.name) + assertTrue(resolved.second.access and Opcodes.ACC_ABSTRACT != 0) + assertEquals(0, resolved.second.instructions.size()) + } + } + + @Test + fun `contracts cannot trust application or declared dependency shadows of platform types`() { + val app = directory("application", mapOf("java/lang/String" to definition("java/lang/String"))) + val dependency = archive("shadow", mapOf("java/lang/Integer.class" to definition("java/lang/Integer"))) + SafetyClassFiles(app, listOf(dependency)).use { files -> + assertFalse(files.supportsContract("java/lang/String")) + assertFalse(files.supportsContract("java/lang/Integer")) + assertTrue(files.supportsContract("java/lang/Object")) + assertNull(files.find("java/lang/Object"), "Standard narrow contracts do not imply host bytecode fallback") + assertFalse(files.supportsContract("custom/Library")) + } + } + + @Test + fun `Kotlin null contracts require one byte-identical declared runtime definition`() { + val owner = "kotlin/jvm/internal/Intrinsics" + val actual = EngineRuntimeClasses.requireBytes(owner) + val trusted = directory("trusted", mapOf(owner to actual)) + val shadow = archive("shadow", mapOf("$owner.class" to definition(owner))) + val empty = directory("empty", emptyMap()) + SafetyClassFiles(empty, listOf(trusted)).use { assertTrue(it.supportsContract(owner)) } + SafetyClassFiles(empty, listOf(shadow)).use { assertFalse(it.supportsContract(owner)) } + SafetyClassFiles(empty, listOf(trusted, trusted)).use { assertFalse(it.supportsContract(owner)) } + SafetyClassFiles(empty, emptyList()).use { assertFalse(it.supportsContract(owner)) } + SafetyClassFiles(trusted, emptyList()).use { assertFalse(it.supportsContract(owner)) } + assertNotNull(EngineRuntimeClasses.bytes(owner)) + } + + private fun directory(name: String, classes: Map): Path { + val root = Files.createDirectory(temporary.resolve(name)) + Files.createDirectory(root.resolve("empty")) + Files.writeString(root.resolve("ignored.txt"), "not a class") + classes.forEach { (owner, bytes) -> + val file = root.resolve("$owner.class") + Files.createDirectories(file.parent) + Files.write(file, bytes) + } + return root + } + + private fun archive(name: String, entries: Map): Path { + val path = temporary.resolve("$name.jar") + JarOutputStream(Files.newOutputStream(path)).use { jar -> + entries.forEach { (entry, bytes) -> jar.putNextEntry(JarEntry(entry)); jar.write(bytes); jar.closeEntry() } + } + return path + } + + private fun definition( + owner: String, parent: String? = "java/lang/Object", method: String? = null, static: Boolean = true, + ): ByteArray { + val writer = ClassWriter(0) + writer.visit(Opcodes.V17, Opcodes.ACC_PUBLIC, owner, null, parent, null) + val access = Opcodes.ACC_PUBLIC or (if (static) Opcodes.ACC_STATIC else 0) + if (method != null) writer.visitMethod(access, method, "()I", null, null).apply { + visitCode(); visitInsn(Opcodes.ICONST_1); visitInsn(Opcodes.IRETURN); visitMaxs(1, if (static) 0 else 1); visitEnd() + } + writer.visitEnd() + return writer.toByteArray() + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyConditionTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyConditionTest.kt new file mode 100644 index 00000000..8b929e7e --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyConditionTest.kt @@ -0,0 +1,182 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import org.junit.jupiter.api.Test +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class SafetyConditionTest { + private val argument = SafetyValue.unknown(Type.INT_TYPE, "arg:0", true) + private val reference = SafetyValue.unknown(Type.getObjectType("java/lang/Object"), "arg:1", true) + + @Test + fun `unsupported branch operators do not invent a known comparison result`() { + assertNull(SafetyCondition(Opcodes.JSR, SafetyValue.integer(1), SafetyValue.integer(2)).known()) + } + + @Test + fun `integer comparisons use signed JVM ordering including extreme values`() { + val comparisons = listOf( + listOf(Opcodes.IFLT, Opcodes.IF_ICMPLT) to listOf(true, false, false), + listOf(Opcodes.IFLE, Opcodes.IF_ICMPLE) to listOf(true, true, false), + listOf(Opcodes.IFGT, Opcodes.IF_ICMPGT) to listOf(false, false, true), + listOf(Opcodes.IFGE, Opcodes.IF_ICMPGE) to listOf(false, true, true), + ) + for ((opcodes, expected) in comparisons) for (opcode in opcodes) { + val actual = listOf(Int.MIN_VALUE, 0, Int.MAX_VALUE).map { + SafetyCondition(opcode, SafetyValue.integer(it), SafetyValue.integer(0)).known() + } + assertEquals(expected, actual) + assertNull(SafetyCondition(opcode, argument, SafetyValue.integer(0)).known()) + assertNull(SafetyCondition(opcode, SafetyValue.integer(0), argument).known()) + } + } + + @Test + fun `equality distinguishes constants from unknown values and preserves negation`() { + for ((equal, unequal) in listOf(Opcodes.IFEQ to Opcodes.IFNE, Opcodes.IF_ICMPEQ to Opcodes.IF_ICMPNE)) { + val zero = SafetyValue.integer(0) + val knownZero = argument.copy(integer = 0) + assertEquals(true, SafetyCondition(equal, knownZero, zero).known()) + assertEquals(false, SafetyCondition(unequal, knownZero, zero).known()) + assertEquals(false, SafetyCondition(equal, SafetyValue.integer(1), zero).known()) + assertEquals(true, SafetyCondition(unequal, SafetyValue.integer(1), zero).known()) + assertNull(SafetyCondition(equal, argument, zero).known()) + assertNull(SafetyCondition(unequal, zero, argument).known()) + } + } + + @Test + fun `reference identity and nullness do not invent aliases for unknown references`() { + val nonnull = reference.copy(nullness = ReturnNullness.NON_NULL) + val nullAlias = reference.copy(nullness = ReturnNullness.NULL) + val cases = listOf(SafetyValue.NULL to nonnull, nonnull to SafetyValue.NULL, + SafetyValue.NULL to nullAlias, nullAlias to SafetyValue.NULL) + for ((left, right) in cases) { + val expected = left.nullness == right.nullness + assertEquals(expected, SafetyCondition(Opcodes.IF_ACMPEQ, left, right).known()) + assertEquals(!expected, SafetyCondition(Opcodes.IF_ACMPNE, left, right).known()) + } + assertEquals(true, SafetyCondition(Opcodes.IF_ACMPEQ, reference, reference).known()) + assertNull(SafetyCondition(Opcodes.IF_ACMPEQ, reference, reference.copy(expression = "arg:2")).known()) + assertNull(SafetyCondition(Opcodes.IFNULL, reference, SafetyValue.NULL).known()) + assertNull(SafetyCondition(Opcodes.IF_ACMPNE, SafetyValue.NULL, reference).known()) + assertEquals(true, SafetyCondition(Opcodes.IFNULL, nullAlias, SafetyValue.NULL).known()) + assertEquals(true, SafetyCondition(Opcodes.IFNONNULL, nonnull, SafetyValue.NULL).known()) + } + + @Test + fun `null guards refine a local snapshot without leaking facts into siblings or rereads`() { + val original = SafetyState(mutableMapOf(0 to reference)) + val condition = SafetyCondition(Opcodes.IFNONNULL, reference, SafetyValue.NULL) + val present = assertNotNull(condition.refine(original, true)) + val absent = assertNotNull(condition.refine(original, false)) + assertEquals(ReturnNullness.NON_NULL, present.resolve(reference).nullness) + assertEquals(ReturnNullness.NULL, absent.resolve(reference).nullness) + assertEquals(ReturnNullness.UNKNOWN, original.resolve(reference).nullness) + val reversed = SafetyCondition(Opcodes.IF_ACMPEQ, SafetyValue.NULL, reference) + assertEquals(ReturnNullness.NULL, assertNotNull(reversed.refine(original, true)).resolve(reference).nullness) + val read = reference.copy(expression = "field:read1", stable = false) + val guardedRead = assertNotNull(SafetyCondition(Opcodes.IFNULL, read, SafetyValue.NULL).refine(original, false)) + assertEquals(ReturnNullness.NON_NULL, guardedRead.resolve(read).nullness) + assertEquals(ReturnNullness.UNKNOWN, guardedRead.resolve(read.copy(expression = "field:read2")).nullness) + assertFalse(guardedRead.feasible, "An arbitrary field result does not itself establish a feasible witness") + val aliases = assertNotNull(SafetyCondition(Opcodes.IF_ACMPEQ, reference, reference.copy(expression = "arg:2")) + .refine(original, true)) + assertTrue(aliases.nullFacts.isEmpty()) + assertFalse(aliases.feasible) + } + + @Test + fun `integer guards exclude impossible branches without mutating the prior path`() { + val original = SafetyState(mutableMapOf(0 to argument)) + val zero = SafetyValue.integer(0) + val equality = SafetyCondition(Opcodes.IF_ICMPEQ, argument, zero) + val notZero = assertNotNull(equality.refine(original, false)) + assertNull(equality.refine(notZero, true)) + val zeroPath = assertNotNull(equality.refine(original, true)) + assertEquals(0, zeroPath.resolve(argument).integer) + assertNull(original.resolve(argument).integer) + assertTrue(original.excludedIntegers.isEmpty()) + val reversed = SafetyCondition(Opcodes.IF_ICMPNE, zero, argument) + assertEquals(0, assertNotNull(reversed.refine(original, false)).resolve(argument).integer) + val notOne = assertNotNull(SafetyCondition(Opcodes.IF_ICMPEQ, SafetyValue.integer(1), argument).refine(notZero, false)) + assertEquals(setOf(0, 1), notOne.excludedIntegers[argument.expression]) + assertEquals(setOf(0), notZero.excludedIntegers[argument.expression]) + val symbolic = argument.copy(expression = "strlen(arg:1)") + assertFalse(assertNotNull(SafetyCondition(Opcodes.IFNE, symbolic, zero).refine(original, true)).feasible) + assertFalse(assertNotNull(SafetyCondition(Opcodes.IF_ICMPEQ, argument, argument.copy(expression = "arg:2")) + .refine(original, true)).feasible) + assertFalse(assertNotNull(SafetyCondition(Opcodes.IFGT, argument, zero).refine(original, true)).feasible) + } + + @Test + fun `negated equality and exchanged branches express the same relational result`() { + val yes = evaluation(SafetyValue.integer(10)) + val no = evaluation(SafetyValue.integer(20)) + val equal = SafetyCondition(Opcodes.IF_ICMPEQ, argument, SafetyValue.integer(0)) + val unequal = SafetyCondition(Opcodes.IF_ICMPNE, SafetyValue.integer(0), argument) + assertEquals(equal.key, unequal.key) + assertEquals(equal.combine(yes, no).semanticKey(), unequal.combine(no, yes).semanticKey()) + val ordering = SafetyCondition(Opcodes.IF_ICMPLT, argument, SafetyValue.integer(0)) + assertNotEquals(ordering.key, SafetyCondition(Opcodes.IF_ICMPLT, SafetyValue.integer(0), argument).key) + assertNotEquals(equal.key, ordering.key) + assertNull(equal.copy(left = argument.copy(stable = false)).combine(yes, no).semanticKey()) + assertNull(equal.copy(right = SafetyValue.integer(0).copy(stable = false)).combine(yes, no).semanticKey()) + assertNull(equal.combine(yes.copy(value = yes.value?.copy(stable = false)), no).semanticKey()) + assertNull(equal.combine(yes, no.copy(value = no.value?.copy(stable = false))).semanticKey()) + } + + @Test + fun `joins retain only shared nullness constants and exact types`() { + val condition = SafetyCondition(Opcodes.IFNE, argument, SafetyValue.integer(0)) + val same = evaluation(SafetyValue.string("value")) + assertEquals(same.value, condition.combine(same, same).value) + val upcast = evaluation(assertNotNull(same.value).copy(type = Type.getObjectType("java/lang/Object"))) + val alias = assertNotNull(condition.combine(same, upcast).value) + assertEquals(ReturnNullness.NON_NULL, alias.nullness) + assertEquals("java/lang/String", alias.exactType) + val differentString = evaluation(SafetyValue.string("other")) + val joinedStrings = assertNotNull(condition.combine(same, differentString).value) + assertNull(joinedStrings.string) + assertEquals("java/lang/String", joinedStrings.exactType) + val nullable = assertNotNull(condition.combine(same, evaluation(SafetyValue.NULL)).value) + assertEquals(ReturnNullness.UNKNOWN, nullable.nullness) + assertNull(nullable.exactType) + val first = reference.copy(expression = "box1", nullness = ReturnNullness.NON_NULL, + exactType = "java/lang/Integer", fresh = true, boxed = SafetyValue.integer(1)) + val second = first.copy(expression = "box2", boxed = SafetyValue.integer(2)) + val boxes = assertNotNull(condition.combine(evaluation(first), evaluation(second)).value) + assertNull(boxes.boxed) + assertFalse(boxes.fresh) + val unstable = condition.combine(same, evaluation(assertNotNull(same.value).copy(stable = false))) + assertNull(unstable.semanticKey()) + } + + @Test + fun `joins retain unsupported branch evidence and never fabricate a return value`() { + val condition = SafetyCondition(Opcodes.IFNE, argument, SafetyValue.integer(0)) + val safe = evaluation(SafetyValue.integer(1)) + val unknown = SafetyEvaluation(null, SafetyFacts().unknown("unsupported branch")) + for (result in listOf(condition.combine(safe, unknown), condition.combine(unknown, safe))) { + assertNull(result.value) + assertNull(result.semanticKey()) + assertEquals(ProofStatus.UNPROVEN, result.facts.effects.status) + assertTrue(result.facts.effects.evidence.contains("unsupported branch")) + } + assertEquals(safe, condition.combine(null, safe)) + assertEquals(safe, condition.combine(safe, null)) + assertFailsWith { condition.combine(null, null) } + assertEquals(safe.value, condition.combine(safe, safe).value) + } + + private fun evaluation(value: SafetyValue) = SafetyEvaluation(value, SafetyFacts()) +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContractBoundaryTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContractBoundaryTest.kt new file mode 100644 index 00000000..4bb7cbea --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyContractBoundaryTest.kt @@ -0,0 +1,86 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import io.johnsonlee.graphfold.engine.safety.ReturnNullness +import org.junit.jupiter.api.Test +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.MethodInsnNode +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class SafetyContractBoundaryTest { + @Test + fun `literal string operations preserve exact results and unsupported methods have no contract`() { + val length = call("java/lang/String", "length", "()I") + assertEquals(3, safetyContract(length, SafetyValue.string("abc"), emptyList())?.value?.integer) + val unknown = SafetyValue("input", Type.getObjectType("java/lang/String"), ReturnNullness.NON_NULL) + assertEquals("strlen(input)", safetyContract(length, unknown, emptyList())?.value?.expression) + assertNull(safetyContract(length, null, emptyList())) + assertNull(safetyContract(call("java/lang/String", "intern", "()Ljava/lang/String;"), unknown, emptyList())) + } + + @Test + fun `string equality separates null literals exact nonstrings and symbolic strings`() { + val equals = call("java/lang/String", "equals", "(Ljava/lang/Object;)Z") + val literal = SafetyValue.string("value") + fun equality(receiver: SafetyValue, argument: SafetyValue) = + assertNotNull(safetyContract(equals, receiver, listOf(argument))?.value) + assertEquals(0, equality(literal, SafetyValue.NULL).integer) + assertEquals(1, equality(literal, SafetyValue.string("value")).integer) + assertEquals(0, equality(literal, SafetyValue.string("different")).integer) + val objectValue = SafetyValue("object", Type.getObjectType("example/Object"), ReturnNullness.NON_NULL, + exactType = "example/Object") + assertEquals(0, equality(literal, objectValue).integer) + val symbolic = SafetyValue("text", Type.getObjectType("java/lang/String"), ReturnNullness.NON_NULL, + exactType = "java/lang/String") + assertNull(equality(literal, symbolic).integer) + assertNull(equality(symbolic, literal).integer) + assertFalse(equality(symbolic.copy(stable = false), symbolic).stable) + assertFalse(equality(symbolic, symbolic.copy(stable = false, exactType = null)).stable) + } + + @Test + fun `boxing contract retains primitive payload but never applies to a nonstatic invocation`() { + val boxing = call("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", Opcodes.INVOKESTATIC) + val primitive = SafetyValue.integer(7) + val boxed = assertNotNull(safetyContract(boxing, null, listOf(primitive))?.value) + assertEquals(ReturnNullness.NON_NULL, boxed.nullness) + assertEquals(primitive, boxed.boxed) + val unboxing = call("java/lang/Integer", "intValue", "()I") + assertEquals(primitive, safetyContract(unboxing, boxed, emptyList())?.value) + val parameter = SafetyValue("parameter", boxed.type, ReturnNullness.NON_NULL) + assertEquals("unbox:java/lang/Integer.intValue()I(parameter)", + safetyContract(unboxing, parameter, emptyList())?.value?.expression) + assertNull(safetyContract(unboxing, null, emptyList())) + assertNull(safetyContract(boxing.apply { opcode = Opcodes.INVOKEVIRTUAL }, parameter, listOf(primitive))) + } + + @Test + fun `Kotlin null checks distinguish known null from unsupported nullness without assuming annotations`() { + val check = call("kotlin/jvm/internal/Intrinsics", "checkNotNull", "(Ljava/lang/Object;)V", Opcodes.INVOKESTATIC) + val known = assertNotNull(safetyContract(check, null, listOf(SafetyValue.string("safe")))) + assertEquals(ProofStatus.PROVEN, known.facts.exceptions.status) + assertEquals(SafetyValue.VOID, known.value) + val nullValue = assertNotNull(safetyContract(check, null, listOf(SafetyValue.NULL))) + assertEquals(ProofStatus.REFUTED, nullValue.facts.exceptions.status) + assertNull(nullValue.value) + val unknown = SafetyValue.unknown(Type.getObjectType("java/lang/Object"), "parameter") + assertEquals(ProofStatus.UNPROVEN, safetyContract(check, null, listOf(unknown))?.facts?.exceptions?.status) + assertNull(safetyContract(check.apply { opcode = Opcodes.INVOKEVIRTUAL }, unknown, listOf(unknown))) + } + + @Test + fun `Object initialization contract requires invokespecial and unknown library names remain unsupported`() { + val initialization = call("java/lang/Object", "", "()V", Opcodes.INVOKESPECIAL) + assertEquals(SafetyValue.VOID, safetyContract(initialization, SafetyValue("this", Type.getObjectType("java/lang/Object"), + ReturnNullness.NON_NULL), emptyList())?.value) + assertNull(safetyContract(initialization.apply { opcode = Opcodes.INVOKESTATIC }, null, emptyList())) + assertNull(safetyContract(call("example/Library", "length", "()I"), SafetyValue.string("safe"), emptyList())) + } + + private fun call(owner: String, name: String, descriptor: String, opcode: Int = Opcodes.INVOKEVIRTUAL) = + MethodInsnNode(opcode, owner, name, descriptor, false) +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyNormalCompletionTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyNormalCompletionTest.kt new file mode 100644 index 00000000..fd12fb3a --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyNormalCompletionTest.kt @@ -0,0 +1,20 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.PropertyProof +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +/** Domain mechanics: independent status combinations, not engine-produced safety certificates. */ +class SafetyNormalCompletionTest { + @Test fun `normal completion requires both nonthrowing and terminating proofs independently`() { + for (exceptions in ProofStatus.entries) for (termination in ProofStatus.entries) { + val facts = SafetyFacts( + exceptions = PropertyProof(exceptions, listOf("Domain status combination")), + termination = PropertyProof(termination, listOf("Domain status combination")), + ) + assertEquals(exceptions == ProofStatus.PROVEN && termination == ProofStatus.PROVEN, + facts.normalCompletionProven, "$exceptions / $termination") + } + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecodeTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecodeTest.kt new file mode 100644 index 00000000..9c891f81 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/SafetyResidualBytecodeTest.kt @@ -0,0 +1,136 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import org.junit.jupiter.api.Test +import org.objectweb.asm.ConstantDynamic +import org.objectweb.asm.Handle +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.FrameNode +import org.objectweb.asm.tree.IincInsnNode +import org.objectweb.asm.tree.InsnNode +import org.objectweb.asm.tree.IntInsnNode +import org.objectweb.asm.tree.InvokeDynamicInsnNode +import org.objectweb.asm.tree.JumpInsnNode +import org.objectweb.asm.tree.LabelNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.LineNumberNode +import org.objectweb.asm.tree.LookupSwitchInsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.MultiANewArrayInsnNode +import org.objectweb.asm.tree.TableSwitchInsnNode +import org.objectweb.asm.tree.TypeInsnNode +import org.objectweb.asm.tree.VarInsnNode +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class SafetyResidualBytecodeTest { + @Test + fun `unvisited operands retain field method type local increment and allocation differences`() { + val changes = listOf( + IntInsnNode(Opcodes.BIPUSH, 7) to IntInsnNode(Opcodes.BIPUSH, 8), + VarInsnNode(Opcodes.ILOAD, 1) to VarInsnNode(Opcodes.ILOAD, 2), + TypeInsnNode(Opcodes.CHECKCAST, "Left") to TypeInsnNode(Opcodes.CHECKCAST, "Right"), + FieldInsnNode(Opcodes.GETSTATIC, "Owner", "before", "I") to FieldInsnNode(Opcodes.GETSTATIC, "Owner", "after", "I"), + MethodInsnNode(Opcodes.INVOKESTATIC, "Owner", "before", "()V", false) to + MethodInsnNode(Opcodes.INVOKESTATIC, "Owner", "after", "()V", false), + IincInsnNode(1, 1) to IincInsnNode(1, -1), + MultiANewArrayInsnNode("[[I", 1) to MultiANewArrayInsnNode("[[I", 2), + InsnNode(Opcodes.IADD) to InsnNode(Opcodes.ISUB), + ) + changes.forEach { (before, after) -> assertNotEquals(fingerprint(before), fingerprint(after)) } + } + + @Test + fun `bootstrap arguments handles descriptors and floating raw bits remain part of residual identity`() { + val handle = Handle(Opcodes.H_INVOKESTATIC, "Bootstrap", "link", "()Ljava/lang/Object;", false) + assertNotEquals(fingerprint(InvokeDynamicInsnNode("value", "()I", handle, "before")), + fingerprint(InvokeDynamicInsnNode("value", "()I", handle, "after"))) + val other = Handle(Opcodes.H_INVOKESTATIC, "Bootstrap", "other", "()Ljava/lang/Object;", false) + assertNotEquals(fingerprint(LdcInsnNode(handle)), fingerprint(LdcInsnNode(other))) + assertNotEquals(fingerprint(LdcInsnNode(Type.getObjectType("Before"))), + fingerprint(LdcInsnNode(Type.getObjectType("After")))) + assertNotEquals(fingerprint(LdcInsnNode(ConstantDynamic("value", "I", handle, "before"))), + fingerprint(LdcInsnNode(ConstantDynamic("value", "I", handle, "after")))) + assertNotEquals(fingerprint(LdcInsnNode(ConstantDynamic("value", "I", handle))), + fingerprint(LdcInsnNode(ConstantDynamic("value", "I", handle, "argument")))) + assertNotEquals(fingerprint(LdcInsnNode(0.0f)), fingerprint(LdcInsnNode(-0.0f))) + assertNotEquals(fingerprint(LdcInsnNode(0.0)), fingerprint(LdcInsnNode(-0.0))) + } + + @Test + fun `switch keys ranges and target blocks cannot lose inactive control flow differences`() { + assertNotEquals(controlFlow("lookup", false), controlFlow("lookup", true)) + assertNotEquals(controlFlow("table", false), controlFlow("table", true)) + assertNotEquals(controlFlow("jump", false), controlFlow("jump", true)) + } + + @Test + fun `line frame and label metadata do not change unchanged residual code`() { + val plain = MethodNode().apply { + instructions.add(InsnNode(Opcodes.RETURN)) + instructions.add(IntInsnNode(Opcodes.BIPUSH, 7)) + instructions.add(InsnNode(Opcodes.IRETURN)) + } + val start = LabelNode() + val decorated = MethodNode().apply { + instructions.add(InsnNode(Opcodes.RETURN)) + instructions.add(start) + instructions.add(LineNumberNode(70, start)) + instructions.add(FrameNode(Opcodes.F_SAME, 0, null, 0, null)) + instructions.add(IntInsnNode(Opcodes.BIPUSH, 7)) + instructions.add(InsnNode(Opcodes.IRETURN)) + } + assertEquals(SafetyResidualBytecode(plain).fingerprint(setOf(0)), + SafetyResidualBytecode(decorated).fingerprint(setOf(0))) + assertNull(SafetyResidualBytecode(decorated).fingerprint(setOf(0, 4, 5))) + assertNull(SafetyResidualBytecode(MethodNode()).fingerprint(emptySet())) + } + + @Test + fun `a trailing target remains distinct from a target containing instructions`() { + fun method(trailing: Boolean): MethodNode { + val end = LabelNode() + return MethodNode().apply { + instructions.add(JumpInsnNode(Opcodes.GOTO, end)) + if (trailing) instructions.add(InsnNode(Opcodes.RETURN)) + instructions.add(end) + if (!trailing) instructions.add(InsnNode(Opcodes.RETURN)) + } + } + assertNotEquals(SafetyResidualBytecode(method(true)).fingerprint(emptySet()), + SafetyResidualBytecode(method(false)).fingerprint(emptySet())) + } + + private fun fingerprint(instruction: AbstractInsnNode): String { + val method = MethodNode().apply { + instructions.add(InsnNode(Opcodes.RETURN)) + instructions.add(instruction) + } + return assertNotNull(SafetyResidualBytecode(method).fingerprint(setOf(0))) + } + + private fun controlFlow(kind: String, changed: Boolean): String { + val first = LabelNode() + val second = LabelNode() + val branch = when (kind) { + "lookup" -> LookupSwitchInsnNode(first, intArrayOf(if (changed) 2 else 1), arrayOf(second)) + "table" -> TableSwitchInsnNode(if (changed) 2 else 1, if (changed) 2 else 1, first, second) + else -> JumpInsnNode(Opcodes.GOTO, if (changed) first else second) + } + val method = MethodNode().apply { + instructions.add(branch) + instructions.add(first) + instructions.add(InsnNode(Opcodes.ICONST_0)) + instructions.add(InsnNode(Opcodes.IRETURN)) + instructions.add(second) + instructions.add(InsnNode(Opcodes.ICONST_1)) + instructions.add(InsnNode(Opcodes.IRETURN)) + } + return assertNotNull(SafetyResidualBytecode(method).fingerprint(emptySet())) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysisTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysisTest.kt new file mode 100644 index 00000000..6cb253cd --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyAnalysisTest.kt @@ -0,0 +1,181 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import javax.tools.ToolProvider +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** Fixtures supply bytecode, never expected certificates or runtime observations. */ +class StaticSafetyAnalysisTest { + @TempDir lateinit var temporary: Path + private val summaries by lazy { analyze(SOURCE) } + + @Test fun `whole method keys distinguish equivalent computations from pure changed results`() { + val identity = summary("identity", "(I)I") + listOf("plusZero", "dead", "helperCaller", "boxed").forEach { name -> + val result = summary(name, "(I)I") + assertComplete(result) + assertEquals(identity.semanticKey, result.semanticKey, name) + } + val changed = summary("zero", "(I)I") + assertComplete(changed) + assertNotEquals(identity.semanticKey, changed.semanticKey) + } + + @Test fun `null guards and short circuit edges prove dereferences with local facts`() { + listOf("guarded" to "(Ljava/lang/String;)I", "shortCircuit" to "(Ljava/lang/String;)Z", + "guardedJoin" to "(Ljava/lang/String;Z)I").forEach { (name, descriptor) -> assertComplete(summary(name, descriptor)) } + assertEquals(ProofStatus.UNPROVEN, summary("unsafeJoin", "(Ljava/lang/String;Z)I").nonThrowing.status) + assertEquals(ProofStatus.UNPROVEN, summary("unguarded", "(Ljava/lang/String;)I").nonThrowing.status) + assertEquals(ProofStatus.REFUTED, summary("literalNull", "()I").nonThrowing.status) + } + + @Test fun `literal equals permits nullable final argument but not hazardous argument evaluation`() { + assertComplete(summary("equalsNullable", "(Ljava/lang/String;)Z")) + assertEquals(ProofStatus.UNPROVEN, summary("equalsArgument", "(Ljava/lang/String;)Z").nonThrowing.status) + assertEquals(ProofStatus.REFUTED, summary("internalNull", "()I").nonThrowing.status) + assertEquals(ProofStatus.UNPROVEN, summary("unbox", "(Ljava/lang/Integer;)I").nonThrowing.status) + assertEquals(ProofStatus.REFUTED, summary("unboxNull", "()I").nonThrowing.status) + } + + @Test fun `callee facts include effects unsupported dispatch recursion and initialization`() { + assertEquals(ProofStatus.REFUTED, summary("write", "(I)I").effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary("recursive", "(I)I").terminating.status) + assertEquals(ProofStatus.UNPROVEN, summary("loop", "(I)I").terminating.status) + assertEquals(ProofStatus.UNPROVEN, summary("unknown", "(Ljava/util/function/Supplier;)Ljava/lang/Object;").effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary("caught", "(Ljava/lang/String;)I").nonThrowing.status) + assertEquals(ProofStatus.REFUTED, summary("initialized", "()I").nonThrowing.status) + assertNull(summary("write", "(I)I").semanticKey) + } + + @Test fun `stored getter result can be guarded but repeated mutable getters cannot share facts`() { + val stored = summary("storedGetter", "(LSafetyExample\$Box;)I") + assertEquals(ProofStatus.PROVEN, stored.nonThrowing.status) + assertEquals(ProofStatus.PROVEN, stored.effectFree.status) + assertNull(stored.semanticKey) + val repeated = summary("repeatedGetter", "(LSafetyExample\$Box;)I") + assertEquals(ProofStatus.UNPROVEN, repeated.nonThrowing.status) + assertNull(repeated.semanticKey) + assertEquals(ProofStatus.UNPROVEN, summary("allocation", "()Ljava/lang/Object;").effectFree.status) + } + + @Test fun `all production methods include uninvoked hazardous callback bodies`() { + val callback = summaries.values.single { it.identity.contains("lambda\$callback\$") } + assertEquals(ProofStatus.REFUTED, callback.nonThrowing.status) + assertEquals(ProofStatus.UNPROVEN, summary("callback", "()Ljava/lang/Runnable;").terminating.status) + assertTrue(summaries.keys.any { it.contains("unusedHazard") }) + assertEquals(ProofStatus.REFUTED, summary("unusedHazard", "()I").nonThrowing.status) + } + + @Test fun `guard joins do not leak one branch constants into caller summaries`() { + val value = summary("joinedValue", "(I)I") + assertComplete(value) + assertEquals(summary("identity", "(I)I").semanticKey, value.semanticKey) + assertNotEquals(summary("zero", "(I)I").semanticKey, summary("joinedCaller", "(I)I").semanticKey) + } + + @Test fun `boxing retains primitive payload but never invents reference identity`() { + val compared = summary("sameBoxes", "(I)Z") + assertEquals(ProofStatus.PROVEN, compared.effectFree.status) + assertEquals(ProofStatus.PROVEN, compared.nonThrowing.status) + assertNull(compared.semanticKey) + assertComplete(summary("sameAlias", "(I)Z")) + assertEquals(summary("identity", "(I)I").semanticKey, summary("boxed", "(I)I").semanticKey) + } + + @Test fun `unproved normal completion cannot establish a later effect witness`() { + assertEquals(ProofStatus.UNPROVEN, summary("blockedEffect", "(I)I").effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary("blockedCalleeEffect", "(I)I").effectFree.status) + assertEquals(ProofStatus.REFUTED, summary("guardedEffect", "(I)I").effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary("initializedEffect", "()I").effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary("inheritedInitializationEffect", "()I").effectFree.status) + } + + private fun summary(name: String, descriptor: String) = summaries.getValue("SafetyExample.$name$descriptor") + + private fun assertComplete(summary: MethodSafetySummary) { + assertEquals(ProofStatus.PROVEN, summary.effectFree.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.nonThrowing.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.terminating.status, summary.identity) + assertNotNull(summary.semanticKey, summary.identity) + } + + private fun analyze(source: String): Map { + val file = temporary.resolve("SafetyExample.java") + Files.writeString(file, source) + val classes = Files.createDirectory(temporary.resolve("classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, "-g", "-d", classes.toString(), file.toString())) + return StaticSafetyAnalysis(classes, emptyList()).use { it.summaries() } + } + + private companion object { + val SOURCE = """ + public final class SafetyExample { + static int count; + public static int identity(int x) { return x; } + public static int plusZero(int x) { return x + 0; } + public static int dead(int x) { int unused = x + 1; return x; } + private static int helper(int x) { return x + 0; } + public static int helperCaller(int x) { return helper(x); } + public static int boxed(int x) { return Integer.valueOf(x).intValue(); } + public static boolean sameBoxes(int x) { return Integer.valueOf(x) == Integer.valueOf(x); } + public static boolean sameAlias(int x) { Integer v = Integer.valueOf(x); return v == v; } + public static int blockedEffect(int x) { int d=x-x; int v=10/d; count++; return v; } + private static int uncertainCompletion(int x) { int d=x-x; return 10/d; } + public static int blockedCalleeEffect(int x) { int v=uncertainCompletion(x); count++; return v; } + public static int guardedEffect(int x) { if(x==0)return 0; int v=10/x; count++; return v; } + public static int zero(int x) { return 0; } + public static int guarded(String x) { if (x == null) return 0; return x.length(); } + public static boolean shortCircuit(String x) { return x != null && x.length() > 0; } + public static int guardedJoin(String x, boolean b) { + if (x == null) return 0; String v; if (b) v=x; else v="ok"; return v.length(); + } + public static int unsafeJoin(String x, boolean b) { String v; if (b) v=x; else v="ok"; return v.length(); } + public static int unguarded(String x) { return x.length(); } + public static int literalNull() { String x = null; return x.length(); } + public static boolean equalsNullable(String x) { return "Android".equals(x); } + private static String load(String x) { x.length(); return "Android"; } + public static boolean equalsArgument(String x) { return "Android".equals(load(x)); } + private static String getSafeNamedPlatform() { return null; } + public static int internalNull() { return getSafeNamedPlatform().length(); } + public static int unbox(Integer x) { return x; } + public static int unboxNull() { Integer x = null; return x; } + public static int write(int x) { count=x; return x; } + public static int recursive(int x) { return recursive(x); } + public static int loop(int x) { while (x>0) x--; return x; } + public static Object unknown(java.util.function.Supplier supplier) { return supplier.get(); } + public static int caught(String x) { try { return x.length(); } catch (Exception e) { return 0; } } + static final class Init { + static int n=fail(); private static int fail() { return 1 / 0; } static int value() { return 1; } + static int effect() { count++; return 1; } + } + static class Parent { static int n=fail(); static int fail() { return 1 / 0; } } + static final class Child extends Parent { static { count++; } static int value() { return 1; } } + public static int initialized() { return Init.value(); } + public static int initializedEffect() { return Init.effect(); } + public static int inheritedInitializationEffect() { return Child.value(); } + public static final class Box { String value; public String get() { return value; } } + public static int storedGetter(Box box) { + if(box==null) return 0; String v=box.get(); if(v==null)return 0; return v.length(); + } + public static int repeatedGetter(Box box) { + if(box==null)return 0; if(box.get()==null)return 0; return box.get().length(); + } + public static Object allocation() { return new Object(); } + public static Runnable callback() { return () -> { String x=null; x.length(); }; } + private static int unusedHazard() { String x=null; return x.length(); } + private static int joined(int x) { if(x==0)return x; return x; } + public static int joinedValue(int x) { return joined(x); } + public static int joinedCaller(int x) { return joined(x)+1; } + } + """.trimIndent() + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyCallTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyCallTest.kt new file mode 100644 index 00000000..f56371b7 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyCallTest.kt @@ -0,0 +1,238 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.AcceptanceDecision +import io.johnsonlee.graphfold.engine.WholeChangeSafety +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import javax.tools.ToolProvider +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class StaticSafetyCallTest { + @TempDir lateinit var temporary: Path + private val summaries by lazy { compile() } + + @Test fun `resolved static calls preserve category two argument slots and return provenance`() { + assertEquals(summary("identity", "(JID)I").semanticKey, summary("forward", "(JID)I").semanticKey) + assertComplete(summary("forward", "(JID)I")) + assertTrue(summary("forward", "(JID)I").dependencies.contains("CallCases.identity(JID)I")) + } + + @Test fun `private final and superclass calls are resolved without trusting virtual names`() { + listOf("privateCall", "finalCall", "superCall").forEach { assertComplete(summary(it, "()I")) } + assertComplete(summary("finalOwnerCall", "(LFinalValue;)I")) + val virtual = summary("virtualCall", "()I") + assertEquals(ProofStatus.UNPROVEN, virtual.effectFree.status) + assertNull(virtual.semanticKey) + assertEquals(ProofStatus.UNPROVEN, summary("interfaceCall", "(LCallCases\$Value;)I").nonThrowing.status) + } + + @Test fun `native abstract synchronized and missing dependency methods remain unproven`() { + listOf("nativeCall", "synchronizedCall", "missingCall").forEach { name -> + val result = summary(name, "()I") + assertEquals(ProofStatus.UNPROVEN, result.effectFree.status, name) + assertNull(result.semanticKey, name) + } + val abstractMethod = summaries.getValue("CallCases\$Value.value()I") + assertEquals(ProofStatus.UNPROVEN, abstractMethod.terminating.status) + } + + @Test fun `inspected cross owner initialization retains mutable and volatile field effects`() { + assertComplete(summary("cleanStatic", "()I")) + val mutableRead = summary("mutableRead", "()I") + assertEquals(ProofStatus.PROVEN, mutableRead.nonThrowing.status) + assertEquals(ProofStatus.PROVEN, mutableRead.effectFree.status) + assertNull(mutableRead.semanticKey) + listOf("volatileRead", "staticWrite", "instanceWrite").forEach { name -> + assertEquals(ProofStatus.REFUTED, summary(name, "()I").effectFree.status, name) + } + } + + @Test fun `interface initializer closure and missing field inventories cannot certify safety`() { + val initialized = summary("interfaceInitialization", "()I") + assertEquals(ProofStatus.UNPROVEN, initialized.effectFree.status) + assertNull(initialized.semanticKey) + val field = summary("missingField", "()I") + assertEquals(ProofStatus.UNPROVEN, field.nonThrowing.status) + assertNull(field.semanticKey) + } + + @Test fun `null receiver prevents field read or write while new allocation remains unproven`() { + listOf("nullRead", "nullWrite").forEach { name -> + assertEquals(ProofStatus.REFUTED, summary(name, "()I").nonThrowing.status, name) + } + assertEquals(ProofStatus.UNPROVEN, summary("newInitialized", "()I").effectFree.status) + assertNull(summary("newInitialized", "()I").semanticKey) + } + + @Test fun `deep nonrecursive call chain exceeds the bound without claiming recursion or safety`() { + val source = temporary.resolve("Deep.java") + val methods = (0..33).joinToString("\n") { index -> + val expression = if (index == 33) "7" else "value${index + 1}()" + "static int value$index() { return $expression; }" + } + Files.writeString(source, "class Deep { $methods }") + val classes = Files.createDirectory(temporary.resolve("deep-classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, "-d", classes.toString(), source.toString())) + val results = StaticSafetyAnalysis(classes, emptyList()).use { it.summaries() } + assertEquals(ProofStatus.UNPROVEN, results.getValue("Deep.value0()I").terminating.status) + assertComplete(results.getValue("Deep.value33()I")) + } + + @Test fun `field lookup requires both declared name and JVM descriptor`() { + assertNotNull(summary("mutableRead", "()I")) + StaticSafetyAnalysis(temporary.resolve("classes"), emptyList()).use { analysis -> + assertNotNull(analysis.field("Clean", "field", "I")) + assertNull(analysis.field("Clean", "field", "J")) + assertNull(analysis.field("Clean", "absent", "I")) + } + } + + @Test fun `concrete allocation resolves its virtual target without claiming allocation purity`() { + val result = summary("allocatedVirtual", "()I") + assertEquals(ProofStatus.UNPROVEN, result.effectFree.status) + assertEquals(ProofStatus.PROVEN, result.nonThrowing.status) + assertEquals(ProofStatus.PROVEN, result.terminating.status) + assertTrue("FinalValue.get()I" in result.dependencies) + assertNull(result.semanticKey) + } + + @Test fun `field access includes the failing class initializer before reading its value`() { + val result = summary("failedInitializerRead", "()I") + assertEquals(ProofStatus.REFUTED, result.nonThrowing.status) + assertNull(result.semanticKey) + } + + @Test fun `contradictory comparisons and switch cases cannot introduce a reachable null throw`() { + listOf("contradictoryEqual", "contradictoryUnequal", "contradictorySwitch").forEach { + assertComplete(summary(it, "(I)I")) + } + } + + @Test fun `hazards after unknown completion and computed selector paths remain unproven witnesses`() { + val afterCall = summary("throwAfterUnknownCall", "(Ljava/lang/String;)I") + val computedSwitch = summary("computedSwitch", "(I)I") + listOf(afterCall, computedSwitch).forEach { + assertEquals(ProofStatus.PROVEN, it.effectFree.status) + assertEquals(ProofStatus.UNPROVEN, it.nonThrowing.status) + assertEquals(ProofStatus.PROVEN, it.terminating.status) + assertNull(it.semanticKey) + } + } + + @Test fun `resolved inherited bodies and initializer bodies remain explicit proof dependencies`() { + val cases = mapOf( + "inheritedStatic" to setOf("InheritedChild.value()I", "InheritedBase.value()I"), + "inheritedFinal" to setOf("InheritedChild.finalValue()I", "InheritedBase.finalValue()I"), + "initializedStatic" to setOf("Initialized.()V", "InitializedBase.()V"), + ) + cases.forEach { (name, dependencies) -> + val descriptor = if (name == "inheritedFinal") "(LInheritedChild;)I" else "()I" + val result = summary(name, descriptor) + assertComplete(result) + dependencies.forEach { dependency -> + assertTrue(dependency in result.dependencies, "$name must retain $dependency: ${result.dependencies}") + val proof = WholeChangeSafety( + mapOf(result.identity to result), mapOf(result.identity to result)) + val assessment = proof.assess(listOf(result.identity), emptyList(), setOf(result.identity), setOf(dependency)) + assertEquals(AcceptanceDecision.REJECTED, assessment.decision, dependency) + } + } + } + + private fun summary(name: String, descriptor: String) = summaries.getValue("CallCases.$name$descriptor") + + private fun assertComplete(summary: MethodSafetySummary) { + assertEquals(ProofStatus.PROVEN, summary.effectFree.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.nonThrowing.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.terminating.status, summary.identity) + assertNotNull(summary.semanticKey, summary.identity) + } + + private fun compile(): Map { + val source = temporary.resolve("CallCases.java") + Files.writeString(source, SOURCE) + val classes = Files.createDirectory(temporary.resolve("classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, "-g", "-d", classes.toString(), source.toString())) + Files.delete(classes.resolve("Missing.class")) + return StaticSafetyAnalysis(classes, emptyList()).use { it.summaries() } + } + + private companion object { + val SOURCE = """ + final class FinalValue { int get() { return 7; } } + class Base { int parent() { return 8; } } + public class CallCases extends Base { + int field; + static long unused; + static int identity(long wide, int value, double other) { return value; } + static int forward(long wide, int value, double other) { return identity(wide, value, other); } + private int privateValue() { return 7; } + final int finalValue() { return 7; } + int virtualValue() { return 7; } + int privateCall() { return privateValue(); } + int finalCall() { return finalValue(); } + int virtualCall() { return virtualValue(); } + int superCall() { return super.parent(); } + static int finalOwnerCall(FinalValue value) { return value == null ? 0 : value.get(); } + static int interfaceCall(Value value) { return value == null ? 0 : value.value(); } + interface Value { int value(); } + static native int nativeValue(); + static int nativeCall() { return nativeValue(); } + static synchronized int synchronizedValue() { return 7; } + static int synchronizedCall() { return synchronizedValue(); } + static int missingCall() { return Missing.value(); } + static int missingField() { return Missing.field; } + static int allocatedVirtual() { return new FinalValue().get(); } + static int failedInitializerRead() { return FailedInitializer.field; } + static int contradictoryEqual(int x) { + if (x != 0) { if (x == 0) throw null; return 1; } return 0; + } + static int contradictoryUnequal(int x) { + if (x != 0) { if (x != 0) return 1; throw null; } return 0; + } + static int contradictorySwitch(int x) { + if (x == 1) return 1; + switch (x) { case 1: throw null; case 2: return 2; default: return 3; } + } + static int throwAfterUnknownCall(String value) { int length = value.length(); throw null; } + static int computedSwitch(int x) { switch (x + 1) { case 0: throw null; default: return 2; } } + static int inheritedStatic() { return InheritedChild.value(); } + static int inheritedFinal(InheritedChild receiver) { return receiver == null ? 0 : receiver.finalValue(); } + static int initializedStatic() { return Initialized.value(); } + static int cleanStatic() { return Clean.value(); } + static int mutableRead() { return Clean.field; } + static int volatileRead() { return Clean.volatileField; } + static int staticWrite() { Clean.field = 3; return 7; } + int instanceWrite() { field = 3; return 7; } + static int interfaceInitialization() { return HasInterface.read(); } + static int nullRead() { CallCases value = null; return value.field; } + static int nullWrite() { CallCases value = null; value.field = 3; return 7; } + static int newInitialized() { return new Clean().instance; } + } + class Clean { + int instance; + Clean() { instance = 3; } + static int field; + static volatile int volatileField; + static int value() { return 7; } + } + class HasInterface implements CallCases.Value { + static int read() { return 7; } + public int value() { return 7; } + } + class FailedInitializer { static int field = ((String)null).length(); } + class InheritedBase { static int value() { return 7; } final int finalValue() { return 8; } } + class InheritedChild extends InheritedBase { } + class InitializedBase { static { int unused = 1; } } + class Initialized extends InitializedBase { static { int unused = 2; } static int value() { return 9; } } + class Missing { static int field; static int value() { return 7; } } + """.trimIndent() + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyInstructionTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyInstructionTest.kt new file mode 100644 index 00000000..ee80e1c5 --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyInstructionTest.kt @@ -0,0 +1,260 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Label +import org.objectweb.asm.MethodVisitor +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import java.nio.file.Files +import java.nio.file.Path +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +/** Emitted instructions are analysis inputs, not execution-based safety certificates. */ +class StaticSafetyInstructionTest { + @TempDir lateinit var temporary: Path + + @Test fun `numeric constants and category two local slots retain exact values`() { + val values = listOf(0L, 1L, 9876543210L, 0.0f, 1.0f, 2.0f, -0.0f, 3.5f, 0.0, 1.0, -0.0, 3.5) + values.forEachIndexed { index, value -> + val type = when (value) { is Long -> Type.LONG_TYPE; is Float -> Type.FLOAT_TYPE; else -> Type.DOUBLE_TYPE } + val shortOpcode = when (value) { + 0L -> Opcodes.LCONST_0 + 1L -> Opcodes.LCONST_1 + 0.0f -> Opcodes.FCONST_0 + 1.0f -> Opcodes.FCONST_1 + 2.0f -> Opcodes.FCONST_2 + 0.0 -> Opcodes.DCONST_0 + 1.0 -> Opcodes.DCONST_1 + else -> null + } + val direct = analyze("Numeric$index", "()${type.descriptor}") { + visitLdcInsn(value) + visitInsn(type.getOpcode(Opcodes.IRETURN)) + } + val stored = analyze("Stored$index", "()${type.descriptor}") { + if (shortOpcode == null) visitLdcInsn(value) else visitInsn(shortOpcode) + visitVarInsn(type.getOpcode(Opcodes.ISTORE), 2) + visitVarInsn(type.getOpcode(Opcodes.ILOAD), 2) + visitInsn(type.getOpcode(Opcodes.IRETURN)) + } + assertComplete(direct) + assertComplete(stored) + assertEquals(direct.semanticKey, stored.semanticKey, value.toString()) + } + } + + @Test fun `stack rearrangements and increments preserve the selected integer`() { + val direct = analyze("Direct", "()I") { visitIntInsn(Opcodes.SIPUSH, 321); visitInsn(Opcodes.IRETURN) } + val rearranged = analyze("Rearranged", "()I") { + visitInsn(Opcodes.NOP) + visitIntInsn(Opcodes.BIPUSH, 12) + visitInsn(Opcodes.DUP) + visitInsn(Opcodes.POP) + visitIntInsn(Opcodes.SIPUSH, 320) + visitInsn(Opcodes.SWAP) + visitInsn(Opcodes.POP) + visitVarInsn(Opcodes.ISTORE, 0) + visitIincInsn(0, 1) + visitVarInsn(Opcodes.ILOAD, 0) + visitInsn(Opcodes.IRETURN) + } + assertComplete(rearranged) + assertEquals(direct.semanticKey, rearranged.semanticKey) + } + + @Test fun `switch selectors distinguish cases and retain a total default`() { + listOf(false, true).forEach { table -> + val symbolic = switchSummary("Symbolic$table", table, null) + assertComplete(symbolic) + val first = switchSummary("First$table", table, 1) + val second = switchSummary("Second$table", table, 2) + val otherwise = switchSummary("Default$table", table, 3) + listOf(first, second, otherwise).forEach(::assertComplete) + assertNotEquals(first.semanticKey, second.semanticKey) + assertNotEquals(second.semanticKey, otherwise.semanticKey) + } + } + + @Test fun `casts prove null exact string and Object but reject an unknown narrowing target`() { + val nullCast = analyze("NullCast", "()Ljava/lang/String;") { + visitInsn(Opcodes.ACONST_NULL); visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String"); visitInsn(Opcodes.ARETURN) + } + val exactCast = analyze("ExactCast", "()Ljava/lang/String;") { + visitLdcInsn("known"); visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String"); visitInsn(Opcodes.ARETURN) + } + val objectCast = analyze("ObjectCast", "(Ljava/lang/Object;)Ljava/lang/Object;") { + visitVarInsn(Opcodes.ALOAD, 0); visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Object"); visitInsn(Opcodes.ARETURN) + } + listOf(nullCast, exactCast, objectCast).forEach(::assertComplete) + val narrowing = analyze("Narrowing", "(Ljava/lang/Object;)Ljava/lang/String;") { + visitVarInsn(Opcodes.ALOAD, 0); visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String"); visitInsn(Opcodes.ARETURN) + } + assertEquals(ProofStatus.UNPROVEN, narrowing.nonThrowing.status) + assertNull(narrowing.semanticKey) + } + + @Test fun `instanceof handles null exact and symbolic operands without inventing equality`() { + val nullType = analyze("NullType", "()Z") { + visitInsn(Opcodes.ACONST_NULL); visitTypeInsn(Opcodes.INSTANCEOF, "java/lang/String"); visitInsn(Opcodes.IRETURN) + } + val exactType = analyze("ExactType", "()Z") { + visitLdcInsn("known"); visitTypeInsn(Opcodes.INSTANCEOF, "java/lang/String"); visitInsn(Opcodes.IRETURN) + } + val unknownType = analyze("UnknownType", "(Ljava/lang/Object;)Z") { + visitVarInsn(Opcodes.ALOAD, 0); visitTypeInsn(Opcodes.INSTANCEOF, "java/lang/String"); visitInsn(Opcodes.IRETURN) + } + listOf(nullType, exactType, unknownType).forEach(::assertComplete) + assertNotEquals(nullType.semanticKey, exactType.semanticKey) + assertNotEquals(exactType.semanticKey, unknownType.semanticKey) + } + + @Test fun `array length is safe after a guard but null array and unsupported array operations have no key`() { + val guarded = analyze("GuardedArray", "([I)I") { + val present = Label() + visitVarInsn(Opcodes.ALOAD, 0); visitJumpInsn(Opcodes.IFNONNULL, present) + visitInsn(Opcodes.ICONST_0); visitInsn(Opcodes.IRETURN) + visitLabel(present); visitVarInsn(Opcodes.ALOAD, 0); visitInsn(Opcodes.ARRAYLENGTH); visitInsn(Opcodes.IRETURN) + } + assertComplete(guarded) + val nullArray = analyze("NullArray", "()I") { + visitInsn(Opcodes.ACONST_NULL); visitInsn(Opcodes.ARRAYLENGTH); visitInsn(Opcodes.IRETURN) + } + assertEquals(ProofStatus.REFUTED, nullArray.nonThrowing.status) + val allocation = analyze("ArrayAllocation", "()[I") { + visitInsn(Opcodes.ICONST_1); visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT); visitInsn(Opcodes.ARETURN) + } + val read = analyze("ArrayRead", "([I)I") { + visitVarInsn(Opcodes.ALOAD, 0); visitInsn(Opcodes.ICONST_0); visitInsn(Opcodes.IALOAD); visitInsn(Opcodes.IRETURN) + } + val referenceArray = analyze("ReferenceArray", "()[Ljava/lang/String;") { + visitInsn(Opcodes.ICONST_1); visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String"); visitInsn(Opcodes.ARETURN) + } + val dimensions = analyze("Dimensions", "()[[I") { + visitInsn(Opcodes.ICONST_1); visitInsn(Opcodes.ICONST_1) + visitMultiANewArrayInsn("[[I", 2); visitInsn(Opcodes.ARETURN) + } + listOf(allocation, read, referenceArray, dimensions).forEach { + assertEquals(ProofStatus.UNPROVEN, it.nonThrowing.status) + assertNull(it.semanticKey) + } + } + + @Test fun `class literals and unsupported numeric operations fail closed`() { + val literal = analyze("ClassLiteral", "()Ljava/lang/Class;") { + visitLdcInsn(Type.getObjectType("java/lang/String")); visitInsn(Opcodes.ARETURN) + } + val wideAdd = analyze("WideAdd", "(J)J") { + visitVarInsn(Opcodes.LLOAD, 0); visitInsn(Opcodes.LCONST_1); visitInsn(Opcodes.LADD); visitInsn(Opcodes.LRETURN) + } + listOf(literal, wideAdd).forEach { + assertEquals(ProofStatus.UNPROVEN, it.effectFree.status) + assertNull(it.semanticKey) + } + } + + @Test fun `exhausted analysis budget does not manufacture a termination certificate`() { + val result = analyze("LargeStraightLine", "()I") { + repeat(10001) { visitInsn(Opcodes.NOP) } + visitInsn(Opcodes.ICONST_0); visitInsn(Opcodes.IRETURN) + } + assertEquals(ProofStatus.UNPROVEN, result.terminating.status) + assertNull(result.semanticKey) + } + + @Test fun `integer negation and remainder retain overflow and division hazards`() { + val minimum = analyze("MinimumNegation", "()I") { + visitLdcInsn(Int.MIN_VALUE); visitInsn(Opcodes.INEG); visitInsn(Opcodes.IRETURN) + } + val unchanged = analyze("MinimumConstant", "()I") { visitLdcInsn(Int.MIN_VALUE); visitInsn(Opcodes.IRETURN) } + assertComplete(minimum) + assertEquals(unchanged.semanticKey, minimum.semanticKey) + val symbolic = analyze("SymbolicNegation", "(I)I") { + visitVarInsn(Opcodes.ILOAD, 0); visitInsn(Opcodes.INEG); visitInsn(Opcodes.IRETURN) + } + assertComplete(symbolic) + listOf(Opcodes.IDIV, Opcodes.IREM).forEach { opcode -> + val zero = analyze("ZeroDivisor$opcode", "()I") { + visitInsn(Opcodes.ICONST_1); visitInsn(Opcodes.ICONST_0); visitInsn(opcode); visitInsn(Opcodes.IRETURN) + } + assertEquals(ProofStatus.REFUTED, zero.nonThrowing.status) + assertNull(zero.semanticKey) + val nonzero = analyze("NonzeroDivisor$opcode", "(I)I") { + visitVarInsn(Opcodes.ILOAD, 0); visitInsn(Opcodes.ICONST_2); visitInsn(opcode); visitInsn(Opcodes.IRETURN) + } + assertComplete(nonzero) + } + } + + @Test fun `each compact integer constant equals its full constant encoding`() { + (-1..5).forEach { value -> + val compact = analyze("Compact${value + 1}", "()I") { + visitInsn(Opcodes.ICONST_0 + value); visitInsn(Opcodes.IRETURN) + } + val full = analyze("Full${value + 1}", "()I") { visitLdcInsn(value); visitInsn(Opcodes.IRETURN) } + assertComplete(compact) + assertEquals(full.semanticKey, compact.semanticKey) + } + } + + @Test fun `known conditional edges preserve a precise return while retaining the unused branch`() { + listOf(0, 1).forEach { selector -> + val result = analyze("KnownBranch$selector", "()I") { + val target = Label() + visitLdcInsn(selector); visitJumpInsn(Opcodes.IFEQ, target) + visitInsn(Opcodes.ICONST_1); visitInsn(Opcodes.IRETURN) + visitLabel(target); visitInsn(Opcodes.ICONST_2); visitInsn(Opcodes.IRETURN) + } + assertComplete(result) + val key = assertNotNull(result.semanticKey) + val expected = if (selector == 0) 2 else 1 + assertEquals("return:I:int:$expected", key.substringBefore('|')) + assertNotEquals("[]", key.substringAfter("|residuals:")) + } + } + + @Test fun `explicit throw has no normal return or totality key`() { + val result = analyze("ExplicitThrow", "()I") { visitInsn(Opcodes.ACONST_NULL); visitInsn(Opcodes.ATHROW) } + assertEquals(ProofStatus.REFUTED, result.nonThrowing.status) + assertNull(result.semanticKey) + } + + private fun switchSummary(name: String, table: Boolean, selector: Int?) = analyze(name, "(I)I") { + val first = Label() + val second = Label() + val otherwise = Label() + if (selector == null) visitVarInsn(Opcodes.ILOAD, 0) else visitLdcInsn(selector) + if (table) visitTableSwitchInsn(1, 2, otherwise, first, second) + else visitLookupSwitchInsn(otherwise, intArrayOf(1, 2), arrayOf(first, second)) + visitLabel(first); visitIntInsn(Opcodes.BIPUSH, 10); visitInsn(Opcodes.IRETURN) + visitLabel(second); visitIntInsn(Opcodes.BIPUSH, 20); visitInsn(Opcodes.IRETURN) + visitLabel(otherwise); visitIntInsn(Opcodes.BIPUSH, 30); visitInsn(Opcodes.IRETURN) + } + + private fun analyze(name: String, descriptor: String, body: MethodVisitor.() -> Unit): MethodSafetySummary { + val classes = Files.createDirectory(temporary.resolve(name)) + val writer = ClassWriter(ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES) + writer.visit(Opcodes.V17, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", null) + val method = writer.visitMethod(Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC, "value", descriptor, null, null) + method.visitCode() + method.body() + method.visitMaxs(0, 0) + method.visitEnd() + writer.visitEnd() + Files.write(classes.resolve("$name.class"), writer.toByteArray()) + return StaticSafetyAnalysis(classes, emptyList()).use { it.summaries().getValue("$name.value$descriptor") } + } + + private fun assertComplete(summary: MethodSafetySummary) { + assertEquals(ProofStatus.PROVEN, summary.effectFree.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.nonThrowing.status, summary.identity) + assertEquals(ProofStatus.PROVEN, summary.terminating.status, summary.identity) + assertNotNull(summary.semanticKey, summary.identity) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyMalformedInputTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyMalformedInputTest.kt new file mode 100644 index 00000000..cdfdf53b --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyMalformedInputTest.kt @@ -0,0 +1,97 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import io.johnsonlee.graphfold.engine.safety.ProofStatus +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.objectweb.asm.Label +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.MethodVisitor +import org.objectweb.asm.Opcodes +import java.nio.file.Files +import java.nio.file.Path +import java.util.jar.JarEntry +import java.util.jar.JarOutputStream +import kotlin.test.assertEquals +import kotlin.test.assertNull + +/** Archives are decoded without executing a JVM verifier, so malformed bodies must never receive safety keys. */ +class StaticSafetyMalformedInputTest { + @TempDir lateinit var temporary: Path + + @Test fun `undefined local read and increment in packaged bytes fail closed`() { + val read = analyze("UndefinedRead") { visitVarInsn(Opcodes.ILOAD, 0); visitInsn(Opcodes.IRETURN) } + val increment = analyze("UndefinedIncrement") { + visitIincInsn(0, 1); visitVarInsn(Opcodes.ILOAD, 0); visitInsn(Opcodes.IRETURN) + } + listOf(read, increment).forEach(::assertUnproven) + } + + @Test fun `falling out of a body and unsupported return-address local cannot acquire a key`() { + val falloff = analyze("Falloff") { visitInsn(Opcodes.NOP) } + val legacyReturn = analyze("LegacyReturn") { visitVarInsn(Opcodes.RET, 0) } + listOf(falloff, legacyReturn).forEach(::assertUnproven) + } + + @Test fun `missing cyclic and absent superclass inventories cannot certify static calls`() { + listOf("Missing", "Target", null).forEachIndexed { index, superclass -> + val caller = classBytes("Caller", "java/lang/Object") { + visitMethodInsn(Opcodes.INVOKESTATIC, "Target", "value", "()I", false) + visitInsn(Opcodes.IRETURN) + } + val target = classBytes("Target", superclass) { visitInsn(Opcodes.ICONST_1); visitInsn(Opcodes.IRETURN) } + val artifact = archive("Hierarchy$index", mapOf("Caller" to caller, "Target" to target)) + val result = StaticSafetyAnalysis(artifact, emptyList()).use { it.summaries().getValue("Caller.value()I") } + assertUnproven(result) + } + } + + @Test fun `valid legacy subroutine remains explicitly unsupported without consuming a nonexistent operand`() { + val result = analyze("LegacySubroutine") { + val subroutine = Label() + visitJumpInsn(Opcodes.JSR, subroutine) + visitInsn(Opcodes.ICONST_1) + visitInsn(Opcodes.IRETURN) + visitLabel(subroutine) + visitVarInsn(Opcodes.ASTORE, 0) + visitVarInsn(Opcodes.RET, 0) + } + assertUnproven(result) + } + + private fun analyze(name: String, body: MethodVisitor.() -> Unit): MethodSafetySummary { + val artifact = archive(name, mapOf(name to classBytes(name, "java/lang/Object", body))) + return StaticSafetyAnalysis(artifact, emptyList()).use { it.summaries().getValue("$name.value()I") } + } + + private fun classBytes(name: String, superclass: String?, body: MethodVisitor.() -> Unit): ByteArray { + val writer = ClassWriter(0) + writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, superclass, null) + val method = writer.visitMethod(Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC, "value", "()I", null, null) + method.visitCode() + method.body() + method.visitMaxs(2, 1) + method.visitEnd() + writer.visitEnd() + return writer.toByteArray() + } + + private fun archive(name: String, classes: Map): Path { + val artifact = temporary.resolve("$name.jar") + JarOutputStream(Files.newOutputStream(artifact)).use { jar -> + classes.forEach { (owner, bytes) -> + jar.putNextEntry(JarEntry("$owner.class")) + jar.write(bytes) + jar.closeEntry() + } + } + return artifact + } + + private fun assertUnproven(summary: MethodSafetySummary) { + assertEquals(ProofStatus.UNPROVEN, summary.effectFree.status) + assertEquals(ProofStatus.UNPROVEN, summary.nonThrowing.status) + assertEquals(ProofStatus.UNPROVEN, summary.terminating.status) + assertNull(summary.semanticKey) + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyResidualTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyResidualTest.kt new file mode 100644 index 00000000..503610df --- /dev/null +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StaticSafetyResidualTest.kt @@ -0,0 +1,67 @@ +package io.johnsonlee.graphfold.engine.bytecode.asm + +import io.johnsonlee.graphfold.engine.safety.MethodSafetySummary +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import javax.tools.ToolProvider +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class StaticSafetyResidualTest { + @TempDir lateinit var temporary: Path + private var sequence = 0 + + @Test fun `constant callee does not erase unused production return changes`() { + val base = analyze("if (always()) return x; return 10;") + val head = analyze("if (always()) return x; return 20;") + assertNotNull(base.semanticKey) + assertNotNull(head.semanticKey) + assertNotEquals(base.semanticKey, head.semanticKey) + assertTrue(head.nonThrowing.evidence.any { it.contains("Unselected instructions") }) + } + + @Test fun `unused effects and throws remain in the whole method certificate`() { + val base = analyze("if (always()) return x; return 10;") + val effect = analyze("if (always()) return x; count++; return 10;") + val hazard = analyze("if (always()) return x; throw new IllegalStateException();") + assertNotEquals(base.semanticKey, effect.semanticKey) + assertNotEquals(base.semanticKey, hazard.semanticKey) + assertTrue(effect.effectFree.evidence.any { it.contains("retained exactly") }) + assertTrue(hazard.nonThrowing.evidence.any { it.contains("retained exactly") }) + } + + @Test fun `unchanged unused instructions do not prevent equivalent analyzed computation proof`() { + val base = analyze("if (always()) return x; return 10;") + val head = analyze("if (always()) return x + 0; return 10;") + assertNotNull(base.semanticKey) + assertEquals(base.semanticKey, head.semanticKey) + } + + @Test fun `callee residual certificate reaches its callers`() { + val base = analyze("return helper(x);", "if (always()) return x; return 10;") + val head = analyze("return helper(x);", "if (always()) return x; return 20;") + assertNotEquals(base.semanticKey, head.semanticKey) + assertTrue(head.dependencies.any { it.contains("helper") }) + assertTrue(head.nonThrowing.evidence.any { it.contains("ResidualExample.helper") }) + } + + private fun analyze(body: String, helper: String = "return x;"): MethodSafetySummary { + val directory = Files.createDirectory(temporary.resolve("case-${sequence++}")) + val file = directory.resolve("ResidualExample.java") + Files.writeString(file, """ + public final class ResidualExample { + static int count; + private static boolean always() { return true; } + private static int helper(int x) { $helper } + public static int value(int x) { $body } + } + """.trimIndent()) + val classes = Files.createDirectory(directory.resolve("classes")) + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, "-g", "-d", classes.toString(), file.toString())) + return StaticSafetyAnalysis(classes, emptyList()).use { it.summaries().getValue("ResidualExample.value(I)I") } + } +} diff --git a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StructuralClassInventoryTest.kt b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StructuralClassInventoryTest.kt index 4c81f9e6..9b29220f 100644 --- a/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StructuralClassInventoryTest.kt +++ b/engine/src/test/kotlin/io/johnsonlee/graphfold/engine/bytecode/asm/StructuralClassInventoryTest.kt @@ -17,6 +17,30 @@ class StructuralClassInventoryTest { @TempDir lateinit var temporary: Path private var sequence = 0 + @Test + fun `inner class table order is irrelevant but membership and flags remain evidence`() { + fun ordered(names: List, access: Int = Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC): BytecodeInventory { + val directory = Files.createDirectory(temporary.resolve("ordered-${sequence++}")) + val writer = ClassWriter(0) + writer.visit(Opcodes.V17, Opcodes.ACC_PUBLIC, "sample/Example", null, "java/lang/Object", null) + names.forEach { writer.visitInnerClass("sample/Example\$$it", "sample/Example", it, access) } + writer.visitEnd() + Files.write(directory.resolve("Example.class"), writer.toByteArray()) + return BytecodeInventory().also { it.read(directory) } + } + val before = ordered(listOf("Context", "Device")) + val reordered = ordered(listOf("Device", "Context")) + assertEquals(before.declarations, reordered.declarations) + assertEquals(before.classShapes, reordered.classShapes) + assertEquals(before.structuralClassShapes, reordered.structuralClassShapes) + listOf(ordered(listOf("Context")), ordered(listOf("Context", "Device"), Opcodes.ACC_PRIVATE or Opcodes.ACC_STATIC)) + .forEach { changed -> + assertNotEquals(before.declarations, changed.declarations) + assertNotEquals(before.classShapes, changed.classShapes) + assertNotEquals(before.structuralClassShapes, changed.structuralClassShapes) + } + } + @Test fun `Kotlin metadata nesting remains evidence when JVM inner class rows are identical`() { val before = inventory(listOf("Nested")) diff --git a/engine/src/test/resources/safety/cases/java/callee-NPE/base.java b/engine/src/test/resources/safety/cases/java/callee-NPE/base.java new file mode 100644 index 00000000..ef553f6e --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/callee-NPE/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } +private static String platform(String text) { String broken = null; broken.length(); return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/callee-NPE/head.java b/engine/src/test/resources/safety/cases/java/callee-NPE/head.java new file mode 100644 index 00000000..4904d9d2 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/callee-NPE/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +boolean ignored = "Android".equals(platform(text)); +return 1; + } +private static String platform(String text) { String broken = null; broken.length(); return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/callee-safe/base.java b/engine/src/test/resources/safety/cases/java/callee-safe/base.java new file mode 100644 index 00000000..a9b194e4 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/callee-safe/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } +private static String platform(String text) { return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/callee-safe/head.java b/engine/src/test/resources/safety/cases/java/callee-safe/head.java new file mode 100644 index 00000000..41d2ccad --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/callee-safe/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +boolean ignored = "Android".equals(platform(text)); +return 1; + } +private static String platform(String text) { return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/context-only/base.java b/engine/src/test/resources/safety/cases/java/context-only/base.java new file mode 100644 index 00000000..d06d4bd0 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/context-only/base.java @@ -0,0 +1,15 @@ +public class Example { + public static int value(Context ctx) { +return 1; + } +public static final class Context { + private final Device device; + public Context(Device device) { this.device = device; } + public Device getDevice() { return device; } +} +public static final class Device { + private final String platform; + public Device(String platform) { this.platform = platform; } + public String getPlatform() { return platform; } +} +} diff --git a/engine/src/test/resources/safety/cases/java/context-only/head.java b/engine/src/test/resources/safety/cases/java/context-only/head.java new file mode 100644 index 00000000..ba592039 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/context-only/head.java @@ -0,0 +1,16 @@ +public class Example { + public static int value(Context ctx) { +if (ctx != null) { boolean ignored = "Android".equals(ctx.getDevice().getPlatform()); } +return 1; + } +public static final class Context { + private final Device device; + public Context(Device device) { this.device = device; } + public Device getDevice() { return device; } +} +public static final class Device { + private final String platform; + public Device(String platform) { this.platform = platform; } + public String getPlatform() { return platform; } +} +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-chain/base.java b/engine/src/test/resources/safety/cases/java/guarded-chain/base.java new file mode 100644 index 00000000..d06d4bd0 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-chain/base.java @@ -0,0 +1,15 @@ +public class Example { + public static int value(Context ctx) { +return 1; + } +public static final class Context { + private final Device device; + public Context(Device device) { this.device = device; } + public Device getDevice() { return device; } +} +public static final class Device { + private final String platform; + public Device(String platform) { this.platform = platform; } + public String getPlatform() { return platform; } +} +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-chain/head.java b/engine/src/test/resources/safety/cases/java/guarded-chain/head.java new file mode 100644 index 00000000..c4a5f9da --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-chain/head.java @@ -0,0 +1,16 @@ +public class Example { + public static int value(Context ctx) { +if (ctx != null) { Device device = ctx.getDevice(); if (device != null) { boolean ignored = "Android".equals(device.getPlatform()); } } +return 1; + } +public static final class Context { + private final Device device; + public Context(Device device) { this.device = device; } + public Device getDevice() { return device; } +} +public static final class Device { + private final String platform; + public Device(String platform) { this.platform = platform; } + public String getPlatform() { return platform; } +} +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-join/base.java b/engine/src/test/resources/safety/cases/java/guarded-join/base.java new file mode 100644 index 00000000..26c11e60 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-join/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text, boolean choose) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-join/head.java b/engine/src/test/resources/safety/cases/java/guarded-join/head.java new file mode 100644 index 00000000..3f499b6b --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-join/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text, boolean choose) { +String selected = choose ? text : "ok"; if (selected != null) { int ignored = selected.length(); } +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-string/base.java b/engine/src/test/resources/safety/cases/java/guarded-string/base.java new file mode 100644 index 00000000..58464cef --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-string/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-string/head.java b/engine/src/test/resources/safety/cases/java/guarded-string/head.java new file mode 100644 index 00000000..3220e050 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-string/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +if (text != null) { int ignored = text.length(); } +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-unbox/base.java b/engine/src/test/resources/safety/cases/java/guarded-unbox/base.java new file mode 100644 index 00000000..be3db4fa --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-unbox/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(Integer boxed) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/guarded-unbox/head.java b/engine/src/test/resources/safety/cases/java/guarded-unbox/head.java new file mode 100644 index 00000000..67f97be2 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/guarded-unbox/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(Integer boxed) { +if (boxed != null) { int ignored = boxed + 1; } +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/instance-equals-nullable/base.java b/engine/src/test/resources/safety/cases/java/instance-equals-nullable/base.java new file mode 100644 index 00000000..58464cef --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/instance-equals-nullable/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/instance-equals-nullable/head.java b/engine/src/test/resources/safety/cases/java/instance-equals-nullable/head.java new file mode 100644 index 00000000..79b5aada --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/instance-equals-nullable/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +boolean ignored = text.equals("Android"); +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/literal-equals-nullable/base.java b/engine/src/test/resources/safety/cases/java/literal-equals-nullable/base.java new file mode 100644 index 00000000..58464cef --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/literal-equals-nullable/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/literal-equals-nullable/head.java b/engine/src/test/resources/safety/cases/java/literal-equals-nullable/head.java new file mode 100644 index 00000000..79bb6971 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/literal-equals-nullable/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +boolean ignored = "Android".equals(text); +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/opaque-alternative/base.java b/engine/src/test/resources/safety/cases/java/opaque-alternative/base.java new file mode 100644 index 00000000..1b3c8b46 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/opaque-alternative/base.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text, boolean choose) { +return 1; + } +private static String first(String text) { return text; } +private static native String second(String text); +} diff --git a/engine/src/test/resources/safety/cases/java/opaque-alternative/head.java b/engine/src/test/resources/safety/cases/java/opaque-alternative/head.java new file mode 100644 index 00000000..a111e9f6 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/opaque-alternative/head.java @@ -0,0 +1,8 @@ +public class Example { + public static int value(String text, boolean choose) { +if (choose) { first(text); } else { second(text); } +return 1; + } +private static String first(String text) { return text; } +private static native String second(String text); +} diff --git a/engine/src/test/resources/safety/cases/java/resolved-alternatives/base.java b/engine/src/test/resources/safety/cases/java/resolved-alternatives/base.java new file mode 100644 index 00000000..df6f80a5 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/resolved-alternatives/base.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text, boolean choose) { +return 1; + } +private static String first(String text) { return text; } +private static String second(String text) { return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/resolved-alternatives/head.java b/engine/src/test/resources/safety/cases/java/resolved-alternatives/head.java new file mode 100644 index 00000000..d863c662 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/resolved-alternatives/head.java @@ -0,0 +1,8 @@ +public class Example { + public static int value(String text, boolean choose) { +if (choose) { first(text); } else { second(text); } +return 1; + } +private static String first(String text) { return text; } +private static String second(String text) { return text; } +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-join/base.java b/engine/src/test/resources/safety/cases/java/unguarded-join/base.java new file mode 100644 index 00000000..26c11e60 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-join/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text, boolean choose) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-join/head.java b/engine/src/test/resources/safety/cases/java/unguarded-join/head.java new file mode 100644 index 00000000..46b66a34 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-join/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text, boolean choose) { +String selected = choose ? text : "ok"; int ignored = selected.length(); +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-string/base.java b/engine/src/test/resources/safety/cases/java/unguarded-string/base.java new file mode 100644 index 00000000..58464cef --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-string/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(String text) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-string/head.java b/engine/src/test/resources/safety/cases/java/unguarded-string/head.java new file mode 100644 index 00000000..3f6126c8 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-string/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(String text) { +int ignored = text.length(); +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-unbox/base.java b/engine/src/test/resources/safety/cases/java/unguarded-unbox/base.java new file mode 100644 index 00000000..be3db4fa --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-unbox/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(Integer boxed) { +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unguarded-unbox/head.java b/engine/src/test/resources/safety/cases/java/unguarded-unbox/head.java new file mode 100644 index 00000000..b93ffda4 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unguarded-unbox/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(Integer boxed) { +int ignored = boxed + 1; +return 1; + } + +} diff --git a/engine/src/test/resources/safety/cases/java/unresolved-dispatch/base.java b/engine/src/test/resources/safety/cases/java/unresolved-dispatch/base.java new file mode 100644 index 00000000..0eae758f --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unresolved-dispatch/base.java @@ -0,0 +1,6 @@ +public class Example { + public static int value(Reader reader, String text) { +return 1; + } +public interface Reader { String read(String text); } +} diff --git a/engine/src/test/resources/safety/cases/java/unresolved-dispatch/head.java b/engine/src/test/resources/safety/cases/java/unresolved-dispatch/head.java new file mode 100644 index 00000000..4d16ed25 --- /dev/null +++ b/engine/src/test/resources/safety/cases/java/unresolved-dispatch/head.java @@ -0,0 +1,7 @@ +public class Example { + public static int value(Reader reader, String text) { +if (reader != null) { reader.read(text); } +return 1; + } +public interface Reader { String read(String text); } +} diff --git a/engine/src/test/resources/safety/cases/kotlin/callee-NPE/base.kt b/engine/src/test/resources/safety/cases/kotlin/callee-NPE/base.kt new file mode 100644 index 00000000..10707a2b --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/callee-NPE/base.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +return 1 +} +private fun platform(text: String?): String? { val broken: String? = null; broken!!.length; return text } diff --git a/engine/src/test/resources/safety/cases/kotlin/callee-NPE/head.kt b/engine/src/test/resources/safety/cases/kotlin/callee-NPE/head.kt new file mode 100644 index 00000000..640b8905 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/callee-NPE/head.kt @@ -0,0 +1,5 @@ +fun value(text: String?): Int { +val ignored = "Android".equals(platform(text)) +return 1 +} +private fun platform(text: String?): String? { val broken: String? = null; broken!!.length; return text } diff --git a/engine/src/test/resources/safety/cases/kotlin/callee-safe/base.kt b/engine/src/test/resources/safety/cases/kotlin/callee-safe/base.kt new file mode 100644 index 00000000..e6c7cf95 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/callee-safe/base.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +return 1 +} +private fun platform(text: String?): String? = text diff --git a/engine/src/test/resources/safety/cases/kotlin/callee-safe/head.kt b/engine/src/test/resources/safety/cases/kotlin/callee-safe/head.kt new file mode 100644 index 00000000..83ec0c79 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/callee-safe/head.kt @@ -0,0 +1,5 @@ +fun value(text: String?): Int { +val ignored = "Android".equals(platform(text)) +return 1 +} +private fun platform(text: String?): String? = text diff --git a/engine/src/test/resources/safety/cases/kotlin/context-only/base.kt b/engine/src/test/resources/safety/cases/kotlin/context-only/base.kt new file mode 100644 index 00000000..2805d104 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/context-only/base.kt @@ -0,0 +1,5 @@ +fun value(ctx: Context?): Int { +return 1 +} +class Context(private val device: Device?) { fun getDevice(): Device? = device } +class Device(private val platform: String?) { fun getPlatform(): String? = platform } diff --git a/engine/src/test/resources/safety/cases/kotlin/context-only/head.kt b/engine/src/test/resources/safety/cases/kotlin/context-only/head.kt new file mode 100644 index 00000000..962b194c --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/context-only/head.kt @@ -0,0 +1,6 @@ +fun value(ctx: Context?): Int { +if (ctx != null) { val ignored = "Android".equals(ctx.getDevice()!!.getPlatform()) } +return 1 +} +class Context(private val device: Device?) { fun getDevice(): Device? = device } +class Device(private val platform: String?) { fun getPlatform(): String? = platform } diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-chain/base.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-chain/base.kt new file mode 100644 index 00000000..2805d104 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-chain/base.kt @@ -0,0 +1,5 @@ +fun value(ctx: Context?): Int { +return 1 +} +class Context(private val device: Device?) { fun getDevice(): Device? = device } +class Device(private val platform: String?) { fun getPlatform(): String? = platform } diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-chain/head.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-chain/head.kt new file mode 100644 index 00000000..96f613bb --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-chain/head.kt @@ -0,0 +1,6 @@ +fun value(ctx: Context?): Int { +if (ctx != null) { val device = ctx.getDevice(); if (device != null) { val ignored = "Android".equals(device.getPlatform()) } } +return 1 +} +class Context(private val device: Device?) { fun getDevice(): Device? = device } +class Device(private val platform: String?) { fun getPlatform(): String? = platform } diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-join/base.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-join/base.kt new file mode 100644 index 00000000..1785126e --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-join/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?, choose: Boolean): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-join/head.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-join/head.kt new file mode 100644 index 00000000..70eaad50 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-join/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?, choose: Boolean): Int { +val selected = if (choose) text else "ok"; if (selected != null) { val ignored = selected.length } +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-string/base.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-string/base.kt new file mode 100644 index 00000000..822f9d9d --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-string/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-string/head.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-string/head.kt new file mode 100644 index 00000000..446495fa --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-string/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +if (text != null) { val ignored = text.length } +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/base.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/base.kt new file mode 100644 index 00000000..a6263335 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/base.kt @@ -0,0 +1,3 @@ +fun value(boxed: Int?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/head.kt b/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/head.kt new file mode 100644 index 00000000..a4e6f802 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/guarded-unbox/head.kt @@ -0,0 +1,4 @@ +fun value(boxed: Int?): Int { +if (boxed != null) { val ignored: Int = boxed + 1 } +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/base.kt b/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/base.kt new file mode 100644 index 00000000..822f9d9d --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/head.kt b/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/head.kt new file mode 100644 index 00000000..5bb24e96 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/instance-equals-nullable/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +val ignored = text!!.equals("Android") +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/base.kt b/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/base.kt new file mode 100644 index 00000000..822f9d9d --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/head.kt b/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/head.kt new file mode 100644 index 00000000..9a58d300 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/literal-equals-nullable/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +val ignored = "Android".equals(text) +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/base.kt b/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/base.kt new file mode 100644 index 00000000..fe033bb0 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/base.kt @@ -0,0 +1,5 @@ +fun value(text: String?, choose: Boolean): Int { +return 1 +} +private fun first(text: String?): String? = text +private external fun second(text: String?): String? diff --git a/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/head.kt b/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/head.kt new file mode 100644 index 00000000..e2c9cbd8 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/opaque-alternative/head.kt @@ -0,0 +1,6 @@ +fun value(text: String?, choose: Boolean): Int { +if (choose) { first(text) } else { second(text) } +return 1 +} +private fun first(text: String?): String? = text +private external fun second(text: String?): String? diff --git a/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/base.kt b/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/base.kt new file mode 100644 index 00000000..2cf2ab32 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/base.kt @@ -0,0 +1,5 @@ +fun value(text: String?, choose: Boolean): Int { +return 1 +} +private fun first(text: String?): String? = text +private fun second(text: String?): String? = text diff --git a/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/head.kt b/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/head.kt new file mode 100644 index 00000000..23d6b594 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/resolved-alternatives/head.kt @@ -0,0 +1,6 @@ +fun value(text: String?, choose: Boolean): Int { +if (choose) { first(text) } else { second(text) } +return 1 +} +private fun first(text: String?): String? = text +private fun second(text: String?): String? = text diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-join/base.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-join/base.kt new file mode 100644 index 00000000..1785126e --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-join/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?, choose: Boolean): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-join/head.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-join/head.kt new file mode 100644 index 00000000..c5f50ca6 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-join/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?, choose: Boolean): Int { +val selected = if (choose) text else "ok"; val ignored = selected!!.length +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-string/base.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-string/base.kt new file mode 100644 index 00000000..822f9d9d --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-string/base.kt @@ -0,0 +1,3 @@ +fun value(text: String?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-string/head.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-string/head.kt new file mode 100644 index 00000000..fa722c31 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-string/head.kt @@ -0,0 +1,4 @@ +fun value(text: String?): Int { +val ignored = text!!.length +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/base.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/base.kt new file mode 100644 index 00000000..a6263335 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/base.kt @@ -0,0 +1,3 @@ +fun value(boxed: Int?): Int { +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/head.kt b/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/head.kt new file mode 100644 index 00000000..3bd1338a --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unguarded-unbox/head.kt @@ -0,0 +1,4 @@ +fun value(boxed: Int?): Int { +val ignored: Int = boxed!! + 1 +return 1 +} diff --git a/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/base.kt b/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/base.kt new file mode 100644 index 00000000..8c7cf4e0 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/base.kt @@ -0,0 +1,4 @@ +fun value(reader: Reader?, text: String?): Int { +return 1 +} +interface Reader { fun read(text: String?): String? } diff --git a/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/head.kt b/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/head.kt new file mode 100644 index 00000000..0db5fe37 --- /dev/null +++ b/engine/src/test/resources/safety/cases/kotlin/unresolved-dispatch/head.kt @@ -0,0 +1,5 @@ +fun value(reader: Reader?, text: String?): Int { +if (reader != null) { reader.read(text) } +return 1 +} +interface Reader { fun read(text: String?): String? } diff --git a/engine/src/test/resources/safety/manifest.json b/engine/src/test/resources/safety/manifest.json new file mode 100644 index 00000000..834a4d37 --- /dev/null +++ b/engine/src/test/resources/safety/manifest.json @@ -0,0 +1,2386 @@ +[ + { + "id": "java/boot/safe", + "language": "java", + "inputVariant": "boot", + "change": "safe", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/return-change", + "language": "java", + "inputVariant": "boot", + "change": "return-change", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/null-dereference", + "language": "java", + "inputVariant": "boot", + "change": "null-dereference", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/safe", + "language": "java", + "inputVariant": "war", + "change": "safe", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/return-change", + "language": "java", + "inputVariant": "war", + "change": "return-change", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/null-dereference", + "language": "java", + "inputVariant": "war", + "change": "null-dereference", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/safe", + "language": "kotlin", + "inputVariant": "boot", + "change": "safe", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/return-change", + "language": "kotlin", + "inputVariant": "boot", + "change": "return-change", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/null-dereference", + "language": "kotlin", + "inputVariant": "boot", + "change": "null-dereference", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/safe", + "language": "kotlin", + "inputVariant": "war", + "change": "safe", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/return-change", + "language": "kotlin", + "inputVariant": "war", + "change": "return-change", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/null-dereference", + "language": "kotlin", + "inputVariant": "war", + "change": "null-dereference", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/helper-pure", + "language": "java", + "inputVariant": "boot", + "change": "helper-pure", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/helper-effect", + "language": "java", + "inputVariant": "boot", + "change": "helper-effect", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "REFUTED", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/helper-opaque", + "language": "java", + "inputVariant": "boot", + "change": "helper-opaque", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "java/war/helper-pure", + "language": "java", + "inputVariant": "war", + "change": "helper-pure", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/helper-effect", + "language": "java", + "inputVariant": "war", + "change": "helper-effect", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "REFUTED", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/helper-opaque", + "language": "java", + "inputVariant": "war", + "change": "helper-opaque", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "kotlin/boot/helper-pure", + "language": "kotlin", + "inputVariant": "boot", + "change": "helper-pure", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/helper-effect", + "language": "kotlin", + "inputVariant": "boot", + "change": "helper-effect", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "REFUTED", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/helper-opaque", + "language": "kotlin", + "inputVariant": "boot", + "change": "helper-opaque", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "kotlin/war/helper-pure", + "language": "kotlin", + "inputVariant": "war", + "change": "helper-pure", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/helper-effect", + "language": "kotlin", + "inputVariant": "war", + "change": "helper-effect", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "REFUTED", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/helper-opaque", + "language": "kotlin", + "inputVariant": "war", + "change": "helper-opaque", + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "java/boot/guarded-string", + "language": "java", + "inputVariant": "boot", + "change": "guarded-string", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-string/base.java", + "headResource": "/safety/cases/java/guarded-string/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/guarded-string", + "language": "java", + "inputVariant": "war", + "change": "guarded-string", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-string/base.java", + "headResource": "/safety/cases/java/guarded-string/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/guarded-string", + "language": "kotlin", + "inputVariant": "boot", + "change": "guarded-string", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-string/base.kt", + "headResource": "/safety/cases/kotlin/guarded-string/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/guarded-string", + "language": "kotlin", + "inputVariant": "war", + "change": "guarded-string", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-string/base.kt", + "headResource": "/safety/cases/kotlin/guarded-string/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/unguarded-string", + "language": "java", + "inputVariant": "boot", + "change": "unguarded-string", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-string/base.java", + "headResource": "/safety/cases/java/unguarded-string/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/unguarded-string", + "language": "java", + "inputVariant": "war", + "change": "unguarded-string", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-string/base.java", + "headResource": "/safety/cases/java/unguarded-string/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/unguarded-string", + "language": "kotlin", + "inputVariant": "boot", + "change": "unguarded-string", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-string/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-string/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/unguarded-string", + "language": "kotlin", + "inputVariant": "war", + "change": "unguarded-string", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-string/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-string/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/guarded-chain", + "language": "java", + "inputVariant": "boot", + "change": "guarded-chain", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-chain/base.java", + "headResource": "/safety/cases/java/guarded-chain/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/guarded-chain", + "language": "java", + "inputVariant": "war", + "change": "guarded-chain", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-chain/base.java", + "headResource": "/safety/cases/java/guarded-chain/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/guarded-chain", + "language": "kotlin", + "inputVariant": "boot", + "change": "guarded-chain", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-chain/base.kt", + "headResource": "/safety/cases/kotlin/guarded-chain/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/guarded-chain", + "language": "kotlin", + "inputVariant": "war", + "change": "guarded-chain", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-chain/base.kt", + "headResource": "/safety/cases/kotlin/guarded-chain/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/context-only", + "language": "java", + "inputVariant": "boot", + "change": "context-only", + "sourcePair": { + "baseResource": "/safety/cases/java/context-only/base.java", + "headResource": "/safety/cases/java/context-only/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/context-only", + "language": "java", + "inputVariant": "war", + "change": "context-only", + "sourcePair": { + "baseResource": "/safety/cases/java/context-only/base.java", + "headResource": "/safety/cases/java/context-only/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/context-only", + "language": "kotlin", + "inputVariant": "boot", + "change": "context-only", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/context-only/base.kt", + "headResource": "/safety/cases/kotlin/context-only/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/context-only", + "language": "kotlin", + "inputVariant": "war", + "change": "context-only", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/context-only/base.kt", + "headResource": "/safety/cases/kotlin/context-only/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/literal-equals-nullable", + "language": "java", + "inputVariant": "boot", + "change": "literal-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/java/literal-equals-nullable/base.java", + "headResource": "/safety/cases/java/literal-equals-nullable/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/literal-equals-nullable", + "language": "java", + "inputVariant": "war", + "change": "literal-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/java/literal-equals-nullable/base.java", + "headResource": "/safety/cases/java/literal-equals-nullable/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/literal-equals-nullable", + "language": "kotlin", + "inputVariant": "boot", + "change": "literal-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/literal-equals-nullable/base.kt", + "headResource": "/safety/cases/kotlin/literal-equals-nullable/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/literal-equals-nullable", + "language": "kotlin", + "inputVariant": "war", + "change": "literal-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/literal-equals-nullable/base.kt", + "headResource": "/safety/cases/kotlin/literal-equals-nullable/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/instance-equals-nullable", + "language": "java", + "inputVariant": "boot", + "change": "instance-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/java/instance-equals-nullable/base.java", + "headResource": "/safety/cases/java/instance-equals-nullable/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/instance-equals-nullable", + "language": "java", + "inputVariant": "war", + "change": "instance-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/java/instance-equals-nullable/base.java", + "headResource": "/safety/cases/java/instance-equals-nullable/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/instance-equals-nullable", + "language": "kotlin", + "inputVariant": "boot", + "change": "instance-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/instance-equals-nullable/base.kt", + "headResource": "/safety/cases/kotlin/instance-equals-nullable/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/instance-equals-nullable", + "language": "kotlin", + "inputVariant": "war", + "change": "instance-equals-nullable", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/instance-equals-nullable/base.kt", + "headResource": "/safety/cases/kotlin/instance-equals-nullable/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/guarded-unbox", + "language": "java", + "inputVariant": "boot", + "change": "guarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-unbox/base.java", + "headResource": "/safety/cases/java/guarded-unbox/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/guarded-unbox", + "language": "java", + "inputVariant": "war", + "change": "guarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-unbox/base.java", + "headResource": "/safety/cases/java/guarded-unbox/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/guarded-unbox", + "language": "kotlin", + "inputVariant": "boot", + "change": "guarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-unbox/base.kt", + "headResource": "/safety/cases/kotlin/guarded-unbox/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/guarded-unbox", + "language": "kotlin", + "inputVariant": "war", + "change": "guarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-unbox/base.kt", + "headResource": "/safety/cases/kotlin/guarded-unbox/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/unguarded-unbox", + "language": "java", + "inputVariant": "boot", + "change": "unguarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-unbox/base.java", + "headResource": "/safety/cases/java/unguarded-unbox/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/unguarded-unbox", + "language": "java", + "inputVariant": "war", + "change": "unguarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-unbox/base.java", + "headResource": "/safety/cases/java/unguarded-unbox/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/unguarded-unbox", + "language": "kotlin", + "inputVariant": "boot", + "change": "unguarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-unbox/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-unbox/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/unguarded-unbox", + "language": "kotlin", + "inputVariant": "war", + "change": "unguarded-unbox", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-unbox/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-unbox/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/callee-safe", + "language": "java", + "inputVariant": "boot", + "change": "callee-safe", + "sourcePair": { + "baseResource": "/safety/cases/java/callee-safe/base.java", + "headResource": "/safety/cases/java/callee-safe/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/callee-safe", + "language": "java", + "inputVariant": "war", + "change": "callee-safe", + "sourcePair": { + "baseResource": "/safety/cases/java/callee-safe/base.java", + "headResource": "/safety/cases/java/callee-safe/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/callee-safe", + "language": "kotlin", + "inputVariant": "boot", + "change": "callee-safe", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/callee-safe/base.kt", + "headResource": "/safety/cases/kotlin/callee-safe/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/callee-safe", + "language": "kotlin", + "inputVariant": "war", + "change": "callee-safe", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/callee-safe/base.kt", + "headResource": "/safety/cases/kotlin/callee-safe/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/callee-NPE", + "language": "java", + "inputVariant": "boot", + "change": "callee-NPE", + "sourcePair": { + "baseResource": "/safety/cases/java/callee-NPE/base.java", + "headResource": "/safety/cases/java/callee-NPE/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/callee-NPE", + "language": "java", + "inputVariant": "war", + "change": "callee-NPE", + "sourcePair": { + "baseResource": "/safety/cases/java/callee-NPE/base.java", + "headResource": "/safety/cases/java/callee-NPE/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/callee-NPE", + "language": "kotlin", + "inputVariant": "boot", + "change": "callee-NPE", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/callee-NPE/base.kt", + "headResource": "/safety/cases/kotlin/callee-NPE/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/callee-NPE", + "language": "kotlin", + "inputVariant": "war", + "change": "callee-NPE", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/callee-NPE/base.kt", + "headResource": "/safety/cases/kotlin/callee-NPE/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "REFUTED", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/guarded-join", + "language": "java", + "inputVariant": "boot", + "change": "guarded-join", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-join/base.java", + "headResource": "/safety/cases/java/guarded-join/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/guarded-join", + "language": "java", + "inputVariant": "war", + "change": "guarded-join", + "sourcePair": { + "baseResource": "/safety/cases/java/guarded-join/base.java", + "headResource": "/safety/cases/java/guarded-join/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/guarded-join", + "language": "kotlin", + "inputVariant": "boot", + "change": "guarded-join", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-join/base.kt", + "headResource": "/safety/cases/kotlin/guarded-join/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/guarded-join", + "language": "kotlin", + "inputVariant": "war", + "change": "guarded-join", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/guarded-join/base.kt", + "headResource": "/safety/cases/kotlin/guarded-join/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/unguarded-join", + "language": "java", + "inputVariant": "boot", + "change": "unguarded-join", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-join/base.java", + "headResource": "/safety/cases/java/unguarded-join/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/unguarded-join", + "language": "java", + "inputVariant": "war", + "change": "unguarded-join", + "sourcePair": { + "baseResource": "/safety/cases/java/unguarded-join/base.java", + "headResource": "/safety/cases/java/unguarded-join/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/unguarded-join", + "language": "kotlin", + "inputVariant": "boot", + "change": "unguarded-join", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-join/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-join/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/unguarded-join", + "language": "kotlin", + "inputVariant": "war", + "change": "unguarded-join", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unguarded-join/base.kt", + "headResource": "/safety/cases/kotlin/unguarded-join/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/boot/resolved-alternatives", + "language": "java", + "inputVariant": "boot", + "change": "resolved-alternatives", + "sourcePair": { + "baseResource": "/safety/cases/java/resolved-alternatives/base.java", + "headResource": "/safety/cases/java/resolved-alternatives/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/war/resolved-alternatives", + "language": "java", + "inputVariant": "war", + "change": "resolved-alternatives", + "sourcePair": { + "baseResource": "/safety/cases/java/resolved-alternatives/base.java", + "headResource": "/safety/cases/java/resolved-alternatives/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/boot/resolved-alternatives", + "language": "kotlin", + "inputVariant": "boot", + "change": "resolved-alternatives", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/resolved-alternatives/base.kt", + "headResource": "/safety/cases/kotlin/resolved-alternatives/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "kotlin/war/resolved-alternatives", + "language": "kotlin", + "inputVariant": "war", + "change": "resolved-alternatives", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/resolved-alternatives/base.kt", + "headResource": "/safety/cases/kotlin/resolved-alternatives/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "PROVEN", + "NONTHROWING": "PROVEN", + "TERMINATING": "PROVEN", + "WHOLE_CHANGE_EQUIVALENT": "PROVEN" + }, + "decision": "ACCEPTED", + "basis": "STATIC_SAFETY", + "verdict": "PASS", + "diagnosticCodes": [] + } + }, + { + "id": "java/boot/opaque-alternative", + "language": "java", + "inputVariant": "boot", + "change": "opaque-alternative", + "sourcePair": { + "baseResource": "/safety/cases/java/opaque-alternative/base.java", + "headResource": "/safety/cases/java/opaque-alternative/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "java/war/opaque-alternative", + "language": "java", + "inputVariant": "war", + "change": "opaque-alternative", + "sourcePair": { + "baseResource": "/safety/cases/java/opaque-alternative/base.java", + "headResource": "/safety/cases/java/opaque-alternative/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "kotlin/boot/opaque-alternative", + "language": "kotlin", + "inputVariant": "boot", + "change": "opaque-alternative", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/opaque-alternative/base.kt", + "headResource": "/safety/cases/kotlin/opaque-alternative/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "kotlin/war/opaque-alternative", + "language": "kotlin", + "inputVariant": "war", + "change": "opaque-alternative", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/opaque-alternative/base.kt", + "headResource": "/safety/cases/kotlin/opaque-alternative/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "INCOMPLETE_GRAPH", + "UNVERIFIED_BUILD_INPUT" + ] + } + }, + { + "id": "java/boot/unresolved-dispatch", + "language": "java", + "inputVariant": "boot", + "change": "unresolved-dispatch", + "sourcePair": { + "baseResource": "/safety/cases/java/unresolved-dispatch/base.java", + "headResource": "/safety/cases/java/unresolved-dispatch/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "java/war/unresolved-dispatch", + "language": "java", + "inputVariant": "war", + "change": "unresolved-dispatch", + "sourcePair": { + "baseResource": "/safety/cases/java/unresolved-dispatch/base.java", + "headResource": "/safety/cases/java/unresolved-dispatch/head.java" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/boot/unresolved-dispatch", + "language": "kotlin", + "inputVariant": "boot", + "change": "unresolved-dispatch", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unresolved-dispatch/base.kt", + "headResource": "/safety/cases/kotlin/unresolved-dispatch/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + }, + { + "id": "kotlin/war/unresolved-dispatch", + "language": "kotlin", + "inputVariant": "war", + "change": "unresolved-dispatch", + "sourcePair": { + "baseResource": "/safety/cases/kotlin/unresolved-dispatch/base.kt", + "headResource": "/safety/cases/kotlin/unresolved-dispatch/head.kt" + }, + "evidenceRequirements": [ + "NONEMPTY_SUBJECT", + "NONEMPTY_CITATIONS", + "NONEMPTY_ASSUMPTIONS" + ], + "expected": { + "control": "UNCONTROLLED", + "claims": { + "EFFECT_FREE": "UNPROVEN", + "NONTHROWING": "UNPROVEN", + "TERMINATING": "UNPROVEN", + "WHOLE_CHANGE_EQUIVALENT": "UNPROVEN" + }, + "decision": "REJECTED", + "basis": "INCOMPLETE_PROOF", + "verdict": "FAIL", + "diagnosticCodes": [ + "PRODUCTION_OUTPUT_CHANGED" + ] + } + } +]