rea-agents 1.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -66
- package/bridge/ghidra/ReaGhidraBridge.java +2075 -0
- package/bridge/hopper_bridge.py +13 -3
- package/dist/application/AbortablePromise.js +25 -0
- package/dist/application/AnalysisProvider.js +3 -0
- package/dist/application/AnalysisProviderEvaluation.js +60 -0
- package/dist/application/AnalysisProviderRegistry.js +228 -0
- package/dist/application/AnalysisSnapshotCache.js +41 -21
- package/dist/application/AnalysisSnapshotFiles.js +5 -6
- package/dist/application/BinarySession.js +218 -101
- package/dist/application/CapabilityInventory.js +18 -0
- package/dist/application/CompositeProvider.js +20 -8
- package/dist/application/CrossVersionInvestigation.js +10 -6
- package/dist/application/DirectAnalysis.js +49 -35
- package/dist/application/Doctor.js +110 -8
- package/dist/application/ElectronBoundaryAnalysis.js +96 -0
- package/dist/application/ElectronBoundaryGraph.js +9 -0
- package/dist/application/ElectronBoundaryGraphContext.js +62 -0
- package/dist/application/ElectronBoundaryGraphIpc.js +242 -0
- package/dist/application/ElectronBoundaryGraphNative.js +86 -0
- package/dist/application/ElectronBoundaryGraphWindows.js +199 -0
- package/dist/application/EnhancedTools.js +39 -14
- package/dist/application/InvestigationProviders.js +41 -0
- package/dist/application/JavaScriptApplicationEvidence.js +26 -0
- package/dist/application/JavaScriptApplicationEvidenceGraph.js +89 -0
- package/dist/application/JavaScriptApplicationService.js +74 -0
- package/dist/application/JavaScriptApplicationWorkflowEvidence.js +29 -0
- package/dist/application/JavaScriptApplicationWorkflowService.js +90 -0
- package/dist/application/JavaScriptArtifactAnalysis.js +305 -0
- package/dist/application/JavaScriptArtifactAnalysisTypes.js +1 -0
- package/dist/application/JavaScriptArtifactFiles.js +231 -0
- package/dist/application/JavaScriptArtifactGraphAccumulator.js +54 -0
- package/dist/application/JavaScriptArtifactGraphBuilder.js +144 -0
- package/dist/application/JavaScriptArtifactGraphContext.js +205 -0
- package/dist/application/JavaScriptArtifactGraphDocuments.js +108 -0
- package/dist/application/JavaScriptArtifactGraphEvidence.js +99 -0
- package/dist/application/JavaScriptArtifactGraphFindings.js +271 -0
- package/dist/application/JavaScriptArtifactGraphStructure.js +409 -0
- package/dist/application/JavaScriptArtifactReconstruction.js +77 -0
- package/dist/application/JavaScriptArtifactReconstructionInput.js +18 -0
- package/dist/application/JavaScriptReplayPlanning.js +201 -0
- package/dist/application/JavaScriptReplayService.js +266 -0
- package/dist/application/JavaScriptRuntimeReconciliationEvidence.js +25 -0
- package/dist/application/JavaScriptRuntimeReconciliationService.js +27 -0
- package/dist/application/MacHopper.js +20 -4
- package/dist/application/ManagedApplicationGraphService.js +55 -0
- package/dist/application/ManagedMemberComparisonService.js +122 -0
- package/dist/application/ManagedNativeVerificationService.js +39 -0
- package/dist/application/ManagedReconstructionService.js +58 -0
- package/dist/application/ManagedRuntimeCorrelationService.js +103 -0
- package/dist/application/PermissionAuthority.js +1 -1
- package/dist/application/ProcessCaptureLifecycle.js +1 -1
- package/dist/application/ProcessHarness.js +1 -1
- package/dist/application/ProcessSampling.js +16 -12
- package/dist/application/ProjectPermissionStore.js +1 -0
- package/dist/application/SessionProviderRouter.js +219 -0
- package/dist/application/Setup.js +84 -60
- package/dist/application/SetupClients.js +1 -1
- package/dist/application/SetupPlan.js +9 -2
- package/dist/application/SupportedClients.js +40 -16
- package/dist/application/Upgrade.js +1 -1
- package/dist/application/runtime.js +11 -6
- package/dist/artifacts/AsarArtifactReader.js +23 -3
- package/dist/browser/CdpBrowserProvider.js +4 -2
- package/dist/browser/CdpCaptureEvents.js +49 -0
- package/dist/browser/CdpElectronInspection.js +106 -162
- package/dist/browser/CdpElectronProvider.js +9 -3
- package/dist/browser/CdpElectronScriptEvents.js +69 -0
- package/dist/browser/CdpElectronScripts.js +94 -0
- package/dist/browser/CdpElectronWorkers.js +56 -0
- package/dist/browser/CdpEndpoint.js +14 -1
- package/dist/browser/CdpPageCapture.js +8 -4
- package/dist/catalogIdentity.js +6 -42
- package/dist/cli.js +446 -34
- package/dist/cliApplicationCommands.js +62 -0
- package/dist/cliBrowserAdvancedCommands.js +5 -4
- package/dist/cliBrowserCommands.js +5 -4
- package/dist/cliCommandNames.js +56 -0
- package/dist/cliElectronCommands.js +106 -3
- package/dist/cliEvidenceCommands.js +4 -3
- package/dist/cliInvestigationCommands.js +2 -1
- package/dist/cliJsonInput.js +54 -0
- package/dist/cliPolicyCommands.js +2 -1
- package/dist/cliProcessCommands.js +3 -2
- package/dist/config.js +101 -2
- package/dist/contracts/applicationToolContracts.js +91 -0
- package/dist/contracts/artifactToolContracts.js +5 -9
- package/dist/contracts/browserToolContracts.js +14 -62
- package/dist/contracts/electronToolContracts.js +45 -19
- package/dist/contracts/enhancedInputs.js +2 -2
- package/dist/contracts/errorSchemas.js +1 -0
- package/dist/contracts/investigationExamples.js +0 -6
- package/dist/contracts/javascriptApplicationWorkflowExamples.js +46 -0
- package/dist/contracts/javascriptRuntimeReconciliationExample.js +208 -0
- package/dist/contracts/managedToolContracts.js +153 -0
- package/dist/contracts/managedWorkflowExamples.js +346 -0
- package/dist/contracts/managedWorkflowToolContracts.js +203 -0
- package/dist/contracts/nativeToolContracts.js +2 -6
- package/dist/contracts/promptContracts.js +18 -2
- package/dist/contracts/providerSelection.js +28 -0
- package/dist/contracts/sessionLifecycleInputs.js +2 -0
- package/dist/contracts/toolContractExamples.js +10 -25
- package/dist/contracts/toolContracts.js +69 -65
- package/dist/contracts/toolEffects.js +160 -0
- package/dist/contracts/toolOutputSchemas.js +152 -42
- package/dist/doctorRuntime.js +47 -0
- package/dist/domain/analysisProfile.js +78 -0
- package/dist/domain/analysisSnapshot.js +92 -46
- package/dist/domain/artifactComparison.js +3 -4
- package/dist/domain/artifactGraph.js +1 -0
- package/dist/domain/binaryTarget.js +4 -29
- package/dist/domain/browserObservation.js +3 -0
- package/dist/domain/bundleComparison.js +4 -4
- package/dist/domain/electronObservation.js +13 -0
- package/dist/domain/electronStaticAnalysis.js +9 -0
- package/dist/domain/electronStaticAnalysisBrowser.js +194 -0
- package/dist/domain/electronStaticAnalysisIpc.js +171 -0
- package/dist/domain/electronStaticAnalysisNative.js +164 -0
- package/dist/domain/electronStaticAnalysisTypes.js +1 -0
- package/dist/domain/electronStaticAnalysisValues.js +103 -0
- package/dist/domain/errors.js +148 -7
- package/dist/domain/evidence.js +44 -5
- package/dist/domain/evidenceBundle.js +9 -7
- package/dist/domain/functionComparison.js +9 -3
- package/dist/domain/functionComparisonNormalization.js +37 -0
- package/dist/domain/functionComparisonSchemas.js +3 -8
- package/dist/domain/functionDossierEvidence.js +4 -1
- package/dist/domain/hopperValues.js +66 -9
- package/dist/domain/inputIssueProjection.js +52 -0
- package/dist/domain/investigationWorkspace.js +2 -1
- package/dist/domain/javascriptApplicationAnalysis.js +155 -0
- package/dist/domain/javascriptApplicationChangeGraph.js +231 -0
- package/dist/domain/javascriptApplicationEvidenceSchemas.js +379 -0
- package/dist/domain/javascriptApplicationGraph.js +282 -0
- package/dist/domain/javascriptApplicationGraphSchemas.js +139 -0
- package/dist/domain/javascriptApplicationVersionComparison.js +116 -0
- package/dist/domain/javascriptApplicationVersionComparisonSchemas.js +139 -0
- package/dist/domain/javascriptApplicationVersionItems.js +221 -0
- package/dist/domain/javascriptApplicationVersionKeys.js +170 -0
- package/dist/domain/javascriptAstFingerprint.js +143 -0
- package/dist/domain/javascriptFeatureSeed.js +140 -0
- package/dist/domain/javascriptFeatureTrace.js +232 -0
- package/dist/domain/javascriptFeatureTraceSchemas.js +136 -0
- package/dist/domain/javascriptFeatureTraversal.js +88 -0
- package/dist/domain/javascriptNativeHandoff.js +85 -0
- package/dist/domain/javascriptReplay.js +361 -0
- package/dist/domain/javascriptRuntimeLoadState.js +106 -0
- package/dist/domain/javascriptRuntimeReconciliation.js +42 -0
- package/dist/domain/javascriptRuntimeReconciliationGraph.js +169 -0
- package/dist/domain/javascriptRuntimeReconciliationMatching.js +247 -0
- package/dist/domain/javascriptRuntimeReconciliationParsing.js +160 -0
- package/dist/domain/javascriptRuntimeReconciliationResult.js +119 -0
- package/dist/domain/javascriptRuntimeReconciliationRuntime.js +316 -0
- package/dist/domain/javascriptRuntimeReconciliationSchemas.js +245 -0
- package/dist/domain/javascriptRuntimeStaticCandidates.js +191 -0
- package/dist/domain/javascriptStaticAnalysis.js +346 -0
- package/dist/domain/javascriptStaticAnalysisFindings.js +39 -0
- package/dist/domain/javascriptStaticAnalysisHelpers.js +274 -0
- package/dist/domain/javascriptStaticAnalysisState.js +23 -0
- package/dist/domain/javascriptStaticAnalysisTypes.js +1 -0
- package/dist/domain/managedApplicationGraph.js +556 -0
- package/dist/domain/managedArtifact.js +444 -0
- package/dist/domain/managedMemberComparison.js +565 -0
- package/dist/domain/managedNativeVerification.js +408 -0
- package/dist/domain/managedReconstruction.js +183 -0
- package/dist/domain/managedRuntimeCorrelation.js +175 -0
- package/dist/domain/reconstructionVerificationSchemas.js +1 -3
- package/dist/dotnet/ManagedArtifactInspector.js +318 -0
- package/dist/dotnet/ManagedMemberInspector.js +887 -0
- package/dist/dotnet/ManagedMetadataHeaps.js +131 -0
- package/dist/dotnet/ManagedMetadataInventory.js +368 -0
- package/dist/dotnet/ManagedMetadataLayout.js +322 -0
- package/dist/dotnet/ManagedNativeBoundaryInspector.js +391 -0
- package/dist/dotnet/ManagedPeReader.js +157 -0
- package/dist/dotnet/ManagedReaderFailure.js +10 -0
- package/dist/dotnet/ManagedStaticProvider.js +203 -0
- package/dist/generatedPackageMetadata.js +2 -2
- package/dist/ghidra/GhidraAnalysisProfile.js +37 -0
- package/dist/ghidra/GhidraClient.js +400 -0
- package/dist/ghidra/GhidraClientTypes.js +1 -0
- package/dist/ghidra/GhidraDefaults.js +20 -0
- package/dist/ghidra/GhidraDiagnostics.js +43 -0
- package/dist/ghidra/GhidraDoctor.js +59 -0
- package/dist/ghidra/GhidraFunctionValues.js +264 -0
- package/dist/ghidra/GhidraInstallation.js +239 -0
- package/dist/ghidra/GhidraInventoryValues.js +217 -0
- package/dist/ghidra/GhidraLauncher.js +139 -0
- package/dist/ghidra/GhidraProvider.js +366 -0
- package/dist/ghidra/GhidraRequestQueue.js +98 -0
- package/dist/ghidra/GhidraResponseBuffer.js +31 -0
- package/dist/ghidra/GhidraResponseRouter.js +24 -0
- package/dist/ghidra/GhidraSessionError.js +39 -0
- package/dist/ghidra/GhidraSessionValues.js +62 -0
- package/dist/ghidra/GhidraSocketConnection.js +46 -0
- package/dist/ghidra/protocol.js +69 -0
- package/dist/hopper/BridgeLauncher.js +19 -54
- package/dist/hopper/HopperAnalysisProfile.js +106 -0
- package/dist/hopper/HopperClient.js +154 -162
- package/dist/hopper/HopperProvider.js +99 -7
- package/dist/hopper/HopperSessionValues.js +27 -0
- package/dist/hopper/HopperSocketConnection.js +31 -0
- package/dist/main.js +14 -0
- package/dist/native/NativeMacOSProvider.js +3 -1
- package/dist/process/PendingOperations.js +59 -0
- package/dist/process/PrivateRuntimeRoot.js +33 -0
- package/dist/process/ProviderDeadline.js +95 -0
- package/dist/process/ProviderProcess.js +275 -0
- package/dist/replay/JavaScriptReplayWorker.js +286 -0
- package/dist/replay/LinuxJavaScriptReplayRunner.js +434 -0
- package/dist/replay/LinuxRuntimeClosure.js +46 -0
- package/dist/replay/LinuxSeccompPolicy.js +40 -0
- package/dist/replay/ReplayWorkerProtocol.js +55 -0
- package/dist/replay/SystemJavaScriptReplayHost.js +182 -0
- package/dist/server/createServer.js +53 -0
- package/dist/server/promptCompletion.js +9 -3
- package/dist/server/registerApplicationTools.js +115 -0
- package/dist/server/registerArtifactComparisonTool.js +16 -9
- package/dist/server/registerBrowserTools.js +33 -8
- package/dist/server/registerBundleComparisonTool.js +17 -20
- package/dist/server/registerElectronTools.js +45 -3
- package/dist/server/registerEnhancedTools.js +67 -19
- package/dist/server/registerEvidenceTools.js +5 -1
- package/dist/server/registerFunctionComparisonTool.js +14 -7
- package/dist/server/registerInvestigationTools.js +21 -17
- package/dist/server/registerManagedTools.js +6 -0
- package/dist/server/registerManagedWorkflowTools.js +277 -0
- package/dist/server/registerOfficialTools.js +11 -8
- package/dist/server/registerProcessComparisonTool.js +25 -47
- package/dist/server/registerSessionStatusTool.js +7 -7
- package/dist/server/registerSessionTools.js +57 -87
- package/dist/server/sessionEvidence.js +14 -19
- package/dist/server/toolInputValidation.js +77 -0
- package/dist/server/toolRegistrationOptions.js +79 -2
- package/dist/server/toolResult.js +25 -17
- package/package.json +17 -7
- package/scripts/rea.mjs +36 -2
- package/skills/rea-analysis/SKILL.md +59 -3
- /package/dist/{application → process}/ProcessOwnership.js +0 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { managedFailure } from "./ManagedReaderFailure.js";
|
|
2
|
+
export const METADATA_TABLE_NAMES = [
|
|
3
|
+
"Module",
|
|
4
|
+
"TypeRef",
|
|
5
|
+
"TypeDef",
|
|
6
|
+
"FieldPtr",
|
|
7
|
+
"Field",
|
|
8
|
+
"MethodPtr",
|
|
9
|
+
"MethodDef",
|
|
10
|
+
"ParamPtr",
|
|
11
|
+
"Param",
|
|
12
|
+
"InterfaceImpl",
|
|
13
|
+
"MemberRef",
|
|
14
|
+
"Constant",
|
|
15
|
+
"CustomAttribute",
|
|
16
|
+
"FieldMarshal",
|
|
17
|
+
"DeclSecurity",
|
|
18
|
+
"ClassLayout",
|
|
19
|
+
"FieldLayout",
|
|
20
|
+
"StandAloneSig",
|
|
21
|
+
"EventMap",
|
|
22
|
+
"EventPtr",
|
|
23
|
+
"Event",
|
|
24
|
+
"PropertyMap",
|
|
25
|
+
"PropertyPtr",
|
|
26
|
+
"Property",
|
|
27
|
+
"MethodSemantics",
|
|
28
|
+
"MethodImpl",
|
|
29
|
+
"ModuleRef",
|
|
30
|
+
"TypeSpec",
|
|
31
|
+
"ImplMap",
|
|
32
|
+
"FieldRVA",
|
|
33
|
+
"ENCLog",
|
|
34
|
+
"ENCMap",
|
|
35
|
+
"Assembly",
|
|
36
|
+
"AssemblyProcessor",
|
|
37
|
+
"AssemblyOS",
|
|
38
|
+
"AssemblyRef",
|
|
39
|
+
"AssemblyRefProcessor",
|
|
40
|
+
"AssemblyRefOS",
|
|
41
|
+
"File",
|
|
42
|
+
"ExportedType",
|
|
43
|
+
"ManifestResource",
|
|
44
|
+
"NestedClass",
|
|
45
|
+
"GenericParam",
|
|
46
|
+
"MethodSpec",
|
|
47
|
+
"GenericParamConstraint",
|
|
48
|
+
];
|
|
49
|
+
const CODED_INDEXES = {
|
|
50
|
+
TypeDefOrRef: { bits: 2, tables: [2, 1, 27] },
|
|
51
|
+
HasConstant: { bits: 2, tables: [4, 8, 23] },
|
|
52
|
+
HasCustomAttribute: {
|
|
53
|
+
bits: 5,
|
|
54
|
+
tables: [
|
|
55
|
+
6, 4, 1, 2, 8, 9, 10, 0, 14, 23, 20, 17, 26, 27, 32, 35, 38, 39, 40, 42,
|
|
56
|
+
44, 43,
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
HasFieldMarshal: { bits: 1, tables: [4, 8] },
|
|
60
|
+
HasDeclSecurity: { bits: 2, tables: [2, 6, 32] },
|
|
61
|
+
MemberRefParent: { bits: 3, tables: [2, 1, 26, 6, 27] },
|
|
62
|
+
HasSemantics: { bits: 1, tables: [20, 23] },
|
|
63
|
+
MethodDefOrRef: { bits: 1, tables: [6, 10] },
|
|
64
|
+
MemberForwarded: { bits: 1, tables: [4, 6] },
|
|
65
|
+
Implementation: { bits: 2, tables: [38, 35, 39] },
|
|
66
|
+
CustomAttributeType: { bits: 3, tables: [6, 10] },
|
|
67
|
+
ResolutionScope: { bits: 2, tables: [0, 26, 35, 1] },
|
|
68
|
+
TypeOrMethodDef: { bits: 1, tables: [2, 6] },
|
|
69
|
+
};
|
|
70
|
+
const SUPPORTED_TABLE_MASK = (1n << BigInt(METADATA_TABLE_NAMES.length)) - 1n;
|
|
71
|
+
const requireRange = (bytes, offset, length, scope) => {
|
|
72
|
+
if (!Number.isSafeInteger(offset) ||
|
|
73
|
+
!Number.isSafeInteger(length) ||
|
|
74
|
+
offset < 0 ||
|
|
75
|
+
length < 0 ||
|
|
76
|
+
offset > bytes.length - length)
|
|
77
|
+
throw managedFailure("invalid-metadata-root", scope, `${scope} leaves the metadata byte range`, Number.isSafeInteger(offset) && offset >= 0 ? offset : null);
|
|
78
|
+
};
|
|
79
|
+
const align4 = (value) => (value + 3) & ~3;
|
|
80
|
+
const readStreamName = (bytes, offset) => {
|
|
81
|
+
const endLimit = Math.min(bytes.length, offset + 32);
|
|
82
|
+
let end = offset;
|
|
83
|
+
while (end < endLimit && bytes[end] !== 0)
|
|
84
|
+
end += 1;
|
|
85
|
+
if (end === endLimit)
|
|
86
|
+
throw managedFailure("invalid-stream", "metadata.stream-name", "Metadata stream name is missing a bounded terminator", offset);
|
|
87
|
+
const nameBytes = bytes.subarray(offset, end);
|
|
88
|
+
if (nameBytes.some((byte) => byte < 0x20 || byte > 0x7e))
|
|
89
|
+
throw managedFailure("invalid-stream", "metadata.stream-name", "Metadata stream name is not printable ASCII", offset);
|
|
90
|
+
return {
|
|
91
|
+
name: nameBytes.toString("ascii"),
|
|
92
|
+
next: align4(end + 1),
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
const streamByName = (streams, names) => {
|
|
96
|
+
for (const name of names) {
|
|
97
|
+
const stream = streams.get(name);
|
|
98
|
+
if (stream !== undefined)
|
|
99
|
+
return stream;
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
};
|
|
103
|
+
const requireStream = (streams, name) => {
|
|
104
|
+
const stream = streams.get(name);
|
|
105
|
+
if (stream === undefined)
|
|
106
|
+
throw managedFailure("missing-stream", `metadata.${name}`, `Required metadata stream ${name} is absent`);
|
|
107
|
+
return stream;
|
|
108
|
+
};
|
|
109
|
+
const codedSize = (name, rowCounts) => {
|
|
110
|
+
const descriptor = CODED_INDEXES[name];
|
|
111
|
+
let maximum = 0;
|
|
112
|
+
for (const table of descriptor.tables)
|
|
113
|
+
maximum = Math.max(maximum, rowCounts[table] ?? 0);
|
|
114
|
+
return maximum < 2 ** (16 - descriptor.bits) ? 2 : 4;
|
|
115
|
+
};
|
|
116
|
+
const tableSize = (index, rows) => (rows[index] ?? 0) < 0x1_0000 ? 2 : 4;
|
|
117
|
+
const rowSize = (table, rowCounts, heaps) => {
|
|
118
|
+
const t = (index) => tableSize(index, rowCounts);
|
|
119
|
+
const c = (name) => codedSize(name, rowCounts);
|
|
120
|
+
const { string: s, guid: g, blob: b } = heaps;
|
|
121
|
+
switch (table) {
|
|
122
|
+
case 0:
|
|
123
|
+
return 2 + s + g * 3;
|
|
124
|
+
case 1:
|
|
125
|
+
return c("ResolutionScope") + s * 2;
|
|
126
|
+
case 2:
|
|
127
|
+
return 4 + s * 2 + c("TypeDefOrRef") + t(4) + t(6);
|
|
128
|
+
case 3:
|
|
129
|
+
return t(4);
|
|
130
|
+
case 4:
|
|
131
|
+
return 2 + s + b;
|
|
132
|
+
case 5:
|
|
133
|
+
return t(6);
|
|
134
|
+
case 6:
|
|
135
|
+
return 8 + s + b + t(8);
|
|
136
|
+
case 7:
|
|
137
|
+
return t(8);
|
|
138
|
+
case 8:
|
|
139
|
+
return 4 + s;
|
|
140
|
+
case 9:
|
|
141
|
+
return t(2) + c("TypeDefOrRef");
|
|
142
|
+
case 10:
|
|
143
|
+
return c("MemberRefParent") + s + b;
|
|
144
|
+
case 11:
|
|
145
|
+
return 2 + c("HasConstant") + b;
|
|
146
|
+
case 12:
|
|
147
|
+
return c("HasCustomAttribute") + c("CustomAttributeType") + b;
|
|
148
|
+
case 13:
|
|
149
|
+
return c("HasFieldMarshal") + b;
|
|
150
|
+
case 14:
|
|
151
|
+
return 2 + c("HasDeclSecurity") + b;
|
|
152
|
+
case 15:
|
|
153
|
+
return 6 + t(2);
|
|
154
|
+
case 16:
|
|
155
|
+
return 4 + t(4);
|
|
156
|
+
case 17:
|
|
157
|
+
return b;
|
|
158
|
+
case 18:
|
|
159
|
+
return t(2) + t(20);
|
|
160
|
+
case 19:
|
|
161
|
+
return t(20);
|
|
162
|
+
case 20:
|
|
163
|
+
return 2 + s + c("TypeDefOrRef");
|
|
164
|
+
case 21:
|
|
165
|
+
return t(2) + t(23);
|
|
166
|
+
case 22:
|
|
167
|
+
return t(23);
|
|
168
|
+
case 23:
|
|
169
|
+
return 2 + s + b;
|
|
170
|
+
case 24:
|
|
171
|
+
return 2 + t(6) + c("HasSemantics");
|
|
172
|
+
case 25:
|
|
173
|
+
return t(2) + c("MethodDefOrRef") * 2;
|
|
174
|
+
case 26:
|
|
175
|
+
return s;
|
|
176
|
+
case 27:
|
|
177
|
+
return b;
|
|
178
|
+
case 28:
|
|
179
|
+
return 2 + c("MemberForwarded") + s + t(26);
|
|
180
|
+
case 29:
|
|
181
|
+
return 4 + t(4);
|
|
182
|
+
case 30:
|
|
183
|
+
return 8;
|
|
184
|
+
case 31:
|
|
185
|
+
return 4;
|
|
186
|
+
case 32:
|
|
187
|
+
return 16 + b + s * 2;
|
|
188
|
+
case 33:
|
|
189
|
+
return 4;
|
|
190
|
+
case 34:
|
|
191
|
+
return 12;
|
|
192
|
+
case 35:
|
|
193
|
+
return 12 + b * 2 + s * 2;
|
|
194
|
+
case 36:
|
|
195
|
+
return 4 + t(35);
|
|
196
|
+
case 37:
|
|
197
|
+
return 12 + t(35);
|
|
198
|
+
case 38:
|
|
199
|
+
return 4 + s + b;
|
|
200
|
+
case 39:
|
|
201
|
+
return 8 + s * 2 + c("Implementation");
|
|
202
|
+
case 40:
|
|
203
|
+
return 8 + s + c("Implementation");
|
|
204
|
+
case 41:
|
|
205
|
+
return t(2) * 2;
|
|
206
|
+
case 42:
|
|
207
|
+
return 4 + c("TypeOrMethodDef") + s;
|
|
208
|
+
case 43:
|
|
209
|
+
return c("MethodDefOrRef") + b;
|
|
210
|
+
case 44:
|
|
211
|
+
return t(42) + c("TypeDefOrRef");
|
|
212
|
+
default:
|
|
213
|
+
throw managedFailure("invalid-tables", "metadata.tables", `Metadata table ${String(table)} is unsupported`);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
/** Parse ECMA-335 stream and table layout with checked byte extents. */
|
|
217
|
+
export const readManagedMetadataLayout = (artifact, rootOffset, size, maxTableRows) => {
|
|
218
|
+
if (size < 20 || rootOffset < 0 || rootOffset > artifact.length - size)
|
|
219
|
+
throw managedFailure("invalid-metadata-root", "metadata.root", "CLI metadata directory leaves the artifact byte range", rootOffset);
|
|
220
|
+
const bytes = artifact.subarray(rootOffset, rootOffset + size);
|
|
221
|
+
if (bytes.readUInt32LE(0) !== 0x424a_5342)
|
|
222
|
+
throw managedFailure("invalid-metadata-root", "metadata.signature", "CLI metadata signature is not BSJB", rootOffset);
|
|
223
|
+
const versionLength = bytes.readUInt32LE(12);
|
|
224
|
+
requireRange(bytes, 16, versionLength, "metadata.version");
|
|
225
|
+
const versionBytes = bytes.subarray(16, 16 + versionLength);
|
|
226
|
+
const zero = versionBytes.indexOf(0);
|
|
227
|
+
const version = versionBytes
|
|
228
|
+
.subarray(0, zero < 0 ? versionBytes.length : zero)
|
|
229
|
+
.toString("utf8");
|
|
230
|
+
let cursor = align4(16 + versionLength);
|
|
231
|
+
requireRange(bytes, cursor, 4, "metadata.stream-count");
|
|
232
|
+
const streamCount = bytes.readUInt16LE(cursor + 2);
|
|
233
|
+
if (streamCount === 0 || streamCount > 64)
|
|
234
|
+
throw managedFailure("invalid-stream", "metadata.stream-count", "Metadata stream count is outside the admitted range", rootOffset + cursor + 2);
|
|
235
|
+
cursor += 4;
|
|
236
|
+
const streams = new Map();
|
|
237
|
+
for (let index = 0; index < streamCount; index += 1) {
|
|
238
|
+
requireRange(bytes, cursor, 8, "metadata.stream-header");
|
|
239
|
+
const relativeOffset = bytes.readUInt32LE(cursor);
|
|
240
|
+
const streamSize = bytes.readUInt32LE(cursor + 4);
|
|
241
|
+
const named = readStreamName(bytes, cursor + 8);
|
|
242
|
+
if (streams.has(named.name))
|
|
243
|
+
throw managedFailure("invalid-stream", "metadata.stream-header", `Duplicate metadata stream ${named.name}`, rootOffset + cursor);
|
|
244
|
+
requireRange(bytes, relativeOffset, streamSize, `metadata.${named.name}`);
|
|
245
|
+
streams.set(named.name, {
|
|
246
|
+
name: named.name,
|
|
247
|
+
offset: rootOffset + relativeOffset,
|
|
248
|
+
size: streamSize,
|
|
249
|
+
});
|
|
250
|
+
cursor = named.next;
|
|
251
|
+
}
|
|
252
|
+
const tablesStream = streamByName(streams, ["#~", "#-"]);
|
|
253
|
+
if (tablesStream === undefined)
|
|
254
|
+
throw managedFailure("missing-stream", "metadata.tables", "Required metadata table stream #~ or #- is absent");
|
|
255
|
+
const tableBytes = artifact.subarray(tablesStream.offset, tablesStream.offset + tablesStream.size);
|
|
256
|
+
requireRange(tableBytes, 0, 24, "metadata.tables-header");
|
|
257
|
+
const heapSizes = tableBytes[6] ?? 0;
|
|
258
|
+
const heaps = {
|
|
259
|
+
string: (heapSizes & 0x01) === 0 ? 2 : 4,
|
|
260
|
+
guid: (heapSizes & 0x02) === 0 ? 2 : 4,
|
|
261
|
+
blob: (heapSizes & 0x04) === 0 ? 2 : 4,
|
|
262
|
+
};
|
|
263
|
+
const valid = tableBytes.readBigUInt64LE(8);
|
|
264
|
+
if ((valid & ~SUPPORTED_TABLE_MASK) !== 0n)
|
|
265
|
+
throw managedFailure("invalid-tables", "metadata.valid-mask", "Metadata valid mask names unsupported tables outside the admitted ECMA-335 range", tablesStream.offset + 8);
|
|
266
|
+
const rowCounts = Array(METADATA_TABLE_NAMES.length).fill(0);
|
|
267
|
+
let tableCursor = 24;
|
|
268
|
+
for (let index = 0; index < METADATA_TABLE_NAMES.length; index += 1) {
|
|
269
|
+
if ((valid & (1n << BigInt(index))) === 0n)
|
|
270
|
+
continue;
|
|
271
|
+
requireRange(tableBytes, tableCursor, 4, "metadata.table-row-count");
|
|
272
|
+
const count = tableBytes.readUInt32LE(tableCursor);
|
|
273
|
+
if (count > maxTableRows)
|
|
274
|
+
throw managedFailure("limit-exceeded", `metadata.${METADATA_TABLE_NAMES[index] ?? String(index)}`, `Metadata table row count ${String(count)} exceeds max_table_rows ${String(maxTableRows)}`, tablesStream.offset + tableCursor);
|
|
275
|
+
rowCounts[index] = count;
|
|
276
|
+
tableCursor += 4;
|
|
277
|
+
}
|
|
278
|
+
const tables = new Map();
|
|
279
|
+
for (let index = 0; index < METADATA_TABLE_NAMES.length; index += 1) {
|
|
280
|
+
const count = rowCounts[index] ?? 0;
|
|
281
|
+
if ((valid & (1n << BigInt(index))) === 0n)
|
|
282
|
+
continue;
|
|
283
|
+
const sizeOfRow = rowSize(index, rowCounts, heaps);
|
|
284
|
+
const byteLength = count * sizeOfRow;
|
|
285
|
+
if (!Number.isSafeInteger(byteLength))
|
|
286
|
+
throw managedFailure("invalid-tables", "metadata.tables", "Metadata table byte size overflowed", tablesStream.offset + tableCursor);
|
|
287
|
+
requireRange(tableBytes, tableCursor, byteLength, "metadata.table-data");
|
|
288
|
+
tables.set(index, {
|
|
289
|
+
index,
|
|
290
|
+
name: METADATA_TABLE_NAMES[index] ?? `Table${String(index)}`,
|
|
291
|
+
rowCount: count,
|
|
292
|
+
rowSize: sizeOfRow,
|
|
293
|
+
offset: tablesStream.offset + tableCursor,
|
|
294
|
+
});
|
|
295
|
+
tableCursor += byteLength;
|
|
296
|
+
}
|
|
297
|
+
const layout = {
|
|
298
|
+
rootOffset,
|
|
299
|
+
size,
|
|
300
|
+
version,
|
|
301
|
+
streamNames: [...streams.keys()].sort(),
|
|
302
|
+
strings: requireStream(streams, "#Strings"),
|
|
303
|
+
guid: requireStream(streams, "#GUID"),
|
|
304
|
+
blob: requireStream(streams, "#Blob"),
|
|
305
|
+
tables,
|
|
306
|
+
rowCounts,
|
|
307
|
+
stringIndexSize: heaps.string,
|
|
308
|
+
guidIndexSize: heaps.guid,
|
|
309
|
+
blobIndexSize: heaps.blob,
|
|
310
|
+
table: (index) => tables.get(index),
|
|
311
|
+
tableIndexSize: (index) => tableSize(index, rowCounts),
|
|
312
|
+
codedIndexSize: (name) => codedSize(name, rowCounts),
|
|
313
|
+
};
|
|
314
|
+
return layout;
|
|
315
|
+
};
|
|
316
|
+
/** Resolve a one-based metadata row to its exact file offset. */
|
|
317
|
+
export const metadataRowOffset = (layout, table, row) => {
|
|
318
|
+
const descriptor = layout.table(table);
|
|
319
|
+
if (descriptor === undefined || row < 1 || row > descriptor.rowCount)
|
|
320
|
+
throw managedFailure("invalid-row", `metadata.${METADATA_TABLE_NAMES[table] ?? String(table)}`, `Metadata row ${String(row)} is outside its table`);
|
|
321
|
+
return descriptor.offset + (row - 1) * descriptor.rowSize;
|
|
322
|
+
};
|