mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
279 lines
8.5 KiB
TypeScript
279 lines
8.5 KiB
TypeScript
import { viewParentIdentity } from "./viewParentIdentity.js";
|
|
import { z } from "zod";
|
|
|
|
import { androidResultSchemas } from "../android/androidAnalysis.js";
|
|
import { androidClassViewRequestSchema } from "../android/androidClassQuery.js";
|
|
import { evidenceSubjectTarget } from "../evidence.js";
|
|
import { boundedList } from "../boundedList.js";
|
|
import { ok } from "../result.js";
|
|
import { filterSet } from "./selectViewRows.js";
|
|
import {
|
|
invalidAnalysisViewSubject,
|
|
invalidAnalysisView,
|
|
} from "./analysisViewErrors.js";
|
|
import {
|
|
createAnalysisViewResultReader,
|
|
type AnalysisViewParent,
|
|
} from "./analysisViewParent.js";
|
|
import {
|
|
boundedViewLimitations,
|
|
completeWithinViewCoverage,
|
|
byteBoundedItemsPage,
|
|
} from "./analysisViewCoverage.js";
|
|
import {
|
|
ownedView,
|
|
type AdmittedView,
|
|
type SummaryAnalysisView,
|
|
} from "./analysisViewShape.js";
|
|
import {
|
|
byteBoundedItemsPageOwner,
|
|
selectByteBoundedViewRows,
|
|
type ByteBoundedPageOwner,
|
|
} from "./byteBoundedViewRows.js";
|
|
|
|
export const INSPECT_ANDROID_CLASS_OPERATION = "inspect_android_class" as const;
|
|
|
|
const classSchema = androidResultSchemas.inspect_android_class;
|
|
type AndroidClass = z.output<typeof classSchema>;
|
|
type ClassView = z.output<typeof androidClassViewRequestSchema>;
|
|
const shape = classSchema.shape;
|
|
const mappingSchema = shape.mapping.unwrap();
|
|
|
|
/** Overloaded method names listed in a summary, most overloads first. */
|
|
const SUMMARY_OVERLOADED_NAMES = 64;
|
|
|
|
const count = z.int().nonnegative();
|
|
|
|
/** The class's identity and counts, with its member lists paged separately. */
|
|
export const androidClassSummarySchema = z.strictObject({
|
|
engine: shape.engine,
|
|
class_name: shape.class_name,
|
|
counts: z.strictObject({
|
|
methods: count,
|
|
constructors: count,
|
|
overloaded_method_names: count,
|
|
fields: count,
|
|
static_values: count,
|
|
inner_classes: count,
|
|
}),
|
|
overloaded_methods: z
|
|
.strictObject({
|
|
items: z.array(z.strictObject({ name: z.string(), overloads: count })),
|
|
omitted: count,
|
|
})
|
|
.describe(
|
|
`Method names declared more than once, most overloads first, at most ${String(SUMMARY_OVERLOADED_NAMES)}; omitted counts the rest`,
|
|
),
|
|
mapping: mappingSchema
|
|
.omit({ symbols: true })
|
|
.extend({ symbols: count })
|
|
.exactOptional()
|
|
.describe(
|
|
"The caller-selected mapping's identity and record counts; page its symbols with facet mapping-symbols",
|
|
),
|
|
});
|
|
|
|
/** One declared method with its position among the class's methods. */
|
|
export const androidClassMethodRowSchema = shape.methods.element.extend({
|
|
index: count,
|
|
});
|
|
/** One declared field with its position among the class's fields. */
|
|
export const androidClassFieldRowSchema = shape.fields.element.extend({
|
|
index: count,
|
|
});
|
|
export const androidClassMappingSymbolRowSchema =
|
|
mappingSchema.shape.symbols.element;
|
|
|
|
const readClass = createAnalysisViewResultReader(classSchema);
|
|
|
|
const compare = ([a, x]: [string, number], [b, y]: [string, number]) =>
|
|
y - x || (a < b ? -1 : a > b ? 1 : 0);
|
|
|
|
const summarize = (
|
|
value: AndroidClass,
|
|
): z.output<typeof androidClassSummarySchema> => {
|
|
const overloads = new Map<string, number>();
|
|
let constructors = 0;
|
|
let staticValues = 0;
|
|
for (const field of value.fields)
|
|
if (field.dex_static_value !== null) staticValues++;
|
|
for (const method of value.methods) {
|
|
overloads.set(method.name, (overloads.get(method.name) ?? 0) + 1);
|
|
if (method.is_constructor) constructors++;
|
|
}
|
|
const overloaded = [...overloads].filter(([, n]) => n > 1).sort(compare);
|
|
const { mapping } = value;
|
|
const listed = boundedList(
|
|
overloaded.map(([name, n]) => ({ name, overloads: n })),
|
|
SUMMARY_OVERLOADED_NAMES,
|
|
);
|
|
return {
|
|
engine: value.engine,
|
|
class_name: value.class_name,
|
|
counts: {
|
|
methods: value.methods.length,
|
|
constructors,
|
|
overloaded_method_names: overloaded.length,
|
|
fields: value.fields.length,
|
|
static_values: staticValues,
|
|
inner_classes: value.inner_classes.length,
|
|
},
|
|
overloaded_methods: { items: [...listed.items], omitted: listed.omitted },
|
|
...(mapping === undefined
|
|
? {}
|
|
: {
|
|
mapping: (({ symbols, ...facts }) => ({
|
|
...facts,
|
|
symbols: symbols.length,
|
|
}))(mapping),
|
|
}),
|
|
};
|
|
};
|
|
|
|
/** A name filter over exact names and prefixes; undefined when unfiltered. */
|
|
const nameFilter = (
|
|
names: readonly string[] | undefined,
|
|
prefixes: readonly string[] | undefined,
|
|
) => {
|
|
if (names === undefined && prefixes === undefined) return undefined;
|
|
const exact = filterSet(names);
|
|
return (name: string) =>
|
|
(exact === undefined || exact.has(name)) &&
|
|
(prefixes === undefined ||
|
|
prefixes.some((prefix) => name.startsWith(prefix)));
|
|
};
|
|
|
|
const selectMemberPage = <View extends ClassView, Source, Row>(
|
|
rows: readonly Source[],
|
|
view: View,
|
|
owner: ByteBoundedPageOwner,
|
|
selection: Parameters<typeof selectByteBoundedViewRows<Source, Row>>[2],
|
|
) => {
|
|
const selected = selectByteBoundedViewRows(rows, view, selection, owner);
|
|
if (!selected.ok) return selected;
|
|
return ok(byteBoundedItemsPage({ view }, selected.value));
|
|
};
|
|
|
|
const selectPlainMemberPage = <View extends ClassView, Row>(
|
|
rows: readonly Row[],
|
|
view: View,
|
|
owner: ByteBoundedPageOwner,
|
|
) =>
|
|
selectMemberPage(rows, view, owner, {
|
|
matches: undefined,
|
|
project: (row: Row) => row,
|
|
});
|
|
|
|
const selectNamedMemberPage = <
|
|
View extends Extract<ClassView, { facet: "methods" | "fields" }>,
|
|
Row extends { readonly name: string },
|
|
>(
|
|
rows: readonly Row[],
|
|
view: View,
|
|
owner: ByteBoundedPageOwner,
|
|
flag: boolean | undefined,
|
|
readFlag: (row: Row) => boolean,
|
|
) => {
|
|
const name = nameFilter(view.names, view.name_prefixes);
|
|
const matches =
|
|
name === undefined && flag === undefined
|
|
? undefined
|
|
: (row: Row) =>
|
|
(name === undefined || name(row.name)) &&
|
|
(flag === undefined || readFlag(row) === flag);
|
|
return selectMemberPage(rows, view, owner, {
|
|
matches,
|
|
project: (row, index) => ({ ...row, index }),
|
|
});
|
|
};
|
|
|
|
/** One page of the selected member facet, with the total that match. */
|
|
const selectMembers = (
|
|
value: AndroidClass,
|
|
view: ClassView,
|
|
owner: ByteBoundedPageOwner,
|
|
) => {
|
|
switch (view.facet) {
|
|
case "methods":
|
|
return selectNamedMemberPage(
|
|
value.methods,
|
|
view,
|
|
owner,
|
|
view.constructors,
|
|
(row) => row.is_constructor,
|
|
);
|
|
case "fields":
|
|
return selectNamedMemberPage(
|
|
value.fields,
|
|
view,
|
|
owner,
|
|
view.static_values,
|
|
(row) => row.dex_static_value !== null,
|
|
);
|
|
case "inner-classes":
|
|
return selectPlainMemberPage(value.inner_classes, view, owner);
|
|
case "mapping-symbols":
|
|
if (value.mapping === undefined)
|
|
return invalidAnalysisView(
|
|
"This inspection selected no mapping; pass mapping_path to inspect_android_class first.",
|
|
"facet",
|
|
);
|
|
return selectPlainMemberPage(value.mapping.symbols, view, owner);
|
|
}
|
|
};
|
|
|
|
/** Project a retained class inspection's summary or one member page. */
|
|
export const projectAndroidClassView = (
|
|
parent: AnalysisViewParent,
|
|
request: AdmittedView,
|
|
) => {
|
|
const admitted = ownedView<SummaryAnalysisView | ClassView>(
|
|
request,
|
|
"summary",
|
|
"android-class",
|
|
);
|
|
if (admitted === undefined)
|
|
return invalidAnalysisView(
|
|
"Select an android-class or summary view.",
|
|
"kind",
|
|
);
|
|
const parsed = readClass(parent);
|
|
if (!parsed.ok) return parsed;
|
|
// The engine artifact digest is the JAR; it cannot identify the APK.
|
|
const subject = evidenceSubjectTarget(parent.subject);
|
|
if (subject === undefined)
|
|
return invalidAnalysisViewSubject(
|
|
"Android class views require the recorded APK subject identity.",
|
|
);
|
|
const value = parsed.value;
|
|
const shared = {
|
|
...viewParentIdentity(parent, INSPECT_ANDROID_CLASS_OPERATION),
|
|
artifact: { path: subject.path, sha256: subject.sha256 },
|
|
limitations: boundedViewLimitations(parent),
|
|
unknowns: [
|
|
"Static class and method analysis does not establish runtime behavior or APK authenticity.",
|
|
],
|
|
};
|
|
if (admitted.kind === "summary")
|
|
return ok({
|
|
...shared,
|
|
kind: "summary" as const,
|
|
view: admitted,
|
|
summary: summarize(value),
|
|
coverage: completeWithinViewCoverage(1),
|
|
});
|
|
const view = admitted;
|
|
const context = {
|
|
...shared,
|
|
kind: "android-class" as const,
|
|
view,
|
|
class_name: value.class_name,
|
|
};
|
|
const page = selectMembers(
|
|
value,
|
|
view,
|
|
byteBoundedItemsPageOwner(parent.evidenceId, view.facet, context),
|
|
);
|
|
return page.ok ? ok({ ...context, ...page.value }) : page;
|
|
};
|