refactor(managed): split metadata analysis

This commit is contained in:
morluto
2026-07-18 05:49:14 +00:00
parent e2c868c4bd
commit 20c88dd567
17 changed files with 3687 additions and 2983 deletions
+10 -512
View File
@@ -1,16 +1,13 @@
import { createHash } from "node:crypto";
import { basename } from "node:path";
import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, parseEvidence, type Evidence } from "./evidence.js";
import {
createJavaScriptApplicationEdge,
createJavaScriptApplicationGraph,
createJavaScriptApplicationNode,
javascriptApplicationGraphSchema,
type ApplicationGraphEvidence,
type ApplicationEdge,
type ApplicationNode,
} from "./javascriptApplicationGraph.js";
import {
@@ -24,13 +21,16 @@ import {
import type { JsonValue } from "./jsonValue.js";
import {
assessManagedGraphOmissions,
completeManagedGraphCoverage,
managedGraphEvidenceCoverage,
managedGraphResultCoverage,
managedSourceCoverage,
managedSourcePageCoverage,
totalManagedGraphOmitted,
} from "./managedApplicationGraphCoverage.js";
import {
addArtifactNode,
addArtifactIdentityNodes,
addBoundaryNodes,
addMemberNodes,
} from "./managedApplicationGraphNodes.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
@@ -117,15 +117,7 @@ export type ManagedApplicationGraphResult = z.infer<
typeof managedApplicationGraphResultSchema
>;
type ManagedMethod = ManagedMemberInspection["methods"]["items"][number];
type ManagedField = ManagedMemberInspection["fields"]["items"][number];
type ManagedType = ManagedMemberInspection["types"]["items"][number];
type PinvokeImport =
ManagedNativeBoundaryInspection["pinvoke_imports"]["items"][number];
type NativeImplementation =
ManagedNativeBoundaryInspection["native_implementations"]["items"][number];
interface ParsedManagedGraphInput {
export interface ParsedManagedGraphInput {
readonly artifact: {
readonly evidence: Evidence;
readonly result: ManagedArtifactInspection;
@@ -140,9 +132,9 @@ interface ParsedManagedGraphInput {
} | null;
}
interface GraphBuildState {
export interface GraphBuildState {
readonly nodes: ApplicationNode[];
readonly edges: ReturnType<typeof createJavaScriptApplicationEdge>[];
readonly edges: ApplicationEdge[];
readonly artifactSha256: string;
readonly artifactPath: string;
readonly evidenceLinks: string[];
@@ -151,11 +143,6 @@ interface GraphBuildState {
readonly fieldNodes: Map<string, ApplicationNode>;
}
interface ContainsEdgeOptions {
readonly kind: string;
readonly coverage: ApplicationGraphEvidence["coverage"];
}
const sha256 = (value: JsonValue): string => {
const serialized = canonicalize(value);
if (serialized === undefined)
@@ -306,495 +293,6 @@ const assertSameArtifact = (
);
};
const addArtifactNode = (state: GraphBuildState): ApplicationNode => {
const node = createJavaScriptApplicationNode({
kind: "artifact",
identity: {
strategy: "content-digest",
stability: "global-exact",
sha256: state.artifactSha256,
},
observations: [
{
label: basename(state.artifactPath),
properties: {
format: "pe",
source: "managed-application-graph",
},
evidence: managedEvidence(state, "project-managed-artifact"),
},
],
});
state.nodes.push(node);
return node;
};
const addArtifactIdentityNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
): void => {
const artifact = parsed.artifact?.result;
const artifactCoverage = managedSourceCoverage(
parsed.artifact?.result.coverage.state ?? "complete",
);
if (artifact?.assembly !== undefined && artifact.assembly !== null) {
const assembly = createJavaScriptApplicationNode({
kind: "managed-assembly",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-assembly",
artifact.assembly.token,
),
observations: [
{
label: artifact.assembly.name,
properties: {
name: artifact.assembly.name,
version: artifact.assembly.version,
culture: artifact.assembly.culture,
public_key_kind: artifact.assembly.public_key.kind,
},
evidence: managedEvidence(
state,
"inspect_managed_artifact",
artifactCoverage,
),
},
],
});
state.nodes.push(assembly);
addContainsEdge(state, artifactNode, assembly, {
kind: "declares-managed-assembly",
coverage: artifactCoverage,
});
}
const module =
artifact?.module ??
parsed.members?.result.module ??
parsed.boundaries?.result.module;
if (module !== undefined && module !== null) {
const moduleCoverage = managedSourceCoverage(moduleCoverageState(parsed));
const moduleNode = createJavaScriptApplicationNode({
kind: "managed-module",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-module",
module.mvid ?? module.token,
),
observations: [
{
label: module.name,
properties: {
name: module.name,
mvid: module.mvid,
generation: module.generation,
token: module.token,
},
evidence: managedEvidence(
state,
"inspect_managed_artifact",
moduleCoverage,
),
},
],
});
state.nodes.push(moduleNode);
addContainsEdge(state, artifactNode, moduleNode, {
kind: "declares-managed-module",
coverage: moduleCoverage,
});
}
};
const addMemberNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
limits: ProjectManagedApplicationGraphInput["limits"],
) => {
const members = parsed.members?.result;
if (members === undefined) return { types: 0, methods: 0, fields: 0 };
const types = members.types.items.slice(0, limits.max_types);
const methods = members.methods.items.slice(0, limits.max_methods);
const fields = members.fields.items.slice(0, limits.max_fields);
const typeCoverage = managedSourcePageCoverage(
members.coverage.state,
members.types,
"types",
);
const methodCoverage = managedSourcePageCoverage(
members.coverage.state,
members.methods,
"methods",
);
const fieldCoverage = managedSourcePageCoverage(
members.coverage.state,
members.fields,
"fields",
);
for (const type of types) {
const node = typeNode(state, type, typeCoverage);
state.nodes.push(node);
state.typeNodes.set(type.token, node);
addContainsEdge(state, artifactNode, node, {
kind: "declares-managed-type",
coverage: typeCoverage,
});
}
for (const method of methods) {
const node = methodNode(state, method, methodCoverage);
state.nodes.push(node);
state.methodNodes.set(method.token, node);
const owner =
method.declaring_type_token === null
? artifactNode
: (state.typeNodes.get(method.declaring_type_token) ?? artifactNode);
addContainsEdge(state, owner, node, {
kind: "declares-managed-method",
coverage: methodCoverage,
});
}
for (const field of fields) {
const node = fieldNode(state, field, fieldCoverage);
state.nodes.push(node);
state.fieldNodes.set(field.token, node);
const owner =
field.declaring_type_token === null
? artifactNode
: (state.typeNodes.get(field.declaring_type_token) ?? artifactNode);
addContainsEdge(state, owner, node, {
kind: "declares-managed-field",
coverage: fieldCoverage,
});
}
return {
types: types.length,
methods: methods.length,
fields: fields.length,
};
};
const typeNode = (
state: GraphBuildState,
type: ManagedType,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-type",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-type-token",
type.token,
),
observations: [
{
label: type.full_name,
properties: {
token: type.token,
namespace: type.namespace,
name: type.name,
full_name: type.full_name,
flags: type.flags,
extends_token: type.extends_token,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
const methodNode = (
state: GraphBuildState,
method: ManagedMethod,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-method",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-method-token",
method.token,
),
observations: [
{
label:
method.declaring_type === null
? method.name
: `${method.declaring_type}.${method.name}`,
properties: {
token: method.token,
declaring_type_token: method.declaring_type_token,
declaring_type: method.declaring_type,
name: method.name,
rva: method.rva,
signature_sha256: method.signature.raw_sha256,
signature_status: method.signature.parse_status,
body_status: method.body.status,
normalized_il_sha256: method.body.normalized_il_sha256,
il_size: method.body.il_size,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
const fieldNode = (
state: GraphBuildState,
field: ManagedField,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-field",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-field-token",
field.token,
),
observations: [
{
label:
field.declaring_type === null
? field.name
: `${field.declaring_type}.${field.name}`,
properties: {
token: field.token,
declaring_type_token: field.declaring_type_token,
declaring_type: field.declaring_type,
name: field.name,
flags: field.flags,
signature_sha256: field.signature.raw_sha256,
signature_status: field.signature.parse_status,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
const addBoundaryNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
limits: ProjectManagedApplicationGraphInput["limits"],
) => {
const boundaries = parsed.boundaries?.result;
if (boundaries === undefined)
return { pinvoke_imports: 0, native_implementations: 0 };
const pinvokes = boundaries.pinvoke_imports.items.slice(
0,
limits.max_pinvoke_imports,
);
const implementations = boundaries.native_implementations.items.slice(
0,
limits.max_native_implementations,
);
const pinvokeCoverage = managedSourcePageCoverage(
boundaries.coverage.state,
boundaries.pinvoke_imports,
"pinvoke_imports",
);
const implementationCoverage = managedSourcePageCoverage(
boundaries.coverage.state,
boundaries.native_implementations,
"native_implementations",
);
for (const pinvoke of pinvokes) {
const node = pinvokeNode(state, pinvoke, pinvokeCoverage);
state.nodes.push(node);
const owner =
pinvoke.member_token === null
? artifactNode
: (state.methodNodes.get(pinvoke.member_token) ?? artifactNode);
state.edges.push(
createJavaScriptApplicationEdge({
source_node_id: owner.node_id,
target_node_id: node.node_id,
relation: "imports",
properties: {
kind: "managed-pinvoke",
import_name: pinvoke.import_name,
import_scope_name: pinvoke.import_scope_name,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
pinvokeCoverage,
),
}),
);
}
for (const implementation of implementations) {
const node = nativeImplementationNode(
state,
implementation,
implementationCoverage,
);
state.nodes.push(node);
const owner = state.methodNodes.get(implementation.token) ?? artifactNode;
addContainsEdge(state, owner, node, {
kind: "declares-managed-native-implementation",
coverage: implementationCoverage,
});
}
return {
pinvoke_imports: pinvokes.length,
native_implementations: implementations.length,
};
};
const pinvokeNode = (
state: GraphBuildState,
pinvoke: PinvokeImport,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-pinvoke-import",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-pinvoke-token",
pinvoke.token,
),
observations: [
{
label: pinvoke.import_name,
properties: {
token: pinvoke.token,
member_token: pinvoke.member_token,
member_name: pinvoke.member_name,
import_name: pinvoke.import_name,
import_scope_name: pinvoke.import_scope_name,
char_set: pinvoke.char_set,
call_convention: pinvoke.call_convention,
verification: pinvoke.verification,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
coverage,
),
},
],
});
const nativeImplementationNode = (
state: GraphBuildState,
implementation: NativeImplementation,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-native-implementation",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-native-implementation-token",
implementation.token,
),
observations: [
{
label: implementation.name,
properties: {
token: implementation.token,
name: implementation.name,
rva: implementation.rva,
code_type: implementation.code_type,
managed_kind: implementation.managed_kind,
pinvoke_declared: implementation.pinvoke_declared,
boundary_kind: implementation.boundary_kind,
body_interpretation: implementation.body_interpretation,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
coverage,
),
},
],
});
const addContainsEdge = (
state: GraphBuildState,
source: ApplicationNode,
target: ApplicationNode,
options: ContainsEdgeOptions,
): void => {
state.edges.push(
createJavaScriptApplicationEdge({
source_node_id: source.node_id,
target_node_id: target.node_id,
relation: "contains",
properties: { kind: options.kind },
evidence: managedEvidence(
state,
"project-managed-contains",
options.coverage,
),
}),
);
};
const artifactLocalIdentity = (
artifactSha256: string,
namespace: string,
key: string,
) => ({
strategy: "artifact-local-key" as const,
stability: "artifact-version" as const,
artifact_sha256: artifactSha256,
namespace,
key,
});
const managedEvidence = (
state: GraphBuildState,
operation: string,
coverage: ApplicationGraphEvidence["coverage"] = completeManagedGraphCoverage(),
): ApplicationGraphEvidence => ({
authority: "managed-static-analysis",
state: "observed",
confidence: "exact",
artifact: {
available: true,
artifact_id: `art_${state.artifactSha256}`,
sha256: state.artifactSha256,
},
location: {
available: true,
value: {
kind: "artifact-path",
path: graphArtifactPath(state.artifactPath),
},
},
extractor: {
name: "rea-dotnet-static",
version: "1",
operation,
executable_sha256: null,
},
coverage,
limitations:
coverage.status === "complete"
? []
: [
"The source managed Evidence slice is incomplete; unreturned or unavailable items remain unobserved.",
],
evidence_ids: state.evidenceLinks,
});
const graphArtifactPath = (path: string): string => {
const name = basename(path).replaceAll(/[^A-Za-z0-9._-]/gu, "_");
return `managed/${name.length === 0 ? "artifact.pe" : name}`;
};
const moduleCoverageState = (
parsed: ParsedManagedGraphInput,
): "complete" | "partial" | "unavailable" => {
const artifact = parsed.artifact;
if (artifact !== null && artifact.result.module !== null)
return artifact.result.coverage.state;
const members = parsed.members;
if (members !== null && members.result.module !== null)
return members.result.coverage.state;
return parsed.boundaries?.result.coverage.state ?? "complete";
};
const omittedCounts = (
parsed: ParsedManagedGraphInput,
limits: ProjectManagedApplicationGraphInput["limits"],
+532
View File
@@ -0,0 +1,532 @@
import { basename } from "node:path";
import {
createJavaScriptApplicationEdge,
createJavaScriptApplicationNode,
type ApplicationGraphEvidence,
type ApplicationNode,
} from "./javascriptApplicationGraph.js";
import {
managedSourceCoverage,
managedSourcePageCoverage,
type ManagedGraphProjectionLimits,
} from "./managedApplicationGraphCoverage.js";
import {
type ManagedMemberInspection,
type ManagedNativeBoundaryInspection,
} from "./managedArtifact.js";
import type { GraphBuildState, ParsedManagedGraphInput } from "./managedApplicationGraph.js";
type ManagedMethod = ManagedMemberInspection["methods"]["items"][number];
type ManagedField = ManagedMemberInspection["fields"]["items"][number];
type ManagedType = ManagedMemberInspection["types"]["items"][number];
type PinvokeImport =
ManagedNativeBoundaryInspection["pinvoke_imports"]["items"][number];
type NativeImplementation =
ManagedNativeBoundaryInspection["native_implementations"]["items"][number];
/** Options for a managed contains relationship edge. */
export interface ContainsEdgeOptions {
readonly kind: string;
readonly coverage: ApplicationGraphEvidence["coverage"];
}
/** Add the root managed artifact node to the graph state. */
export const addArtifactNode = (state: GraphBuildState): ApplicationNode => {
const node = createJavaScriptApplicationNode({
kind: "artifact",
identity: {
strategy: "content-digest",
stability: "global-exact",
sha256: state.artifactSha256,
},
observations: [
{
label: basename(state.artifactPath),
properties: {
format: "pe",
source: "managed-application-graph",
},
evidence: managedEvidence(state, "project-managed-artifact"),
},
],
});
state.nodes.push(node);
return node;
};
/** Add managed assembly and module identity nodes for the artifact. */
export const addArtifactIdentityNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
): void => {
const artifact = parsed.artifact?.result;
const artifactCoverage = managedSourceCoverage(
parsed.artifact?.result.coverage.state ?? "complete",
);
if (artifact?.assembly !== undefined && artifact.assembly !== null) {
const assembly = createJavaScriptApplicationNode({
kind: "managed-assembly",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-assembly",
artifact.assembly.token,
),
observations: [
{
label: artifact.assembly.name,
properties: {
name: artifact.assembly.name,
version: artifact.assembly.version,
culture: artifact.assembly.culture,
public_key_kind: artifact.assembly.public_key.kind,
},
evidence: managedEvidence(
state,
"inspect_managed_artifact",
artifactCoverage,
),
},
],
});
state.nodes.push(assembly);
addContainsEdge(state, artifactNode, assembly, {
kind: "declares-managed-assembly",
coverage: artifactCoverage,
});
}
const module =
artifact?.module ??
parsed.members?.result.module ??
parsed.boundaries?.result.module;
if (module !== undefined && module !== null) {
const moduleCoverage = managedSourceCoverage(moduleCoverageState(parsed));
const moduleNode = createJavaScriptApplicationNode({
kind: "managed-module",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-module",
module.mvid ?? module.token,
),
observations: [
{
label: module.name,
properties: {
name: module.name,
mvid: module.mvid,
generation: module.generation,
token: module.token,
},
evidence: managedEvidence(
state,
"inspect_managed_artifact",
moduleCoverage,
),
},
],
});
state.nodes.push(moduleNode);
addContainsEdge(state, artifactNode, moduleNode, {
kind: "declares-managed-module",
coverage: moduleCoverage,
});
}
};
/** Add managed type, method, and field nodes from member evidence. */
export const addMemberNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
limits: ManagedGraphProjectionLimits,
) => {
const members = parsed.members?.result;
if (members === undefined) return { types: 0, methods: 0, fields: 0 };
const types = members.types.items.slice(0, limits.max_types);
const methods = members.methods.items.slice(0, limits.max_methods);
const fields = members.fields.items.slice(0, limits.max_fields);
const typeCoverage = managedSourcePageCoverage(
members.coverage.state,
members.types,
"types",
);
const methodCoverage = managedSourcePageCoverage(
members.coverage.state,
members.methods,
"methods",
);
const fieldCoverage = managedSourcePageCoverage(
members.coverage.state,
members.fields,
"fields",
);
for (const type of types) {
const node = typeNode(state, type, typeCoverage);
state.nodes.push(node);
state.typeNodes.set(type.token, node);
addContainsEdge(state, artifactNode, node, {
kind: "declares-managed-type",
coverage: typeCoverage,
});
}
for (const method of methods) {
const node = methodNode(state, method, methodCoverage);
state.nodes.push(node);
state.methodNodes.set(method.token, node);
const owner =
method.declaring_type_token === null
? artifactNode
: (state.typeNodes.get(method.declaring_type_token) ?? artifactNode);
addContainsEdge(state, owner, node, {
kind: "declares-managed-method",
coverage: methodCoverage,
});
}
for (const field of fields) {
const node = fieldNode(state, field, fieldCoverage);
state.nodes.push(node);
state.fieldNodes.set(field.token, node);
const owner =
field.declaring_type_token === null
? artifactNode
: (state.typeNodes.get(field.declaring_type_token) ?? artifactNode);
addContainsEdge(state, owner, node, {
kind: "declares-managed-field",
coverage: fieldCoverage,
});
}
return {
types: types.length,
methods: methods.length,
fields: fields.length,
};
};
/** Build a managed type application node. */
export const typeNode = (
state: GraphBuildState,
type: ManagedType,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-type",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-type-token",
type.token,
),
observations: [
{
label: type.full_name,
properties: {
token: type.token,
namespace: type.namespace,
name: type.name,
full_name: type.full_name,
flags: type.flags,
extends_token: type.extends_token,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
/** Build a managed method application node. */
export const methodNode = (
state: GraphBuildState,
method: ManagedMethod,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-method",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-method-token",
method.token,
),
observations: [
{
label:
method.declaring_type === null
? method.name
: `${method.declaring_type}.${method.name}`,
properties: {
token: method.token,
declaring_type_token: method.declaring_type_token,
declaring_type: method.declaring_type,
name: method.name,
rva: method.rva,
signature_sha256: method.signature.raw_sha256,
signature_status: method.signature.parse_status,
body_status: method.body.status,
normalized_il_sha256: method.body.normalized_il_sha256,
il_size: method.body.il_size,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
/** Build a managed field application node. */
export const fieldNode = (
state: GraphBuildState,
field: ManagedField,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-field",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-field-token",
field.token,
),
observations: [
{
label:
field.declaring_type === null
? field.name
: `${field.declaring_type}.${field.name}`,
properties: {
token: field.token,
declaring_type_token: field.declaring_type_token,
declaring_type: field.declaring_type,
name: field.name,
flags: field.flags,
signature_sha256: field.signature.raw_sha256,
signature_status: field.signature.parse_status,
},
evidence: managedEvidence(state, "inspect_managed_members", coverage),
},
],
});
/** Add P/Invoke import and native implementation boundary nodes. */
export const addBoundaryNodes = (
state: GraphBuildState,
artifactNode: ApplicationNode,
parsed: ParsedManagedGraphInput,
limits: ManagedGraphProjectionLimits,
) => {
const boundaries = parsed.boundaries?.result;
if (boundaries === undefined)
return { pinvoke_imports: 0, native_implementations: 0 };
const pinvokes = boundaries.pinvoke_imports.items.slice(
0,
limits.max_pinvoke_imports,
);
const implementations = boundaries.native_implementations.items.slice(
0,
limits.max_native_implementations,
);
const pinvokeCoverage = managedSourcePageCoverage(
boundaries.coverage.state,
boundaries.pinvoke_imports,
"pinvoke_imports",
);
const implementationCoverage = managedSourcePageCoverage(
boundaries.coverage.state,
boundaries.native_implementations,
"native_implementations",
);
for (const pinvoke of pinvokes) {
const node = pinvokeNode(state, pinvoke, pinvokeCoverage);
state.nodes.push(node);
const owner =
pinvoke.member_token === null
? artifactNode
: (state.methodNodes.get(pinvoke.member_token) ?? artifactNode);
state.edges.push(
createJavaScriptApplicationEdge({
source_node_id: owner.node_id,
target_node_id: node.node_id,
relation: "imports",
properties: {
kind: "managed-pinvoke",
import_name: pinvoke.import_name,
import_scope_name: pinvoke.import_scope_name,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
pinvokeCoverage,
),
}),
);
}
for (const implementation of implementations) {
const node = nativeImplementationNode(
state,
implementation,
implementationCoverage,
);
state.nodes.push(node);
const owner = state.methodNodes.get(implementation.token) ?? artifactNode;
addContainsEdge(state, owner, node, {
kind: "declares-managed-native-implementation",
coverage: implementationCoverage,
});
}
return {
pinvoke_imports: pinvokes.length,
native_implementations: implementations.length,
};
};
/** Build a managed P/Invoke import application node. */
export const pinvokeNode = (
state: GraphBuildState,
pinvoke: PinvokeImport,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-pinvoke-import",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-pinvoke-token",
pinvoke.token,
),
observations: [
{
label: pinvoke.import_name,
properties: {
token: pinvoke.token,
member_token: pinvoke.member_token,
member_name: pinvoke.member_name,
import_name: pinvoke.import_name,
import_scope_name: pinvoke.import_scope_name,
char_set: pinvoke.char_set,
call_convention: pinvoke.call_convention,
verification: pinvoke.verification,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
coverage,
),
},
],
});
/** Build a managed native implementation application node. */
export const nativeImplementationNode = (
state: GraphBuildState,
implementation: NativeImplementation,
coverage: ApplicationGraphEvidence["coverage"],
): ApplicationNode =>
createJavaScriptApplicationNode({
kind: "managed-native-implementation",
identity: artifactLocalIdentity(
state.artifactSha256,
"managed-native-implementation-token",
implementation.token,
),
observations: [
{
label: implementation.name,
properties: {
token: implementation.token,
name: implementation.name,
rva: implementation.rva,
code_type: implementation.code_type,
managed_kind: implementation.managed_kind,
pinvoke_declared: implementation.pinvoke_declared,
boundary_kind: implementation.boundary_kind,
body_interpretation: implementation.body_interpretation,
},
evidence: managedEvidence(
state,
"inspect_managed_native_boundaries",
coverage,
),
},
],
});
/** Add a contains relationship edge between two nodes. */
export const addContainsEdge = (
state: GraphBuildState,
source: ApplicationNode,
target: ApplicationNode,
options: ContainsEdgeOptions,
): void => {
state.edges.push(
createJavaScriptApplicationEdge({
source_node_id: source.node_id,
target_node_id: target.node_id,
relation: "contains",
properties: { kind: options.kind },
evidence: managedEvidence(
state,
"project-managed-contains",
options.coverage,
),
}),
);
};
/** Build an artifact-local key identity for a managed graph entity. */
export const artifactLocalIdentity = (
artifactSha256: string,
namespace: string,
key: string,
) => ({
strategy: "artifact-local-key" as const,
stability: "artifact-version" as const,
artifact_sha256: artifactSha256,
namespace,
key,
});
/** Build managed static-analysis evidence for a graph observation. */
export const managedEvidence = (
state: GraphBuildState,
operation: string,
coverage: ApplicationGraphEvidence["coverage"] = managedSourceCoverage("complete"),
): ApplicationGraphEvidence => ({
authority: "managed-static-analysis",
state: "observed",
confidence: "exact",
artifact: {
available: true,
artifact_id: `art_${state.artifactSha256}`,
sha256: state.artifactSha256,
},
location: {
available: true,
value: {
kind: "artifact-path",
path: graphArtifactPath(state.artifactPath),
},
},
extractor: {
name: "rea-dotnet-static",
version: "1",
operation,
executable_sha256: null,
},
coverage,
limitations:
coverage.status === "complete"
? []
: [
"The source managed Evidence slice is incomplete; unreturned or unavailable items remain unobserved.",
],
evidence_ids: state.evidenceLinks,
});
/** Sanitize an artifact path into a managed graph location. */
export const graphArtifactPath = (path: string): string => {
const name = basename(path).replaceAll(/[^A-Za-z0-9._-]/gu, "_");
return `managed/${name.length === 0 ? "artifact.pe" : name}`;
};
/** Derive the module coverage state from the best available evidence source. */
export const moduleCoverageState = (
parsed: ParsedManagedGraphInput,
): "complete" | "partial" | "unavailable" =>
parsed.artifact !== null && parsed.artifact.result.module !== null
? parsed.artifact.result.coverage.state
: parsed.members !== null && parsed.members.result.module !== null
? parsed.members.result.coverage.state
: (parsed.boundaries?.result.coverage.state ?? "complete");
+32 -623
View File
@@ -1,16 +1,24 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, parseEvidence } from "./evidence.js";
import { evidenceSchema } from "./evidence.js";
import {
cliMetadataGuidSchema,
managedMemberInspectionSchema,
type ManagedMemberInspection,
} from "./managedArtifact.js";
import type { JsonValue } from "./jsonValue.js";
import { assessKnownPageCoverage } from "./knownPageCoverage.js";
import {
buildComparisonCoverage,
buildComparisonMatching,
buildComparisonSummary,
comparisonLimitations,
sideManifest,
} from "./managedMemberComparisonCoverage.js";
import {
buildFieldItems,
buildMethodItems,
keyMembers,
sha256,
} from "./managedMemberComparisonMatch.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
@@ -176,44 +184,12 @@ export type ManagedMemberComparisonResult = z.infer<
typeof managedMemberComparisonResultSchema
>;
type Method = ManagedMemberInspection["methods"]["items"][number];
type Field = ManagedMemberInspection["fields"]["items"][number];
type MethodItem = ManagedMemberComparisonResult["methods"][number];
type FieldItem = ManagedMemberComparisonResult["fields"][number];
type MatchBasis = z.infer<typeof matchBasisSchema>;
/** One authenticated side of a managed member comparison. */
export interface ManagedMemberComparisonSide {
readonly evidenceId: string;
readonly result: ManagedMemberInspection;
}
interface Keyed<Item> {
readonly item: Item;
readonly exactKey: string | null;
readonly structuralKey: string | null;
}
interface MatchedPair<Item> {
readonly left: Keyed<Item>;
readonly right: Keyed<Item>;
readonly basis: MatchBasis;
readonly confidence: "exact" | "high";
}
interface Ambiguous<Item> {
readonly left: readonly Keyed<Item>[];
readonly right: readonly Keyed<Item>[];
readonly basis: MatchBasis;
}
const sha256 = (value: JsonValue): string => {
const serialized = canonicalize(value);
if (serialized === undefined)
throw new TypeError("Managed comparison canonicalization failed");
return createHash("sha256").update(serialized).digest("hex");
};
/** Compare two parsed managed member observations without name-based matching. */
export const compareManagedMembers = (
left: ManagedMemberComparisonSide,
@@ -224,56 +200,24 @@ export const compareManagedMembers = (
const rightMethodPage = assessKnownPageCoverage(right.result.methods);
const leftFieldPage = assessKnownPageCoverage(left.result.fields);
const rightFieldPage = assessKnownPageCoverage(right.result.fields);
const methodMatches = matchMethods(
left.result.methods.items.map(keyMethod),
right.result.methods.items.map(keyMethod),
const { methodMatches, fieldMatches } = keyMembers(
left,
right,
limits.max_candidates,
);
const fieldMatches = matchFields(
left.result.fields.items.map(keyField),
right.result.fields.items.map(keyField),
limits.max_candidates,
);
const methodItems = buildMethodItems(methodMatches, {
const itemContext = {
leftEvidenceId: left.evidenceId,
rightEvidenceId: right.evidenceId,
leftComplete: leftMethodPage.sourceComplete,
rightComplete: rightMethodPage.sourceComplete,
leftComplete: leftMethodPage.sourceComplete && leftFieldPage.sourceComplete,
rightComplete:
rightMethodPage.sourceComplete && rightFieldPage.sourceComplete,
limits,
});
const fieldItems = buildFieldItems(fieldMatches, {
leftEvidenceId: left.evidenceId,
rightEvidenceId: right.evidenceId,
leftComplete: leftFieldPage.sourceComplete,
rightComplete: rightFieldPage.sourceComplete,
limits,
});
};
const methodItems = buildMethodItems(methodMatches, itemContext);
const fieldItems = buildFieldItems(fieldMatches, itemContext);
const allItems = [...methodItems.items, ...fieldItems.items];
const summary = {
unchanged: allItems.filter(({ status }) => status === "unchanged").length,
changed: allItems.filter(({ status }) => status === "changed").length,
added: allItems.filter(({ status }) => status === "added").length,
removed: allItems.filter(({ status }) => status === "removed").length,
unknown: allItems.filter(({ status }) => status === "unknown").length,
};
const matching = {
exact_il_signature: methodItems.items.filter(
({ match }) => match.basis === "exact-il-signature",
).length,
structural_method_shape: methodItems.items.filter(
({ match }) => match.basis === "structural-method-shape",
).length,
field_signature: fieldItems.items.filter(
({ match }) => match.basis === "field-signature",
).length,
ambiguous:
methodItems.items.filter(({ match }) => match.status === "ambiguous")
.length +
fieldItems.items.filter(({ match }) => match.status === "ambiguous")
.length,
unmatched: allItems.filter(({ match }) => match.status === "unmatched")
.length,
};
const summary = buildComparisonSummary(allItems);
const matching = buildComparisonMatching(methodItems.items, fieldItems.items);
const coverage = buildComparisonCoverage({
left: left.result,
right: right.result,
@@ -293,7 +237,7 @@ export const compareManagedMembers = (
coverage.omitted_candidates,
);
const result = {
schema_version: 1,
schema_version: 1 as const,
comparison_id: `mmc_${sha256({
left: left.evidenceId,
right: right.evidenceId,
@@ -302,10 +246,10 @@ export const compareManagedMembers = (
limits,
})}`,
algorithm: {
name: "rea-managed-member-comparison",
version: 1,
token_identity: "build-local",
name_matching: "not-used",
name: "rea-managed-member-comparison" as const,
version: 1 as const,
token_identity: "build-local" as const,
name_matching: "not-used" as const,
},
left: sideManifest(left),
right: sideManifest(right),
@@ -320,539 +264,4 @@ export const compareManagedMembers = (
return managedMemberComparisonResultSchema.parse(result);
};
type PageAssessment = ReturnType<typeof assessKnownPageCoverage>;
interface ComparisonCoverageInput {
readonly left: ManagedMemberInspection;
readonly right: ManagedMemberInspection;
readonly leftMethodPage: PageAssessment;
readonly rightMethodPage: PageAssessment;
readonly leftFieldPage: PageAssessment;
readonly rightFieldPage: PageAssessment;
readonly omittedMethodItems: number;
readonly omittedFieldItems: number;
readonly omittedCandidates: number;
}
const buildComparisonCoverage = ({
left,
right,
leftMethodPage,
rightMethodPage,
leftFieldPage,
rightFieldPage,
omittedMethodItems,
omittedFieldItems,
omittedCandidates,
}: ComparisonCoverageInput): ManagedMemberComparisonResult["coverage"] => {
const omittedMethods =
omittedMethodItems +
leftMethodPage.sourceOmittedCount +
rightMethodPage.sourceOmittedCount;
const omittedFields =
omittedFieldItems +
leftFieldPage.sourceOmittedCount +
rightFieldPage.sourceOmittedCount;
const leftStatus = sideCoverageStatus(
left,
leftMethodPage.sourceComplete && leftFieldPage.sourceComplete,
);
const rightStatus = sideCoverageStatus(
right,
rightMethodPage.sourceComplete && rightFieldPage.sourceComplete,
);
const unknownInput =
left.coverage.state !== "complete" || right.coverage.state !== "complete";
const truncated = omittedMethods + omittedFields + omittedCandidates > 0;
return {
status: unknownInput
? "partial"
: truncated
? "truncated"
: leftStatus === "complete" && rightStatus === "complete"
? "complete-within-inputs"
: "partial",
left_status: leftStatus,
right_status: rightStatus,
omitted_methods: omittedMethods,
omitted_fields: omittedFields,
omitted_candidates: omittedCandidates,
};
};
const sideCoverageStatus = (
result: ManagedMemberInspection,
pagesComplete: boolean,
): ManagedMemberComparisonResult["coverage"]["left_status"] =>
result.coverage.state === "unavailable"
? "unavailable"
: result.coverage.state === "complete" && pagesComplete
? "complete"
: "partial";
const sideManifest = (
side: ManagedMemberComparisonSide,
): ManagedMemberComparisonResult["left"] => ({
evidence_id: side.evidenceId,
artifact_sha256: side.result.artifact.sha256,
mvid: side.result.module?.mvid ?? null,
module_name: side.result.module?.name ?? null,
metadata_status: side.result.metadata.status,
methods_total: side.result.methods.total,
fields_total: side.result.fields.total,
});
const keyMethod = (item: Method): Keyed<Method> => ({
item,
exactKey:
item.signature.parse_status === "decoded" &&
item.body.status === "present" &&
item.body.normalized_il_sha256 !== null
? stableKey([
"method-exact",
item.signature.raw_sha256,
item.body.normalized_il_sha256,
])
: null,
structuralKey:
item.body.status === "present"
? stableKey([
"method-structural",
item.signature.kind,
item.signature.calling_convention,
item.signature.generic_parameter_count,
item.signature.parameter_count,
item.signature.return_type,
item.signature.parameter_types,
item.body.status,
item.body.header_format,
item.body.il_size,
item.body.max_stack,
item.body.init_locals,
item.body.opcode_counts,
item.body.anchors.map(({ opcode, operand_kind }) => [
opcode,
operand_kind,
]),
item.body.exception_regions.map((region) => [
region.flags,
region.try_length,
region.handler_length,
region.class_token === null ? null : "type-token",
region.filter_offset === null ? null : "filter",
]),
])
: null,
});
const keyField = (item: Field): Keyed<Field> => ({
item,
exactKey:
item.signature.parse_status === "decoded"
? stableKey(["field-exact", item.signature.raw_sha256])
: null,
structuralKey: stableKey([
"field-structural",
item.signature.kind,
item.signature.field_type,
item.flags,
]),
});
const stableKey = (value: JsonValue): string => sha256(value);
const matchMethods = (
left: readonly Keyed<Method>[],
right: readonly Keyed<Method>[],
maxCandidates: number,
) =>
matchByKeys(left, right, maxCandidates, "exact-il-signature", [
"structural-method-shape",
]);
const matchFields = (
left: readonly Keyed<Field>[],
right: readonly Keyed<Field>[],
maxCandidates: number,
) => matchByKeys(left, right, maxCandidates, "field-signature", []);
const matchByKeys = <Item>(
left: readonly Keyed<Item>[],
right: readonly Keyed<Item>[],
maxCandidates: number,
exactBasis: MatchBasis,
fallbackBases: readonly MatchBasis[],
): {
readonly pairs: readonly MatchedPair<Item>[];
readonly ambiguous: readonly Ambiguous<Item>[];
readonly leftOnly: readonly Keyed<Item>[];
readonly rightOnly: readonly Keyed<Item>[];
readonly omittedCandidates: number;
} => {
const usedLeft = new Set<Keyed<Item>>();
const usedRight = new Set<Keyed<Item>>();
const pairs: MatchedPair<Item>[] = [];
const ambiguous: Ambiguous<Item>[] = [];
let omittedCandidates = 0;
const rounds: readonly {
readonly basis: MatchBasis;
readonly key: (item: Keyed<Item>) => string | null;
}[] = [
{ basis: exactBasis, key: ({ exactKey }) => exactKey },
...fallbackBases.map((basis) => ({
basis,
key: ({ structuralKey }: Keyed<Item>) => structuralKey,
})),
];
for (const round of rounds) {
const leftGroups = groupBy(
left.filter((item) => !usedLeft.has(item)),
round.key,
);
const rightGroups = groupBy(
right.filter((item) => !usedRight.has(item)),
round.key,
);
for (const [key, leftItems] of leftGroups) {
const rightItems = rightGroups.get(key);
if (rightItems === undefined) continue;
if (leftItems.length === 1 && rightItems.length === 1) {
const [leftItem] = leftItems;
const [rightItem] = rightItems;
if (leftItem === undefined || rightItem === undefined) continue;
usedLeft.add(leftItem);
usedRight.add(rightItem);
pairs.push({
left: leftItem,
right: rightItem,
basis: round.basis,
confidence:
round.basis === "exact-il-signature" ||
round.basis === "field-signature"
? "exact"
: "high",
});
} else {
for (const item of leftItems) usedLeft.add(item);
for (const item of rightItems) usedRight.add(item);
omittedCandidates += Math.max(
0,
leftItems.length + rightItems.length - maxCandidates * 2,
);
ambiguous.push({
left: leftItems.slice(0, maxCandidates),
right: rightItems.slice(0, maxCandidates),
basis: round.basis,
});
}
}
}
return {
pairs,
ambiguous,
leftOnly: left.filter((item) => !usedLeft.has(item)),
rightOnly: right.filter((item) => !usedRight.has(item)),
omittedCandidates,
};
};
const groupBy = <Item>(
items: readonly Item[],
keyOf: (item: Item) => string | null,
): Map<string, readonly Item[]> => {
const grouped = new Map<string, Item[]>();
for (const item of items) {
const key = keyOf(item);
if (key === null) continue;
const bucket = grouped.get(key) ?? [];
bucket.push(item);
grouped.set(key, bucket);
}
return grouped;
};
const buildMethodItems = (
matches: ReturnType<typeof matchMethods>,
context: ComparisonItemContext,
): { readonly items: MethodItem[]; readonly omitted: number } => {
const items: MethodItem[] = [];
for (const pair of matches.pairs) {
const dimensions = changedMethodDimensions(pair.left.item, pair.right.item);
items.push({
item_id: `mmc_method_${sha256({
left: pair.left.item.token,
right: pair.right.item.token,
basis: pair.basis,
})}`,
status: dimensions.length === 0 ? "unchanged" : "changed",
left: methodIdentity(pair.left.item),
right: methodIdentity(pair.right.item),
match: {
status: "matched",
basis: pair.basis,
confidence: pair.confidence,
candidate_left_tokens: [],
candidate_right_tokens: [],
},
dimensions,
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [],
});
}
for (const ambiguous of matches.ambiguous) {
items.push({
item_id: `mmc_method_${sha256({
left: ambiguous.left.map(({ item }) => item.token),
right: ambiguous.right.map(({ item }) => item.token),
basis: ambiguous.basis,
})}`,
status: "unknown",
left: null,
right: null,
match: {
status: "ambiguous",
basis: ambiguous.basis,
confidence: "unknown",
candidate_left_tokens: ambiguous.left.map(({ item }) => item.token),
candidate_right_tokens: ambiguous.right.map(({ item }) => item.token),
},
dimensions: ["availability"],
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [
"Multiple managed methods share the same non-name identity key; REA did not guess a token remap.",
],
});
}
for (const item of matches.leftOnly)
items.push(methodOnlyItem(item.item, context, true));
for (const item of matches.rightOnly)
items.push(methodOnlyItem(item.item, context, false));
return limitItems(items, context.limits.max_method_matches);
};
const buildFieldItems = (
matches: ReturnType<typeof matchFields>,
context: ComparisonItemContext,
): { readonly items: FieldItem[]; readonly omitted: number } => {
const items: FieldItem[] = [];
for (const pair of matches.pairs) {
const changed =
pair.left.item.signature.raw_sha256 !==
pair.right.item.signature.raw_sha256 ||
pair.left.item.flags !== pair.right.item.flags;
items.push({
item_id: `mmc_field_${sha256({
left: pair.left.item.token,
right: pair.right.item.token,
})}`,
status: changed ? "changed" : "unchanged",
left: fieldIdentity(pair.left.item),
right: fieldIdentity(pair.right.item),
match: {
status: "matched",
basis: pair.basis,
confidence: pair.confidence,
candidate_left_tokens: [],
candidate_right_tokens: [],
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [],
});
}
for (const ambiguous of matches.ambiguous)
items.push({
item_id: `mmc_field_${sha256({
left: ambiguous.left.map(({ item }) => item.token),
right: ambiguous.right.map(({ item }) => item.token),
})}`,
status: "unknown",
left: null,
right: null,
match: {
status: "ambiguous",
basis: ambiguous.basis,
confidence: "unknown",
candidate_left_tokens: ambiguous.left.map(({ item }) => item.token),
candidate_right_tokens: ambiguous.right.map(({ item }) => item.token),
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [
"Multiple managed fields share the same signature; REA did not guess a token remap from names.",
],
});
for (const item of matches.leftOnly)
items.push(fieldOnlyItem(item.item, context, true));
for (const item of matches.rightOnly)
items.push(fieldOnlyItem(item.item, context, false));
return limitItems(items, context.limits.max_field_matches);
};
interface ComparisonItemContext {
readonly leftEvidenceId: string;
readonly rightEvidenceId: string;
readonly leftComplete: boolean;
readonly rightComplete: boolean;
readonly limits: CompareManagedMembersInput["limits"];
}
const limitItems = <Item>(
items: readonly Item[],
limit: number,
): { readonly items: Item[]; readonly omitted: number } => ({
items: items.slice(0, limit),
omitted: Math.max(0, items.length - limit),
});
const methodOnlyItem = (
item: Method,
context: ComparisonItemContext,
left: boolean,
): MethodItem => {
const absenceObserved = left ? context.rightComplete : context.leftComplete;
return {
item_id: `mmc_method_${sha256({ token: item.token, side: left ? "left" : "right" })}`,
status: absenceObserved ? (left ? "removed" : "added") : "unknown",
left: left ? methodIdentity(item) : null,
right: left ? null : methodIdentity(item),
match: {
status: "unmatched",
basis: "none",
confidence: "unknown",
candidate_left_tokens: [],
candidate_right_tokens: [],
},
dimensions: ["availability"],
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: absenceObserved
? []
: [
`unknown-within-unobserved-page: The ${left ? "right" : "left"} method page is incomplete, so absence was not observed.`,
],
};
};
const fieldOnlyItem = (
item: Field,
context: ComparisonItemContext,
left: boolean,
): FieldItem => {
const absenceObserved = left ? context.rightComplete : context.leftComplete;
return {
item_id: `mmc_field_${sha256({ token: item.token, side: left ? "left" : "right" })}`,
status: absenceObserved ? (left ? "removed" : "added") : "unknown",
left: left ? fieldIdentity(item) : null,
right: left ? null : fieldIdentity(item),
match: {
status: "unmatched",
basis: "none",
confidence: "unknown",
candidate_left_tokens: [],
candidate_right_tokens: [],
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: absenceObserved
? []
: [
`unknown-within-unobserved-page: The ${left ? "right" : "left"} field page is incomplete, so absence was not observed.`,
],
};
};
const methodIdentity = (item: Method): NonNullable<MethodItem["left"]> => ({
token: item.token,
declaring_type: item.declaring_type,
name: item.name,
signature_sha256: item.signature.raw_sha256,
normalized_il_sha256: item.body.normalized_il_sha256,
});
const fieldIdentity = (item: Field): NonNullable<FieldItem["left"]> => ({
token: item.token,
declaring_type: item.declaring_type,
name: item.name,
signature_sha256: item.signature.raw_sha256,
});
const changedMethodDimensions = (
left: Method,
right: Method,
): MethodItem["dimensions"] => {
const dimensions: MethodItem["dimensions"] = [];
if (left.signature.raw_sha256 !== right.signature.raw_sha256)
dimensions.push("signature");
if (left.body.normalized_il_sha256 !== right.body.normalized_il_sha256)
dimensions.push("cil");
if (
canonicalize(left.body.opcode_counts) !==
canonicalize(right.body.opcode_counts)
)
dimensions.push("opcode-shape");
if (callShape(left.body.anchors) !== callShape(right.body.anchors))
dimensions.push("call-shape");
if (fieldShape(left.body.anchors) !== fieldShape(right.body.anchors))
dimensions.push("field-shape");
if (
canonicalize(left.body.exception_regions) !==
canonicalize(right.body.exception_regions)
)
dimensions.push("exception-shape");
if (left.body.status !== right.body.status) dimensions.push("availability");
return dimensions;
};
const callShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
.filter(({ operand_kind }) => operand_kind === "method")
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
const fieldShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
.filter(({ operand_kind }) => operand_kind === "field")
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
const comparisonLimitations = (
left: ManagedMemberInspection,
right: ManagedMemberInspection,
omittedItems: number,
omittedCandidates: number,
): string[] => {
const limitations: string[] = [
"Metadata tokens are build-local coordinates; matched pairs are remaps, not persistent identities.",
"Names are reported as observations but are not used as a matching basis.",
];
if (!left.methods.complete || !right.methods.complete)
limitations.push("At least one method page is incomplete.");
if (!left.fields.complete || !right.fields.complete)
limitations.push("At least one field page is incomplete.");
if (left.coverage.state !== "complete" || right.coverage.state !== "complete")
limitations.push("At least one managed member observation is partial.");
if (omittedItems > 0)
limitations.push(
`${String(omittedItems)} comparison items were omitted by output limits.`,
);
if (omittedCandidates > 0)
limitations.push(
`${String(omittedCandidates)} ambiguous candidates were omitted by candidate limits.`,
);
return limitations;
};
/** Extract and authenticate a managed member inspection Evidence record. */
export const parseManagedMemberEvidence = (
evidence: unknown,
): {
readonly evidenceId: string;
readonly result: ManagedMemberInspection;
} => {
const parsed = parseEvidence(evidence);
if (parsed.operation !== "inspect_managed_members")
throw new TypeError("Evidence operation is not inspect_managed_members");
return {
evidenceId: parsed.evidence_id,
result: managedMemberInspectionSchema.parse(parsed.normalized_result),
};
};
export { parseManagedMemberEvidence } from "./managedMemberComparisonMatch.js";
@@ -0,0 +1,151 @@
import type {
ManagedMemberComparisonResult,
ManagedMemberComparisonSide,
} from "./managedMemberComparison.js";
import type { ManagedMemberInspection } from "./managedArtifact.js";
export interface PageAssessment {
readonly sourceComplete: boolean;
readonly sourceOmittedCount: number;
}
interface ComparisonCoverageInput {
readonly left: ManagedMemberInspection;
readonly right: ManagedMemberInspection;
readonly leftMethodPage: PageAssessment;
readonly rightMethodPage: PageAssessment;
readonly leftFieldPage: PageAssessment;
readonly rightFieldPage: PageAssessment;
readonly omittedMethodItems: number;
readonly omittedFieldItems: number;
readonly omittedCandidates: number;
}
export const buildComparisonCoverage = ({
left,
right,
leftMethodPage,
rightMethodPage,
leftFieldPage,
rightFieldPage,
omittedMethodItems,
omittedFieldItems,
omittedCandidates,
}: ComparisonCoverageInput): ManagedMemberComparisonResult["coverage"] => {
const omittedMethods =
omittedMethodItems +
leftMethodPage.sourceOmittedCount +
rightMethodPage.sourceOmittedCount;
const omittedFields =
omittedFieldItems +
leftFieldPage.sourceOmittedCount +
rightFieldPage.sourceOmittedCount;
const leftStatus = sideCoverageStatus(
left,
leftMethodPage.sourceComplete && leftFieldPage.sourceComplete,
);
const rightStatus = sideCoverageStatus(
right,
rightMethodPage.sourceComplete && rightFieldPage.sourceComplete,
);
const unknownInput =
left.coverage.state !== "complete" || right.coverage.state !== "complete";
const truncated = omittedMethods + omittedFields + omittedCandidates > 0;
return {
status: unknownInput
? "partial"
: truncated
? "truncated"
: leftStatus === "complete" && rightStatus === "complete"
? "complete-within-inputs"
: "partial",
left_status: leftStatus,
right_status: rightStatus,
omitted_methods: omittedMethods,
omitted_fields: omittedFields,
omitted_candidates: omittedCandidates,
};
};
const sideCoverageStatus = (
result: ManagedMemberInspection,
pagesComplete: boolean,
): ManagedMemberComparisonResult["coverage"]["left_status"] =>
result.coverage.state === "unavailable"
? "unavailable"
: result.coverage.state === "complete" && pagesComplete
? "complete"
: "partial";
export const sideManifest = (
side: ManagedMemberComparisonSide,
): ManagedMemberComparisonResult["left"] => ({
evidence_id: side.evidenceId,
artifact_sha256: side.result.artifact.sha256,
mvid: side.result.module?.mvid ?? null,
module_name: side.result.module?.name ?? null,
metadata_status: side.result.metadata.status,
methods_total: side.result.methods.total,
fields_total: side.result.fields.total,
});
export const buildComparisonSummary = (
items: readonly (
| ManagedMemberComparisonResult["methods"][number]
| ManagedMemberComparisonResult["fields"][number]
)[],
): ManagedMemberComparisonResult["summary"] => ({
unchanged: items.filter(({ status }) => status === "unchanged").length,
changed: items.filter(({ status }) => status === "changed").length,
added: items.filter(({ status }) => status === "added").length,
removed: items.filter(({ status }) => status === "removed").length,
unknown: items.filter(({ status }) => status === "unknown").length,
});
export const buildComparisonMatching = (
methodItems: readonly ManagedMemberComparisonResult["methods"][number][],
fieldItems: readonly ManagedMemberComparisonResult["fields"][number][],
): ManagedMemberComparisonResult["matching"] => ({
exact_il_signature: methodItems.filter(
({ match }) => match.basis === "exact-il-signature",
).length,
structural_method_shape: methodItems.filter(
({ match }) => match.basis === "structural-method-shape",
).length,
field_signature: fieldItems.filter(
({ match }) => match.basis === "field-signature",
).length,
ambiguous:
methodItems.filter(({ match }) => match.status === "ambiguous").length +
fieldItems.filter(({ match }) => match.status === "ambiguous").length,
unmatched: [...methodItems, ...fieldItems].filter(
({ match }) => match.status === "unmatched",
).length,
});
export const comparisonLimitations = (
left: ManagedMemberInspection,
right: ManagedMemberInspection,
omittedItems: number,
omittedCandidates: number,
): string[] => {
const limitations: string[] = [
"Metadata tokens are build-local coordinates; matched pairs are remaps, not persistent identities.",
"Names are reported as observations but are not used as a matching basis.",
];
if (!left.methods.complete || !right.methods.complete)
limitations.push("At least one method page is incomplete.");
if (!left.fields.complete || !right.fields.complete)
limitations.push("At least one field page is incomplete.");
if (left.coverage.state !== "complete" || right.coverage.state !== "complete")
limitations.push("At least one managed member observation is partial.");
if (omittedItems > 0)
limitations.push(
`${String(omittedItems)} comparison items were omitted by output limits.`,
);
if (omittedCandidates > 0)
limitations.push(
`${String(omittedCandidates)} ambiguous candidates were omitted by candidate limits.`,
);
return limitations;
};
+513
View File
@@ -0,0 +1,513 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { parseEvidence } from "./evidence.js";
import {
managedMemberInspectionSchema,
type ManagedMemberInspection,
} from "./managedArtifact.js";
import type {
CompareManagedMembersInput,
ManagedMemberComparisonResult,
ManagedMemberComparisonSide,
} from "./managedMemberComparison.js";
import type { JsonValue } from "./jsonValue.js";
export const sha256 = (value: JsonValue): string => {
const serialized = canonicalize(value);
if (serialized === undefined)
throw new TypeError("Managed comparison canonicalization failed");
return createHash("sha256").update(serialized).digest("hex");
};
type Method = ManagedMemberInspection["methods"]["items"][number];
type Field = ManagedMemberInspection["fields"]["items"][number];
type MethodItem = ManagedMemberComparisonResult["methods"][number];
type FieldItem = ManagedMemberComparisonResult["fields"][number];
type MatchBasis = MethodItem["match"]["basis"];
interface Keyed<Item> {
readonly item: Item;
readonly exactKey: string | null;
readonly structuralKey: string | null;
}
interface MatchedPair<Item> {
readonly left: Keyed<Item>;
readonly right: Keyed<Item>;
readonly basis: MatchBasis;
readonly confidence: "exact" | "high";
}
interface Ambiguous<Item> {
readonly left: readonly Keyed<Item>[];
readonly right: readonly Keyed<Item>[];
readonly basis: MatchBasis;
}
export interface ComparisonItemContext {
readonly leftEvidenceId: string;
readonly rightEvidenceId: string;
readonly leftComplete: boolean;
readonly rightComplete: boolean;
readonly limits: CompareManagedMembersInput["limits"];
}
const keyMethod = (item: Method): Keyed<Method> => ({
item,
exactKey:
item.signature.parse_status === "decoded" &&
item.body.status === "present" &&
item.body.normalized_il_sha256 !== null
? stableKey([
"method-exact",
item.signature.raw_sha256,
item.body.normalized_il_sha256,
])
: null,
structuralKey:
item.body.status === "present"
? stableKey([
"method-structural",
item.signature.kind,
item.signature.calling_convention,
item.signature.generic_parameter_count,
item.signature.parameter_count,
item.signature.return_type,
item.signature.parameter_types,
item.body.status,
item.body.header_format,
item.body.il_size,
item.body.max_stack,
item.body.init_locals,
item.body.opcode_counts,
item.body.anchors.map(({ opcode, operand_kind }) => [
opcode,
operand_kind,
]),
item.body.exception_regions.map((region) => [
region.flags,
region.try_length,
region.handler_length,
region.class_token === null ? null : "type-token",
region.filter_offset === null ? null : "filter",
]),
])
: null,
});
const keyField = (item: Field): Keyed<Field> => ({
item,
exactKey:
item.signature.parse_status === "decoded"
? stableKey(["field-exact", item.signature.raw_sha256])
: null,
structuralKey: stableKey([
"field-structural",
item.signature.kind,
item.signature.field_type,
item.flags,
]),
});
const stableKey = (value: JsonValue): string => sha256(value);
const matchMethods = (
left: readonly Keyed<Method>[],
right: readonly Keyed<Method>[],
maxCandidates: number,
) =>
matchByKeys({
left,
right,
maxCandidates,
exactBasis: "exact-il-signature",
fallbackBases: ["structural-method-shape"],
});
const matchFields = (
left: readonly Keyed<Field>[],
right: readonly Keyed<Field>[],
maxCandidates: number,
) =>
matchByKeys({
left,
right,
maxCandidates,
exactBasis: "field-signature",
fallbackBases: [],
});
interface MatchByKeysInput<Item> {
readonly left: readonly Keyed<Item>[];
readonly right: readonly Keyed<Item>[];
readonly maxCandidates: number;
readonly exactBasis: MatchBasis;
readonly fallbackBases: readonly MatchBasis[];
}
const matchByKeys = <Item>({
left,
right,
maxCandidates,
exactBasis,
fallbackBases,
}: MatchByKeysInput<Item>): {
readonly pairs: readonly MatchedPair<Item>[];
readonly ambiguous: readonly Ambiguous<Item>[];
readonly leftOnly: readonly Keyed<Item>[];
readonly rightOnly: readonly Keyed<Item>[];
readonly omittedCandidates: number;
} => {
const usedLeft = new Set<Keyed<Item>>();
const usedRight = new Set<Keyed<Item>>();
const pairs: MatchedPair<Item>[] = [];
const ambiguous: Ambiguous<Item>[] = [];
let omittedCandidates = 0;
const rounds: readonly {
readonly basis: MatchBasis;
readonly key: (item: Keyed<Item>) => string | null;
}[] = [
{ basis: exactBasis, key: ({ exactKey }) => exactKey },
...fallbackBases.map((basis) => ({
basis,
key: ({ structuralKey }: Keyed<Item>) => structuralKey,
})),
];
for (const round of rounds) {
const leftGroups = groupBy(
left.filter((item) => !usedLeft.has(item)),
round.key,
);
const rightGroups = groupBy(
right.filter((item) => !usedRight.has(item)),
round.key,
);
for (const [key, leftItems] of leftGroups) {
const rightItems = rightGroups.get(key);
if (rightItems === undefined) continue;
if (leftItems.length === 1 && rightItems.length === 1) {
const [leftItem] = leftItems;
const [rightItem] = rightItems;
if (leftItem === undefined || rightItem === undefined) continue;
usedLeft.add(leftItem);
usedRight.add(rightItem);
pairs.push({
left: leftItem,
right: rightItem,
basis: round.basis,
confidence:
round.basis === "exact-il-signature" ||
round.basis === "field-signature"
? "exact"
: "high",
});
} else {
for (const item of leftItems) usedLeft.add(item);
for (const item of rightItems) usedRight.add(item);
omittedCandidates += Math.max(
0,
leftItems.length + rightItems.length - maxCandidates * 2,
);
ambiguous.push({
left: leftItems.slice(0, maxCandidates),
right: rightItems.slice(0, maxCandidates),
basis: round.basis,
});
}
}
}
return {
pairs,
ambiguous,
leftOnly: left.filter((item) => !usedLeft.has(item)),
rightOnly: right.filter((item) => !usedRight.has(item)),
omittedCandidates,
};
};
const groupBy = <Item>(
items: readonly Item[],
keyOf: (item: Item) => string | null,
): Map<string, readonly Item[]> => {
const grouped = new Map<string, Item[]>();
for (const item of items) {
const key = keyOf(item);
if (key === null) continue;
const bucket = grouped.get(key) ?? [];
bucket.push(item);
grouped.set(key, bucket);
}
return grouped;
};
const changedMethodDimensions = (
left: Method,
right: Method,
): MethodItem["dimensions"] => {
const dimensions: MethodItem["dimensions"] = [];
if (left.signature.raw_sha256 !== right.signature.raw_sha256)
dimensions.push("signature");
if (left.body.normalized_il_sha256 !== right.body.normalized_il_sha256)
dimensions.push("cil");
if (
canonicalize(left.body.opcode_counts) !==
canonicalize(right.body.opcode_counts)
)
dimensions.push("opcode-shape");
if (callShape(left.body.anchors) !== callShape(right.body.anchors))
dimensions.push("call-shape");
if (fieldShape(left.body.anchors) !== fieldShape(right.body.anchors))
dimensions.push("field-shape");
if (
canonicalize(left.body.exception_regions) !==
canonicalize(right.body.exception_regions)
)
dimensions.push("exception-shape");
if (left.body.status !== right.body.status) dimensions.push("availability");
return dimensions;
};
const callShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
.filter(({ operand_kind }) => operand_kind === "method")
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
const fieldShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
.filter(({ operand_kind }) => operand_kind === "field")
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
const methodIdentity = (item: Method): NonNullable<MethodItem["left"]> => ({
token: item.token,
declaring_type: item.declaring_type,
name: item.name,
signature_sha256: item.signature.raw_sha256,
normalized_il_sha256: item.body.normalized_il_sha256,
});
const fieldIdentity = (item: Field): NonNullable<FieldItem["left"]> => ({
token: item.token,
declaring_type: item.declaring_type,
name: item.name,
signature_sha256: item.signature.raw_sha256,
});
const methodOnlyItem = (
item: Method,
context: ComparisonItemContext,
left: boolean,
): MethodItem => {
const absenceObserved = left ? context.rightComplete : context.leftComplete;
return {
item_id: `mmc_method_${sha256({ token: item.token, side: left ? "left" : "right" })}`,
status: absenceObserved ? (left ? "removed" : "added") : "unknown",
left: left ? methodIdentity(item) : null,
right: left ? null : methodIdentity(item),
match: {
status: "unmatched",
basis: "none",
confidence: "unknown",
candidate_left_tokens: [],
candidate_right_tokens: [],
},
dimensions: ["availability"],
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: absenceObserved
? []
: [
`unknown-within-unobserved-page: The ${left ? "right" : "left"} method page is incomplete, so absence was not observed.`,
],
};
};
const fieldOnlyItem = (
item: Field,
context: ComparisonItemContext,
left: boolean,
): FieldItem => {
const absenceObserved = left ? context.rightComplete : context.leftComplete;
return {
item_id: `mmc_field_${sha256({ token: item.token, side: left ? "left" : "right" })}`,
status: absenceObserved ? (left ? "removed" : "added") : "unknown",
left: left ? fieldIdentity(item) : null,
right: left ? null : fieldIdentity(item),
match: {
status: "unmatched",
basis: "none",
confidence: "unknown",
candidate_left_tokens: [],
candidate_right_tokens: [],
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: absenceObserved
? []
: [
`unknown-within-unobserved-page: The ${left ? "right" : "left"} field page is incomplete, so absence was not observed.`,
],
};
};
export const buildMethodItems = (
matches: ReturnType<typeof matchMethods>,
context: ComparisonItemContext,
): { readonly items: MethodItem[]; readonly omitted: number } => {
const items: MethodItem[] = [];
for (const pair of matches.pairs) {
const dimensions = changedMethodDimensions(pair.left.item, pair.right.item);
items.push({
item_id: `mmc_method_${sha256({
left: pair.left.item.token,
right: pair.right.item.token,
basis: pair.basis,
})}`,
status: dimensions.length === 0 ? "unchanged" : "changed",
left: methodIdentity(pair.left.item),
right: methodIdentity(pair.right.item),
match: {
status: "matched",
basis: pair.basis,
confidence: pair.confidence,
candidate_left_tokens: [],
candidate_right_tokens: [],
},
dimensions,
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [],
});
}
for (const ambiguous of matches.ambiguous) {
items.push({
item_id: `mmc_method_${sha256({
left: ambiguous.left.map(({ item }) => item.token),
right: ambiguous.right.map(({ item }) => item.token),
basis: ambiguous.basis,
})}`,
status: "unknown",
left: null,
right: null,
match: {
status: "ambiguous",
basis: ambiguous.basis,
confidence: "unknown",
candidate_left_tokens: ambiguous.left.map(({ item }) => item.token),
candidate_right_tokens: ambiguous.right.map(({ item }) => item.token),
},
dimensions: ["availability"],
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [
"Multiple managed methods share the same non-name identity key; REA did not guess a token remap.",
],
});
}
for (const item of matches.leftOnly)
items.push(methodOnlyItem(item.item, context, true));
for (const item of matches.rightOnly)
items.push(methodOnlyItem(item.item, context, false));
return limitItems(items, context.limits.max_method_matches);
};
export const buildFieldItems = (
matches: ReturnType<typeof matchFields>,
context: ComparisonItemContext,
): { readonly items: FieldItem[]; readonly omitted: number } => {
const items: FieldItem[] = [];
for (const pair of matches.pairs) {
const changed =
pair.left.item.signature.raw_sha256 !==
pair.right.item.signature.raw_sha256 ||
pair.left.item.flags !== pair.right.item.flags;
items.push({
item_id: `mmc_field_${sha256({
left: pair.left.item.token,
right: pair.right.item.token,
})}`,
status: changed ? "changed" : "unchanged",
left: fieldIdentity(pair.left.item),
right: fieldIdentity(pair.right.item),
match: {
status: "matched",
basis: pair.basis,
confidence: pair.confidence,
candidate_left_tokens: [],
candidate_right_tokens: [],
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [],
});
}
for (const ambiguous of matches.ambiguous)
items.push({
item_id: `mmc_field_${sha256({
left: ambiguous.left.map(({ item }) => item.token),
right: ambiguous.right.map(({ item }) => item.token),
})}`,
status: "unknown",
left: null,
right: null,
match: {
status: "ambiguous",
basis: ambiguous.basis,
confidence: "unknown",
candidate_left_tokens: ambiguous.left.map(({ item }) => item.token),
candidate_right_tokens: ambiguous.right.map(({ item }) => item.token),
},
evidence_links: [context.leftEvidenceId, context.rightEvidenceId],
limitations: [
"Multiple managed fields share the same signature; REA did not guess a token remap from names.",
],
});
for (const item of matches.leftOnly)
items.push(fieldOnlyItem(item.item, context, true));
for (const item of matches.rightOnly)
items.push(fieldOnlyItem(item.item, context, false));
return limitItems(items, context.limits.max_field_matches);
};
const limitItems = <Item>(
items: readonly Item[],
limit: number,
): { readonly items: Item[]; readonly omitted: number } => ({
items: items.slice(0, limit),
omitted: Math.max(0, items.length - limit),
});
export const keyMembers = (
left: ManagedMemberComparisonSide,
right: ManagedMemberComparisonSide,
maxCandidates: number,
): {
readonly methodMatches: ReturnType<typeof matchMethods>;
readonly fieldMatches: ReturnType<typeof matchFields>;
} => ({
methodMatches: matchMethods(
left.result.methods.items.map(keyMethod),
right.result.methods.items.map(keyMethod),
maxCandidates,
),
fieldMatches: matchFields(
left.result.fields.items.map(keyField),
right.result.fields.items.map(keyField),
maxCandidates,
),
});
export const parseManagedMemberEvidence = (
evidence: unknown,
): {
readonly evidenceId: string;
readonly result: ManagedMemberInspection;
} => {
const parsed = parseEvidence(evidence);
if (parsed.operation !== "inspect_managed_members")
throw new TypeError("Evidence operation is not inspect_managed_members");
return {
evidenceId: parsed.evidence_id,
result: managedMemberInspectionSchema.parse(parsed.normalized_result),
};
};
+41 -488
View File
@@ -1,181 +1,30 @@
import { createHash } from "node:crypto";
import { basename } from "node:path";
import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, parseEvidence } from "./evidence.js";
import { functionDossierSchema } from "./hopperValues.js";
import { parseEvidence } from "./evidence.js";
import {
cliMetadataGuidSchema,
managedNativeBoundaryInspectionSchema,
type ManagedNativeBoundaryInspection,
} from "./managedArtifact.js";
import { inspectMachoSchema } from "./nativeInspection.js";
import type { JsonValue } from "./jsonValue.js";
import {
managedNativeVerificationResultSchema,
type ManagedNativeVerificationInput,
type ManagedNativeVerificationResult,
} from "./managedNativeVerificationSchemas.js";
import {
buildVerificationResult,
collectNativeSymbols,
verifyPinvoke,
} from "./managedNativeVerificationMatch.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
const tokenSchema = z.string().regex(/^0x[0-9a-f]{8}$/u);
const boundedTextSchema = z.string().min(1).max(4_096);
const nativeVerificationLimitsSchema = z.strictObject({
max_native_observations: z.number().int().min(1).max(50).default(20),
max_candidates_per_import: z.number().int().min(1).max(100).default(25),
});
/** Authenticated managed boundary Evidence plus native observations. */
export const managedNativeVerificationInputSchema = z
.strictObject({
managed_boundaries: evidenceSchema,
native_observations: z.array(evidenceSchema).min(1).max(50),
limits: nativeVerificationLimitsSchema.default({
max_native_observations: 20,
max_candidates_per_import: 25,
}),
unknown_registry_approved: z.literal(true).optional(),
})
.superRefine((input, context) => {
const ids = new Set<string>();
for (const [index, evidence] of input.native_observations.entries()) {
if (evidence.evidence_id === input.managed_boundaries.evidence_id) {
context.addIssue({
code: "custom",
path: ["native_observations", index],
message:
"Native observation Evidence must be distinct from managed boundary Evidence",
});
}
if (ids.has(evidence.evidence_id)) {
context.addIssue({
code: "custom",
path: ["native_observations", index],
message: "Native observation Evidence IDs must be unique",
});
}
ids.add(evidence.evidence_id);
}
});
const nativeSymbolSchema = z.strictObject({
evidence_id: evidenceIdSchema,
operation: z.string().min(1),
name: z.string().min(1),
address: z.string().nullable(),
module_name: z.string().nullable(),
module_path: z.string().nullable(),
source: z.enum(["macho-export", "function-dossier"]),
});
const matchStatusSchema = z.enum([
"verified",
"inferred",
"unresolved",
"contradicted",
]);
const matchBasisSchema = z.enum([
"exact-export-name",
"exact-function-name",
"decorated-name-candidate",
"module-mismatch",
"no-native-candidate",
"unsupported-native-evidence",
]);
const pinvokeVerificationSchema = z.strictObject({
item_id: z.string().regex(/^mnv_pinvoke_[a-f0-9]{64}$/u),
managed: z.strictObject({
token: tokenSchema,
member_token: tokenSchema.nullable(),
member_name: z.string().nullable(),
import_name: z.string().min(1),
import_scope_name: z.string().nullable(),
no_mangle: z.boolean(),
char_set: z.enum(["not-specified", "ansi", "unicode", "auto", "unknown"]),
call_convention: z.enum([
"not-specified",
"winapi",
"cdecl",
"stdcall",
"thiscall",
"fastcall",
"unknown",
]),
declaration_verification: z.literal("managed-declaration-only"),
}),
status: matchStatusSchema,
basis: matchBasisSchema,
confidence: z.enum(["observed", "inferred", "unknown"]),
matched_native: nativeSymbolSchema.nullable(),
candidates: z.array(nativeSymbolSchema).max(100),
evidence_links: z.array(evidenceIdSchema).min(1).max(51),
limitations: z.array(boundedTextSchema).max(100),
});
/** Provider-neutral managed/native verification result. */
export const managedNativeVerificationResultSchema = z.strictObject({
schema_version: z.literal(1),
verification_id: z.string().regex(/^mnv_[a-f0-9]{64}$/u),
algorithm: z.strictObject({
name: z.literal("rea-managed-native-verification"),
version: z.literal(1),
token_identity: z.literal("build-local"),
token_to_address_mapping: z.literal("not-inferred"),
}),
managed_boundary: z.strictObject({
evidence_id: evidenceIdSchema,
artifact_sha256: digestSchema,
artifact_path: z.string().min(1),
mvid: cliMetadataGuidSchema.nullable(),
metadata_status: z.enum(["absent", "complete", "partial", "malformed"]),
pinvoke_imports_total: z.number().int().min(0),
native_implementations_total: z.number().int().min(0),
coverage_state: z.enum(["complete", "partial", "unavailable"]),
}),
native_observations: z.strictObject({
total: z.number().int().min(1),
accepted: z.number().int().min(0),
unsupported: z.number().int().min(0),
symbols: z.number().int().min(0),
truncated: z.boolean(),
}),
summary: z.strictObject({
verified: z.number().int().min(0),
inferred: z.number().int().min(0),
unresolved: z.number().int().min(0),
contradicted: z.number().int().min(0),
native_body_unresolved: z.number().int().min(0),
}),
pinvoke_imports: z.array(pinvokeVerificationSchema).max(50_000),
native_implementations: z.strictObject({
unresolved: z.number().int().min(0),
reason: boundedTextSchema,
}),
coverage: z.strictObject({
status: z.enum(["complete-within-inputs", "partial", "truncated"]),
omitted_native_observations: z.number().int().min(0),
omitted_candidates: z.number().int().min(0),
}),
evidence_links: z.array(evidenceIdSchema).min(2).max(51),
limitations: z.array(boundedTextSchema).max(1_000),
});
export type ManagedNativeVerificationInput = z.infer<
typeof managedNativeVerificationInputSchema
>;
export type ManagedNativeVerificationResult = z.infer<
typeof managedNativeVerificationResultSchema
>;
type NativeSymbol = z.infer<typeof nativeSymbolSchema>;
type PinvokeImport =
ManagedNativeBoundaryInspection["pinvoke_imports"]["items"][number];
type PinvokeVerification = z.infer<typeof pinvokeVerificationSchema>;
type VerifiedPinvoke = {
readonly verification: PinvokeVerification;
readonly omittedCandidates: number;
};
export {
managedNativeVerificationInputSchema,
type ManagedNativeVerificationInput,
type ManagedNativeVerificationResult,
} from "./managedNativeVerificationSchemas.js";
export { managedNativeVerificationResultSchema };
const sha256 = (value: JsonValue): string => {
const serialized = canonicalize(value);
@@ -184,346 +33,50 @@ const sha256 = (value: JsonValue): string => {
return createHash("sha256").update(serialized).digest("hex");
};
/** Verify managed P/Invoke declarations against authenticated native Evidence. */
export const verifyManagedNativeBoundaries = (
const managedBoundaryFromInput = (
input: ManagedNativeVerificationInput,
): ManagedNativeVerificationResult => {
): ManagedNativeBoundaryInspection => {
const managedEvidence = parseEvidence(input.managed_boundaries);
if (managedEvidence.operation !== "inspect_managed_native_boundaries")
throw new TypeError(
"Evidence operation is not inspect_managed_native_boundaries",
);
const managed = managedNativeBoundaryInspectionSchema.parse(
return managedNativeBoundaryInspectionSchema.parse(
managedEvidence.normalized_result,
);
};
/** Verify managed P/Invoke declarations against authenticated native Evidence. */
export const verifyManagedNativeBoundaries = (
input: ManagedNativeVerificationInput,
): ManagedNativeVerificationResult => {
const managed = managedBoundaryFromInput(input);
const managedEvidence = parseEvidence(input.managed_boundaries);
const native = collectNativeSymbols(
input.native_observations.slice(0, input.limits.max_native_observations),
);
const verifiedPinvokes = managed.pinvoke_imports.items.map((item) =>
verifyPinvoke(
verifyPinvoke({
item,
managedEvidence.evidence_id,
native.symbols,
native.accepted > 0,
managedEvidenceId: managedEvidence.evidence_id,
symbols: native.symbols,
hasSupportedNativeEvidence: native.accepted > 0,
input,
),
}),
);
const pinvokeImports = verifiedPinvokes.map(
({ verification }) => verification,
);
const counts = countStatuses(pinvokeImports);
const omittedNativeObservations = Math.max(
0,
input.native_observations.length - input.limits.max_native_observations,
);
const omittedCandidates = verifiedPinvokes.reduce(
(sum, item) => sum + item.omittedCandidates,
0,
);
const coverageStatus =
omittedNativeObservations > 0 || omittedCandidates > 0
? "truncated"
: managed.coverage.state === "complete" && native.unsupported === 0
? "complete-within-inputs"
: "partial";
const withoutId = {
schema_version: 1 as const,
algorithm: {
name: "rea-managed-native-verification" as const,
version: 1 as const,
token_identity: "build-local" as const,
token_to_address_mapping: "not-inferred" as const,
},
managed_boundary: {
evidence_id: managedEvidence.evidence_id,
artifact_sha256: managed.artifact.sha256,
artifact_path: managed.artifact.path,
mvid: managed.module?.mvid ?? null,
metadata_status: managed.metadata.status,
pinvoke_imports_total: managed.pinvoke_imports.total,
native_implementations_total: managed.native_implementations.total,
coverage_state: managed.coverage.state,
},
native_observations: {
total: input.native_observations.length,
accepted: native.accepted,
unsupported: native.unsupported,
symbols: native.symbols.length,
truncated: omittedNativeObservations > 0,
},
summary: {
...counts,
native_body_unresolved: managed.native_implementations.items.filter(
({ boundary_kind: kind }) => kind !== "pinvoke",
).length,
},
pinvoke_imports: pinvokeImports,
native_implementations: {
unresolved: managed.native_implementations.items.filter(
({ boundary_kind: kind }) => kind !== "pinvoke",
).length,
reason:
"Managed metadata tokens and RVAs are not translated to native provider addresses by this workflow; C++/CLI, ReadyToRun, and native-body mappings require explicit provider-supported bridge evidence.",
},
coverage: {
status: coverageStatus,
omitted_native_observations: omittedNativeObservations,
omitted_candidates: omittedCandidates,
},
evidence_links: [
managedEvidence.evidence_id,
...input.native_observations
.slice(0, input.limits.max_native_observations)
.map(({ evidence_id: id }) => id),
],
limitations: [
"P/Invoke verification checks declared import names against supplied native export or function-name Evidence only.",
"A matching native symbol verifies that a candidate export/function was observed; it does not prove CLR binding, marshaling behavior, call reachability, or runtime loading.",
"Missing supplied native symbols are reported as unresolved, not proof that a dependency cannot exist.",
"Managed metadata tokens and method RVAs are not interpreted as native addresses.",
...(managed.coverage.state === "complete"
? []
: ["Managed boundary input is partial or unavailable."]),
...(native.unsupported === 0
? []
: [
"Some native Evidence operations were unsupported by this workflow.",
]),
],
};
const withoutId = buildVerificationResult({
managedEvidence,
managed,
native,
pinvokeImports,
verifiedPinvokes,
input,
});
return managedNativeVerificationResultSchema.parse({
...withoutId,
verification_id: `mnv_${sha256(withoutId)}`,
});
};
const collectNativeSymbols = (
observations: readonly ManagedNativeVerificationInput["native_observations"][number][],
): {
readonly symbols: readonly NativeSymbol[];
readonly accepted: number;
readonly unsupported: number;
} => {
const symbols: NativeSymbol[] = [];
let accepted = 0;
let unsupported = 0;
for (const raw of observations) {
const evidence = parseEvidence(raw);
const extracted = symbolsForEvidence(evidence);
if (!extracted.supported) {
unsupported += 1;
} else {
accepted += 1;
symbols.push(...extracted.symbols);
}
}
return { symbols, accepted, unsupported };
};
const symbolsForEvidence = (
evidence: ReturnType<typeof parseEvidence>,
): {
readonly supported: boolean;
readonly symbols: readonly NativeSymbol[];
} => {
if (evidence.operation === "inspect_macho") {
const result = inspectMachoSchema.parse(evidence.normalized_result);
return {
supported: true,
symbols: result.exports.items.map((symbol) => ({
evidence_id: evidence.evidence_id,
operation: evidence.operation,
name: symbol.name,
address: symbol.address,
module_name: moduleName(evidence),
module_path: modulePath(evidence),
source: "macho-export" as const,
})),
};
}
if (evidence.operation === "analyze_function") {
const dossier = functionDossierSchema.parse(evidence.normalized_result);
return {
supported: true,
symbols: [
{
evidence_id: evidence.evidence_id,
operation: evidence.operation,
name: dossier.procedure.name,
address: dossier.procedure.address,
module_name: moduleName(evidence),
module_path: modulePath(evidence),
source: "function-dossier" as const,
},
],
};
}
return { supported: false, symbols: [] };
};
const verifyPinvoke = (
item: PinvokeImport,
managedEvidenceId: string,
symbols: readonly NativeSymbol[],
hasSupportedNativeEvidence: boolean,
input: ManagedNativeVerificationInput,
): VerifiedPinvoke => {
const managed = {
token: item.token,
member_token: item.member_token,
member_name: item.member_name,
import_name: item.import_name,
import_scope_name: item.import_scope_name,
no_mangle: item.no_mangle,
char_set: item.char_set,
call_convention: item.call_convention,
declaration_verification: item.verification,
};
const names = candidateNames(managed);
const allCandidates = symbols.filter((symbol) =>
names.some((name) => sameSymbolName(name, symbol.name)),
);
const candidates = allCandidates.slice(
0,
input.limits.max_candidates_per_import,
);
const exact = candidates.find(
(symbol) =>
sameSymbolName(item.import_name, symbol.name) &&
moduleCompatible(item.import_scope_name, symbol),
);
const decorated = candidates.find((symbol) =>
moduleCompatible(item.import_scope_name, symbol),
);
const selected = exact ?? decorated ?? candidates[0] ?? null;
const basis =
selected === null
? hasSupportedNativeEvidence
? "no-native-candidate"
: "unsupported-native-evidence"
: !moduleCompatible(item.import_scope_name, selected)
? "module-mismatch"
: exact !== undefined
? selected.source === "macho-export"
? "exact-export-name"
: "exact-function-name"
: "decorated-name-candidate";
const status =
selected === null
? "unresolved"
: basis === "module-mismatch"
? "contradicted"
: basis === "decorated-name-candidate"
? "inferred"
: "verified";
const limitations = [
...(item.import_scope_name === null
? ["Managed declaration does not name an import scope/module."]
: []),
...(status === "inferred"
? [
"Native symbol matched a decorated candidate rather than the exact declared import name.",
]
: []),
...(status === "contradicted"
? [
"A symbol name candidate exists, but the supplied native Evidence module identity does not match the managed import scope.",
]
: []),
...(status === "unresolved"
? [
"No supplied native export/function Evidence matched this declared import.",
]
: []),
];
return {
verification: pinvokeVerificationSchema.parse({
item_id: `mnv_pinvoke_${sha256({
managedEvidenceId,
token: item.token,
importName: item.import_name,
importScope: item.import_scope_name,
candidates,
})}`,
managed,
status,
basis,
confidence:
status === "verified"
? "observed"
: status === "inferred"
? "inferred"
: "unknown",
matched_native: selected,
candidates,
evidence_links: [
managedEvidenceId,
...new Set(candidates.map(({ evidence_id: id }) => id)),
],
limitations,
}),
omittedCandidates: Math.max(0, allCandidates.length - candidates.length),
};
};
const candidateNames = (
managed: Pick<
PinvokeImport,
"import_name" | "char_set" | "call_convention" | "no_mangle"
>,
): readonly string[] => {
const names = new Set<string>([managed.import_name]);
if (!managed.no_mangle) {
if (managed.char_set === "unicode") names.add(`${managed.import_name}W`);
if (managed.char_set === "ansi") names.add(`${managed.import_name}A`);
if (managed.call_convention === "stdcall") {
names.add(`_${managed.import_name}`);
names.add(`_${managed.import_name}@0`);
}
}
return [...names];
};
const sameSymbolName = (left: string, right: string): boolean =>
normalizeSymbol(left) === normalizeSymbol(right);
const normalizeSymbol = (value: string): string =>
value.replace(/^_+/u, "").replace(/@\d+$/u, "").toLowerCase();
const moduleCompatible = (
scope: string | null,
symbol: NativeSymbol,
): boolean => {
if (scope === null) return true;
const expected = normalizeModule(scope);
return [symbol.module_name, symbol.module_path]
.filter((value): value is string => value !== null)
.map(normalizeModule)
.some((value) => value === expected);
};
const normalizeModule = (value: string): string =>
basename(value)
.toLowerCase()
.replace(/^lib/u, "")
.replace(/\.(dll|dylib|so|node|exe)$/u, "");
const moduleName = (
evidence: ReturnType<typeof parseEvidence>,
): string | null => evidence.subject?.name ?? null;
const modulePath = (
evidence: ReturnType<typeof parseEvidence>,
): string | null => evidence.subject?.local_path ?? null;
const countStatuses = (
items: readonly z.infer<typeof pinvokeVerificationSchema>[],
): Pick<
ManagedNativeVerificationResult["summary"],
"verified" | "inferred" | "unresolved" | "contradicted"
> => ({
verified: items.filter(({ status }) => status === "verified").length,
inferred: items.filter(({ status }) => status === "inferred").length,
unresolved: items.filter(({ status }) => status === "unresolved").length,
contradicted: items.filter(({ status }) => status === "contradicted").length,
});
@@ -0,0 +1,423 @@
import { createHash } from "node:crypto";
import { basename } from "node:path";
import canonicalize from "canonicalize";
import { parseEvidence } from "./evidence.js";
import { functionDossierSchema } from "./hopperValues.js";
import { inspectMachoSchema } from "./nativeInspection.js";
import type { ManagedNativeBoundaryInspection } from "./managedArtifact.js";
import type { JsonValue } from "./jsonValue.js";
import {
pinvokeVerificationSchema,
type ManagedNativeVerificationInput,
type ManagedNativeVerificationResult,
type NativeSymbol,
type PinvokeVerification,
} from "./managedNativeVerificationSchemas.js";
const digest = (value: JsonValue): string => {
const serialized = canonicalize(value);
if (serialized === undefined)
throw new TypeError("Managed/native verification canonicalization failed");
return createHash("sha256").update(serialized).digest("hex");
};
type PinvokeImport =
ManagedNativeBoundaryInspection["pinvoke_imports"]["items"][number];
type VerifiedPinvoke = {
readonly verification: PinvokeVerification;
readonly omittedCandidates: number;
};
const moduleName = (
evidence: ReturnType<typeof parseEvidence>,
): string | null => evidence.subject?.name ?? null;
const modulePath = (
evidence: ReturnType<typeof parseEvidence>,
): string | null => evidence.subject?.local_path ?? null;
export const symbolsForEvidence = (
evidence: ReturnType<typeof parseEvidence>,
): {
readonly supported: boolean;
readonly symbols: readonly NativeSymbol[];
} => {
if (evidence.operation === "inspect_macho") {
const result = inspectMachoSchema.parse(evidence.normalized_result);
return {
supported: true,
symbols: result.exports.items.map((symbol) => ({
evidence_id: evidence.evidence_id,
operation: evidence.operation,
name: symbol.name,
address: symbol.address,
module_name: moduleName(evidence),
module_path: modulePath(evidence),
source: "macho-export" as const,
})),
};
}
if (evidence.operation === "analyze_function") {
const dossier = functionDossierSchema.parse(evidence.normalized_result);
return {
supported: true,
symbols: [
{
evidence_id: evidence.evidence_id,
operation: evidence.operation,
name: dossier.procedure.name,
address: dossier.procedure.address,
module_name: moduleName(evidence),
module_path: modulePath(evidence),
source: "function-dossier" as const,
},
],
};
}
return { supported: false, symbols: [] };
};
export const collectNativeSymbols = (
observations: readonly ManagedNativeVerificationInput["native_observations"][number][],
): {
readonly symbols: readonly NativeSymbol[];
readonly accepted: number;
readonly unsupported: number;
} => {
const symbols: NativeSymbol[] = [];
let accepted = 0;
let unsupported = 0;
for (const raw of observations) {
const evidence = parseEvidence(raw);
const extracted = symbolsForEvidence(evidence);
if (!extracted.supported) {
unsupported += 1;
} else {
accepted += 1;
symbols.push(...extracted.symbols);
}
}
return { symbols, accepted, unsupported };
};
const normalizeSymbol = (value: string): string =>
value.replace(/^_+/u, "").replace(/@\d+$/u, "").toLowerCase();
const sameSymbolName = (left: string, right: string): boolean =>
normalizeSymbol(left) === normalizeSymbol(right);
const normalizeModule = (value: string): string =>
basename(value)
.toLowerCase()
.replace(/^lib/u, "")
.replace(/\.(dll|dylib|so|node|exe)$/u, "");
const moduleCompatible = (
scope: string | null,
symbol: NativeSymbol,
): boolean => {
if (scope === null) return true;
const expected = normalizeModule(scope);
return [symbol.module_name, symbol.module_path]
.filter((value): value is string => value !== null)
.map(normalizeModule)
.some((value) => value === expected);
};
const candidateNames = (
managed: Pick<
PinvokeImport,
"import_name" | "char_set" | "call_convention" | "no_mangle"
>,
): readonly string[] => {
const names = new Set<string>([managed.import_name]);
if (!managed.no_mangle) {
if (managed.char_set === "unicode") names.add(`${managed.import_name}W`);
if (managed.char_set === "ansi") names.add(`${managed.import_name}A`);
if (managed.call_convention === "stdcall") {
names.add(`_${managed.import_name}`);
names.add(`_${managed.import_name}@0`);
}
}
return [...names];
};
interface CandidateSelection {
readonly allCandidates: readonly NativeSymbol[];
readonly candidates: readonly NativeSymbol[];
readonly selected: NativeSymbol | null;
readonly exact: NativeSymbol | undefined;
readonly decorated: NativeSymbol | undefined;
}
const selectCandidates = (
managed: PinvokeImport,
symbols: readonly NativeSymbol[],
maxCandidates: number,
): CandidateSelection => {
const names = candidateNames(managed);
const allCandidates = symbols.filter((symbol) =>
names.some((name) => sameSymbolName(name, symbol.name)),
);
const candidates = allCandidates.slice(0, maxCandidates);
const exact = candidates.find(
(symbol) =>
sameSymbolName(managed.import_name, symbol.name) &&
moduleCompatible(managed.import_scope_name, symbol),
);
const decorated = candidates.find((symbol) =>
moduleCompatible(managed.import_scope_name, symbol),
);
const selected = exact ?? decorated ?? candidates[0] ?? null;
return { allCandidates, candidates, selected, exact, decorated };
};
interface BasisAndStatus {
readonly basis: PinvokeVerification["basis"];
readonly status: PinvokeVerification["status"];
readonly confidence: PinvokeVerification["confidence"];
}
const basisAndStatus = (
managed: PinvokeImport,
selection: CandidateSelection,
hasSupportedNativeEvidence: boolean,
): BasisAndStatus => {
const { selected, exact } = selection;
if (selected === null)
return {
basis: hasSupportedNativeEvidence
? "no-native-candidate"
: "unsupported-native-evidence",
status: "unresolved",
confidence: "unknown",
};
if (!moduleCompatible(managed.import_scope_name, selected))
return {
basis: "module-mismatch",
status: "contradicted",
confidence: "unknown",
};
if (exact !== undefined)
return {
basis:
selected.source === "macho-export"
? "exact-export-name"
: "exact-function-name",
status: "verified",
confidence: "observed",
};
return {
basis: "decorated-name-candidate",
status: "inferred",
confidence: "inferred",
};
};
const pinvokeLimitations = (
status: PinvokeVerification["status"],
managed: PinvokeImport,
): readonly string[] => [
...(managed.import_scope_name === null
? ["Managed declaration does not name an import scope/module."]
: []),
...(status === "inferred"
? [
"Native symbol matched a decorated candidate rather than the exact declared import name.",
]
: []),
...(status === "contradicted"
? [
"A symbol name candidate exists, but the supplied native Evidence module identity does not match the managed import scope.",
]
: []),
...(status === "unresolved"
? [
"No supplied native export/function Evidence matched this declared import.",
]
: []),
];
interface VerifyPinvokeContext {
readonly item: PinvokeImport;
readonly managedEvidenceId: string;
readonly symbols: readonly NativeSymbol[];
readonly hasSupportedNativeEvidence: boolean;
readonly input: ManagedNativeVerificationInput;
}
export const verifyPinvoke = ({
item,
managedEvidenceId,
symbols,
hasSupportedNativeEvidence,
input,
}: VerifyPinvokeContext): VerifiedPinvoke => {
const managed = {
token: item.token,
member_token: item.member_token,
member_name: item.member_name,
import_name: item.import_name,
import_scope_name: item.import_scope_name,
no_mangle: item.no_mangle,
char_set: item.char_set,
call_convention: item.call_convention,
declaration_verification: item.verification,
};
const selection = selectCandidates(
item,
symbols,
input.limits.max_candidates_per_import,
);
const { status, basis, confidence } = basisAndStatus(
item,
selection,
hasSupportedNativeEvidence,
);
const limitations = pinvokeLimitations(status, item);
return {
verification: pinvokeVerificationSchema.parse({
item_id: `mnv_pinvoke_${digest({
managedEvidenceId,
token: item.token,
importName: item.import_name,
importScope: item.import_scope_name,
candidates: [...selection.candidates],
})}`,
managed,
status,
basis,
confidence,
matched_native: selection.selected,
candidates: selection.candidates,
evidence_links: [
managedEvidenceId,
...new Set(selection.candidates.map(({ evidence_id: id }) => id)),
],
limitations,
}),
omittedCandidates: Math.max(
0,
selection.allCandidates.length - selection.candidates.length,
),
};
};
const countStatuses = (
items: readonly PinvokeVerification[],
): Pick<
ManagedNativeVerificationResult["summary"],
"verified" | "inferred" | "unresolved" | "contradicted"
> => ({
verified: items.filter(({ status }) => status === "verified").length,
inferred: items.filter(({ status }) => status === "inferred").length,
unresolved: items.filter(({ status }) => status === "unresolved").length,
contradicted: items.filter(({ status }) => status === "contradicted").length,
});
interface VerificationResultInput {
readonly managedEvidence: ReturnType<typeof parseEvidence>;
readonly managed: ManagedNativeBoundaryInspection;
readonly native: {
readonly symbols: readonly NativeSymbol[];
readonly accepted: number;
readonly unsupported: number;
};
readonly pinvokeImports: readonly PinvokeVerification[];
readonly verifiedPinvokes: readonly VerifiedPinvoke[];
readonly input: ManagedNativeVerificationInput;
}
export const buildVerificationResult = ({
managedEvidence,
managed,
native,
pinvokeImports,
verifiedPinvokes,
input,
}: VerificationResultInput): Omit<
ManagedNativeVerificationResult,
"verification_id"
> => {
const counts = countStatuses(pinvokeImports);
const omittedNativeObservations = Math.max(
0,
input.native_observations.length - input.limits.max_native_observations,
);
const omittedCandidates = verifiedPinvokes.reduce(
(sum, item) => sum + item.omittedCandidates,
0,
);
const nativeBodyUnresolved = managed.native_implementations.items.filter(
({ boundary_kind: kind }) => kind !== "pinvoke",
).length;
const coverageStatus =
omittedNativeObservations > 0 || omittedCandidates > 0
? "truncated"
: managed.coverage.state === "complete" && native.unsupported === 0
? "complete-within-inputs"
: "partial";
return {
schema_version: 1 as const,
algorithm: {
name: "rea-managed-native-verification" as const,
version: 1 as const,
token_identity: "build-local" as const,
token_to_address_mapping: "not-inferred" as const,
},
managed_boundary: {
evidence_id: managedEvidence.evidence_id,
artifact_sha256: managed.artifact.sha256,
artifact_path: managed.artifact.path,
mvid: managed.module?.mvid ?? null,
metadata_status: managed.metadata.status,
pinvoke_imports_total: managed.pinvoke_imports.total,
native_implementations_total: managed.native_implementations.total,
coverage_state: managed.coverage.state,
},
native_observations: {
total: input.native_observations.length,
accepted: native.accepted,
unsupported: native.unsupported,
symbols: native.symbols.length,
truncated: omittedNativeObservations > 0,
},
summary: {
...counts,
native_body_unresolved: nativeBodyUnresolved,
},
pinvoke_imports: [...pinvokeImports],
native_implementations: {
unresolved: nativeBodyUnresolved,
reason:
"Managed metadata tokens and RVAs are not translated to native provider addresses by this workflow; C++/CLI, ReadyToRun, and native-body mappings require explicit provider-supported bridge evidence.",
},
coverage: {
status: coverageStatus,
omitted_native_observations: omittedNativeObservations,
omitted_candidates: omittedCandidates,
},
evidence_links: [
managedEvidence.evidence_id,
...input.native_observations
.slice(0, input.limits.max_native_observations)
.map(({ evidence_id: id }) => id),
],
limitations: [
"P/Invoke verification checks declared import names against supplied native export or function-name Evidence only.",
"A matching native symbol verifies that a candidate export/function was observed; it does not prove CLR binding, marshaling behavior, call reachability, or runtime loading.",
"Missing supplied native symbols are reported as unresolved, not proof that a dependency cannot exist.",
"Managed metadata tokens and method RVAs are not interpreted as native addresses.",
...(managed.coverage.state === "complete"
? []
: ["Managed boundary input is partial or unavailable."]),
...(native.unsupported === 0
? []
: [
"Some native Evidence operations were unsupported by this workflow.",
]),
],
};
};
@@ -0,0 +1,163 @@
import { z } from "zod";
import { evidenceSchema } from "./evidence.js";
import { cliMetadataGuidSchema } from "./managedArtifact.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
const tokenSchema = z.string().regex(/^0x[0-9a-f]{8}$/u);
const boundedTextSchema = z.string().min(1).max(4_096);
const matchStatusSchema = z.enum([
"verified",
"inferred",
"unresolved",
"contradicted",
]);
const matchBasisSchema = z.enum([
"exact-export-name",
"exact-function-name",
"decorated-name-candidate",
"module-mismatch",
"no-native-candidate",
"unsupported-native-evidence",
]);
export const nativeVerificationLimitsSchema = z.strictObject({
max_native_observations: z.number().int().min(1).max(50).default(20),
max_candidates_per_import: z.number().int().min(1).max(100).default(25),
});
export const nativeSymbolSchema = z.strictObject({
evidence_id: evidenceIdSchema,
operation: z.string().min(1),
name: z.string().min(1),
address: z.string().nullable(),
module_name: z.string().nullable(),
module_path: z.string().nullable(),
source: z.enum(["macho-export", "function-dossier"]),
});
export type NativeSymbol = z.infer<typeof nativeSymbolSchema>;
export const pinvokeVerificationSchema = z.strictObject({
item_id: z.string().regex(/^mnv_pinvoke_[a-f0-9]{64}$/u),
managed: z.strictObject({
token: tokenSchema,
member_token: tokenSchema.nullable(),
member_name: z.string().nullable(),
import_name: z.string().min(1),
import_scope_name: z.string().nullable(),
no_mangle: z.boolean(),
char_set: z.enum(["not-specified", "ansi", "unicode", "auto", "unknown"]),
call_convention: z.enum([
"not-specified",
"winapi",
"cdecl",
"stdcall",
"thiscall",
"fastcall",
"unknown",
]),
declaration_verification: z.literal("managed-declaration-only"),
}),
status: matchStatusSchema,
basis: matchBasisSchema,
confidence: z.enum(["observed", "inferred", "unknown"]),
matched_native: nativeSymbolSchema.nullable(),
candidates: z.array(nativeSymbolSchema).max(100),
evidence_links: z.array(evidenceIdSchema).min(1).max(51),
limitations: z.array(boundedTextSchema).max(100),
});
export type PinvokeVerification = z.infer<typeof pinvokeVerificationSchema>;
/** Authenticated managed boundary Evidence plus native observations. */
export const managedNativeVerificationInputSchema = z
.strictObject({
managed_boundaries: evidenceSchema,
native_observations: z.array(evidenceSchema).min(1).max(50),
limits: nativeVerificationLimitsSchema.default({
max_native_observations: 20,
max_candidates_per_import: 25,
}),
unknown_registry_approved: z.literal(true).optional(),
})
.superRefine((input, context) => {
const ids = new Set<string>();
for (const [index, evidence] of input.native_observations.entries()) {
if (evidence.evidence_id === input.managed_boundaries.evidence_id) {
context.addIssue({
code: "custom",
path: ["native_observations", index],
message:
"Native observation Evidence must be distinct from managed boundary Evidence",
});
}
if (ids.has(evidence.evidence_id)) {
context.addIssue({
code: "custom",
path: ["native_observations", index],
message: "Native observation Evidence IDs must be unique",
});
}
ids.add(evidence.evidence_id);
}
});
export type ManagedNativeVerificationInput = z.infer<
typeof managedNativeVerificationInputSchema
>;
/** Provider-neutral managed/native verification result. */
export const managedNativeVerificationResultSchema = z.strictObject({
schema_version: z.literal(1),
verification_id: z.string().regex(/^mnv_[a-f0-9]{64}$/u),
algorithm: z.strictObject({
name: z.literal("rea-managed-native-verification"),
version: z.literal(1),
token_identity: z.literal("build-local"),
token_to_address_mapping: z.literal("not-inferred"),
}),
managed_boundary: z.strictObject({
evidence_id: evidenceIdSchema,
artifact_sha256: digestSchema,
artifact_path: z.string().min(1),
mvid: cliMetadataGuidSchema.nullable(),
metadata_status: z.enum(["absent", "complete", "partial", "malformed"]),
pinvoke_imports_total: z.number().int().min(0),
native_implementations_total: z.number().int().min(0),
coverage_state: z.enum(["complete", "partial", "unavailable"]),
}),
native_observations: z.strictObject({
total: z.number().int().min(1),
accepted: z.number().int().min(0),
unsupported: z.number().int().min(0),
symbols: z.number().int().min(0),
truncated: z.boolean(),
}),
summary: z.strictObject({
verified: z.number().int().min(0),
inferred: z.number().int().min(0),
unresolved: z.number().int().min(0),
contradicted: z.number().int().min(0),
native_body_unresolved: z.number().int().min(0),
}),
pinvoke_imports: z.array(pinvokeVerificationSchema).max(50_000),
native_implementations: z.strictObject({
unresolved: z.number().int().min(0),
reason: boundedTextSchema,
}),
coverage: z.strictObject({
status: z.enum(["complete-within-inputs", "partial", "truncated"]),
omitted_native_observations: z.number().int().min(0),
omitted_candidates: z.number().int().min(0),
}),
evidence_links: z.array(evidenceIdSchema).min(2).max(51),
limitations: z.array(boundedTextSchema).max(1_000),
});
export type ManagedNativeVerificationResult = z.infer<
typeof managedNativeVerificationResultSchema
>;
+143 -87
View File
@@ -7,6 +7,7 @@ import { evidenceSchema, parseEvidence } from "./evidence.js";
import {
cliMetadataGuidSchema,
managedMemberInspectionSchema,
type ManagedMemberInspection,
} from "./managedArtifact.js";
import type { JsonValue } from "./jsonValue.js";
@@ -39,6 +40,10 @@ const managedReconstructionMethodInputSchema = z.strictObject({
}),
});
type ManagedReconstructionMethodInput = z.infer<
typeof managedReconstructionMethodInputSchema
>;
export const managedReconstructionImportInputSchema = z.strictObject({
static_members: evidenceSchema,
decompiler: decompilerSchema,
@@ -111,6 +116,11 @@ export type ManagedReconstructionImportResult = z.infer<
typeof managedReconstructionImportResultSchema
>;
type Member = ManagedMemberInspection["methods"]["items"][number];
type ImportedMethod = ManagedReconstructionImportResult["methods"][number];
type StaticObservation =
ManagedReconstructionImportResult["static_observation"];
const sha256Text = (value: string): string =>
createHash("sha256").update(value).digest("hex");
@@ -121,6 +131,131 @@ const sha256Json = (value: JsonValue): string => {
return createHash("sha256").update(serialized).digest("hex");
};
const countLines = (text: string): number =>
text.length === 0 ? 0 : text.split(/\r\n|\r|\n/u).length;
const findObservedMethod = (
members: ManagedMemberInspection,
candidate: ManagedReconstructionMethodInput,
): Member | undefined =>
members.methods.items.find(
(item) =>
item.token === candidate.token &&
item.signature.raw_sha256 === candidate.signature_sha256 &&
item.body.status === "present" &&
item.body.normalized_il_sha256 !== null &&
candidate.normalized_il_sha256 !== null &&
item.body.normalized_il_sha256 === candidate.normalized_il_sha256,
);
const verifyTextSha256 = (
candidate: ManagedReconstructionMethodInput,
textSha256: string,
): void => {
if (
candidate.reconstruction.text_sha256 !== undefined &&
candidate.reconstruction.text_sha256 !== textSha256
)
throw new TypeError(
`Reconstruction text hash mismatch for ${candidate.token}`,
);
};
const buildImportedMethod = (
observed: Member,
candidate: ManagedReconstructionMethodInput,
textSha256: string,
): ImportedMethod => ({
token: observed.token,
declaring_type: observed.declaring_type,
name: observed.name,
signature_sha256: observed.signature.raw_sha256,
normalized_il_sha256: observed.body.normalized_il_sha256,
body_sha256: observed.body.il_sha256,
il_size: observed.body.il_size,
reconstruction: {
kind: candidate.reconstruction.kind,
language: candidate.reconstruction.language,
text: candidate.reconstruction.text,
text_sha256: textSha256,
text_length: candidate.reconstruction.text.length,
line_count: countLines(candidate.reconstruction.text),
source_path: candidate.reconstruction.source_path ?? null,
start_line: candidate.reconstruction.start_line ?? null,
end_line: candidate.reconstruction.end_line ?? null,
},
validation: {
matched_static_member: true as const,
exact_build_required: true as const,
canonical_observation: false as const,
confidence_floor: "inference" as const,
},
});
const buildReconstructionSummary = (
methods: readonly ImportedMethod[],
): ManagedReconstructionImportResult["summary"] => ({
imported_methods: methods.length,
decompiled_csharp_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "decompiled-csharp",
).length,
decompiled_il_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "decompiled-il",
).length,
pseudocode_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "semantic-pseudocode",
).length,
total_text_bytes: methods.reduce(
(total, { reconstruction }) => total + reconstruction.text_length,
0,
),
});
const buildStaticObservation = (
staticEvidence: ReturnType<typeof parseEvidence>,
members: ManagedMemberInspection,
): StaticObservation => ({
evidence_id: staticEvidence.evidence_id,
artifact_sha256: members.artifact.sha256,
artifact_path: members.artifact.path,
byte_length: members.artifact.byte_length,
module_name: members.module?.name ?? null,
mvid: members.module?.mvid ?? null,
metadata_status: members.metadata.status,
});
const buildDecompilerRecord = (
decompiler: ManagedReconstructionImportInput["decompiler"],
): ManagedReconstructionImportResult["decompiler"] => ({
...decompiler,
options_sha256: sha256Json(decompiler.options),
});
const RECONSTRUCTION_LIMITATIONS = [
"Decompiler output is imported as reconstruction evidence and analyst inference; metadata and IL observations remain canonical.",
"This operation did not execute, load, reflect over, debug, instrument, patch, or decompile the target artifact.",
"The imported reconstruction is valid only for the exact artifact SHA-256, MVID, method signature, and normalized IL shape recorded here.",
"Obfuscated names, decompiler variables, and control-flow restructuring are not treated as proof without separate validation.",
];
const buildReconstructionWithoutId = (
input: ManagedReconstructionImportInput,
staticEvidence: ReturnType<typeof parseEvidence>,
members: ManagedMemberInspection,
methods: readonly ImportedMethod[],
): Omit<ManagedReconstructionImportResult, "reconstruction_id"> => ({
schema_version: 1 as const,
phase: "reconstruction-import" as const,
executed: false as const,
static_observation: buildStaticObservation(staticEvidence, members),
decompiler: buildDecompilerRecord(input.decompiler),
summary: buildReconstructionSummary(methods),
methods: [...methods],
notes: input.notes,
evidence_links: [staticEvidence.evidence_id],
limitations: RECONSTRUCTION_LIMITATIONS,
});
/** Authenticate decompiler reconstruction against static IL Evidence. */
export const importManagedReconstruction = (
input: ManagedReconstructionImportInput,
@@ -132,102 +267,23 @@ export const importManagedReconstruction = (
staticEvidence.normalized_result,
);
const methods = input.methods.map((candidate) => {
const observed = members.methods.items.find(
(item) =>
item.token === candidate.token &&
item.signature.raw_sha256 === candidate.signature_sha256 &&
item.body.status === "present" &&
item.body.normalized_il_sha256 !== null &&
candidate.normalized_il_sha256 !== null &&
item.body.normalized_il_sha256 === candidate.normalized_il_sha256,
);
const observed = findObservedMethod(members, candidate);
if (observed === undefined)
throw new TypeError(
`Reconstruction method lock ${candidate.token} does not match a static managed member observation`,
);
const textSha256 = sha256Text(candidate.reconstruction.text);
if (
candidate.reconstruction.text_sha256 !== undefined &&
candidate.reconstruction.text_sha256 !== textSha256
)
throw new TypeError(
`Reconstruction text hash mismatch for ${candidate.token}`,
);
return {
token: observed.token,
declaring_type: observed.declaring_type,
name: observed.name,
signature_sha256: observed.signature.raw_sha256,
normalized_il_sha256: observed.body.normalized_il_sha256,
body_sha256: observed.body.il_sha256,
il_size: observed.body.il_size,
reconstruction: {
kind: candidate.reconstruction.kind,
language: candidate.reconstruction.language,
text: candidate.reconstruction.text,
text_sha256: textSha256,
text_length: candidate.reconstruction.text.length,
line_count: countLines(candidate.reconstruction.text),
source_path: candidate.reconstruction.source_path ?? null,
start_line: candidate.reconstruction.start_line ?? null,
end_line: candidate.reconstruction.end_line ?? null,
},
validation: {
matched_static_member: true as const,
exact_build_required: true as const,
canonical_observation: false as const,
confidence_floor: "inference" as const,
},
};
verifyTextSha256(candidate, textSha256);
return buildImportedMethod(observed, candidate, textSha256);
});
const withoutId = {
schema_version: 1 as const,
phase: "reconstruction-import" as const,
executed: false as const,
static_observation: {
evidence_id: staticEvidence.evidence_id,
artifact_sha256: members.artifact.sha256,
artifact_path: members.artifact.path,
byte_length: members.artifact.byte_length,
module_name: members.module?.name ?? null,
mvid: members.module?.mvid ?? null,
metadata_status: members.metadata.status,
},
decompiler: {
...input.decompiler,
options_sha256: sha256Json(input.decompiler.options),
},
summary: {
imported_methods: methods.length,
decompiled_csharp_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "decompiled-csharp",
).length,
decompiled_il_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "decompiled-il",
).length,
pseudocode_methods: methods.filter(
({ reconstruction }) => reconstruction.kind === "semantic-pseudocode",
).length,
total_text_bytes: methods.reduce(
(total, { reconstruction }) => total + reconstruction.text_length,
0,
),
},
const withoutId = buildReconstructionWithoutId(
input,
staticEvidence,
members,
methods,
notes: input.notes,
evidence_links: [staticEvidence.evidence_id],
limitations: [
"Decompiler output is imported as reconstruction evidence and analyst inference; metadata and IL observations remain canonical.",
"This operation did not execute, load, reflect over, debug, instrument, patch, or decompile the target artifact.",
"The imported reconstruction is valid only for the exact artifact SHA-256, MVID, method signature, and normalized IL shape recorded here.",
"Obfuscated names, decompiler variables, and control-flow restructuring are not treated as proof without separate validation.",
],
};
);
return managedReconstructionImportResultSchema.parse({
...withoutId,
reconstruction_id: `mre_${sha256Json(withoutId)}`,
});
};
const countLines = (text: string): number =>
text.length === 0 ? 0 : text.split(/\r\n|\r|\n/u).length;