Skip to content

Commit 064fa5f

Browse files
committed
chore(gates): refuse a simctl argv array outside the brand mints however it reaches an executor
runCmd, runCmdBackground and an executable: 'xcrun' spec take any string argv, so the runXcrun brand does not reach them. R79 again flags every array whose first element names simctl (literal, quasi-only template, or a same-file binding of either) outside core/simctl.ts and core/tool-provider.ts, so a variable-held, aliased or spread simctl argv fails the gate as the inline form does.
1 parent 700b9e4 commit 064fa5f

3 files changed

Lines changed: 99 additions & 23 deletions

File tree

‎scripts/layering/apple-simulator-scope-policy.test.ts‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
} from './apple-simulator-scope-policy.ts';
77

88
const APPLE_SRC = 'packages/platform-apple/src/';
9+
const SIMCTL_ARGV_MESSAGE =
10+
/^builds a simctl argv outside core\/simctl\.ts and core\/tool-provider\.ts; build it with buildSimctlArgsForDevice or buildSimctlArgsForAddress$/;
911
const HAND_BUILT_MESSAGE =
1012
/^hands xcrun an argv whose tool is not a literal non-simctl name; build a simctl argv with buildSimctlArgsForDevice or buildSimctlArgsForAddress$/;
1113
const FORGED_MESSAGE =
@@ -29,14 +31,32 @@ const FORGED_ARGV =
2931
const FORGED_ADDRESS = 'const address = { udid, simulatorSetPath } as SimulatorAddress;\n';
3032

3133
test('the #2818 bridge spawn through a plain xcrun executor is refused', () => {
32-
assertFlagged(`${APPLE_SRC}snapshot-source/host.ts`, HAND_BUILT_BRIDGE, HAND_BUILT_MESSAGE);
34+
assertFlagged(`${APPLE_SRC}snapshot-source/host.ts`, HAND_BUILT_BRIDGE, SIMCTL_ARGV_MESSAGE);
3335
});
3436

35-
test('an xcrun argv must name its tool as a literal other than simctl', () => {
37+
test('a simctl argv is refused however it reaches an executor', () => {
3638
for (const source of [
37-
"const tool = 'simctl';\nrunCmd('xcrun', [tool, 'spawn', udid, bin]);\n",
39+
"const argv = ['simctl', 'spawn', udid, bin];\nrunCmd('xcrun', argv);\n",
40+
"const argv = ['simctl', 'spawn', udid, bridge];\nrunCmdBackground('xcrun', argv, { detached: true });\n",
41+
"const argv = ['simctl', 'spawn', udid, 'log', 'stream'];\nhost.commands.run({ executable: 'xcrun', args: argv });\n",
42+
"const argv = ['simctl', 'boot', udid];\nconst alias = argv;\nrunCmd('xcrun', alias);\n",
43+
"const argv = ['simctl', 'boot', udid];\nrunCmd('xcrun', [...argv]);\n",
44+
"const head = ['simctl'];\nrunCmd('xcrun', [...head, 'boot', udid]);\n",
45+
"const tool = 'simctl';\nconst argv = [tool, 'spawn', udid, bin];\nrunCmd('xcrun', argv);\n",
46+
"const tool = `simctl`;\nrunCmdBackground('xcrun', [tool, 'spawn', udid, bin]);\n",
3847
"runCmd('xcrun', [`simctl`, 'boot', udid]);\n",
3948
"host.commands.run({ executable: 'xcrun', args: ['simctl', 'spawn', udid, 'log', 'stream'] });\n",
49+
"const tools = ['simctl', 'devicectl'];\n",
50+
]) {
51+
assertFlagged(`${APPLE_SRC}logs/start.ts`, source, SIMCTL_ARGV_MESSAGE);
52+
}
53+
});
54+
55+
test('an inline xcrun argv must name its tool as a literal other than simctl', () => {
56+
for (const source of [
57+
"runCmd('xcrun', [tool, 'spawn', udid, bin]);\n",
58+
"runCmd('xcrun', [`${tool}`, 'boot', udid]);\n",
59+
"host.commands.run({ executable: 'xcrun', args: [request.tool, ...request.args] });\n",
4060
]) {
4161
assertFlagged(`${APPLE_SRC}logs/start.ts`, source, HAND_BUILT_MESSAGE);
4262
}
@@ -65,9 +85,13 @@ test('a cast to a simulator-scope brand is refused outside the mint modules', ()
6585
);
6686
});
6787

68-
test('the mint modules may cast to the brands they mint', () => {
88+
test('the mint modules may build and cast to the brands they mint', () => {
89+
const mintSource =
90+
FORGED_ARGV +
91+
FORGED_ADDRESS +
92+
"return Object.freeze(['simctl', ...args] as const) as ScopedSimctlCommand;\n";
6993
for (const mint of [`${APPLE_SRC}core/simctl.ts`, `${APPLE_SRC}core/tool-provider.ts`]) {
70-
assert.deepEqual(violationsFor(mint, FORGED_ARGV + FORGED_ADDRESS), [], mint);
94+
assert.deepEqual(violationsFor(mint, mintSource), [], mint);
7195
}
7296
});
7397

@@ -84,7 +108,8 @@ test('named tools, builder output, pass-through argv and simctl text elsewhere a
84108
"runCmdBackground('xcrun', buildSimctlArgsForAddress(simulator, ['spawn', simulator.udid]));",
85109
"host.commands.run({ executable: 'log', args: ['stream'] });",
86110
"host.appleTools.run({ tool: 'simctl', args: scopeSimctlArgsForDevice(device, ['boot', id]) });",
87-
"const tools = ['simctl', 'devicectl'];",
111+
"const tools = ['devicectl', 'simctl'];",
112+
"const tool = 'simctl';",
88113
"if (args.includes('--set') || tool === 'simctl') note(tool);",
89114
].join('\n'),
90115
),

‎scripts/layering/apple-simulator-scope-policy.ts‎

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
// Catches: the two simctl scope bypasses `tsc` cannot reject. (1) A hand-built argv at an xcrun
2-
// invocation: `runXcrun` accepts only a `ScopedSimctlCommand` or a named non-simctl tool, but
3-
// the plain executors (`runCmd('xcrun', …)`, `runCmdBackground('xcrun', …)`, an
4-
// `executable: 'xcrun'` spec) take any string argv, so an inline argv there must name its tool
5-
// as a string literal other than `simctl`. (2) A cast to `ScopedSimctlArgs`,
6-
// `ScopedSimctlCommand` or `SimulatorAddress` outside the modules that mint them. Either form
7-
// runs against the default CoreSimulator set: `Invalid device` for a simulator in a scoped set,
8-
// or a different simulator with the same udid.
1+
// Catches: the simctl scope bypasses `tsc` cannot reject. `runXcrun` accepts only a
2+
// `ScopedSimctlCommand` or a named non-simctl tool, but the plain executors (`runCmd`,
3+
// `runCmdBackground`, an `executable: 'xcrun'` spec) take any string argv, so every simctl argv
4+
// reaching them must be builder output. (1) An array whose first element names simctl (the
5+
// literal, a quasi-only template, or a same-file binding of either) outside the brand mints,
6+
// however it later travels: inline, held in a variable, aliased or spread. (2) At an xcrun
7+
// invocation, an inline argv whose tool is not a string literal other than `simctl`. (3) A cast
8+
// to `ScopedSimctlArgs`, `ScopedSimctlCommand` or `SimulatorAddress` outside the brand mints.
9+
// Every form runs against the default CoreSimulator set: `Invalid device` for a simulator in a
10+
// scoped set, or a different simulator with the same udid.
911
// Evidence: #2784 (fixed by #2818): the AX snapshot bridge (`snapshot-source/host.ts`) and the
1012
// fold HID helper (`foldable/simulator-hid.ts`) built `['simctl', 'spawn', udid, ...]` from a
1113
// bare udid and lost the set; #2824 moved every call site onto `core/simctl.ts` and checked it
1214
// with a manual `git grep "'--set'"`.
13-
// Cost: 225 LOC (109 rule + 116 test).
15+
// Cost: 300 LOC (159 rule + 141 test).
1416
// Kill criterion: none enforced today; retire only by maintainer decision that scoped simulator
1517
// sets (`--ios-simulator-device-set`) are no longer supported, or when no production xcrun
16-
// invocation takes a plain string argv.
18+
// executor takes a plain string argv.
1719

1820
import { parseSync } from 'oxc-parser';
1921
import { propertyName, visitAst } from './layering-ast.ts';
@@ -30,6 +32,9 @@ const BRAND_MINTS = new Set([
3032
]);
3133
const BRANDS = new Set(['ScopedSimctlArgs', 'ScopedSimctlCommand', 'SimulatorAddress']);
3234

35+
const SIMCTL_ARGV_MESSAGE =
36+
'builds a simctl argv outside core/simctl.ts and core/tool-provider.ts; build it with ' +
37+
'buildSimctlArgsForDevice or buildSimctlArgsForAddress';
3338
const HAND_BUILT_MESSAGE =
3439
'hands xcrun an argv whose tool is not a literal non-simctl name; build a simctl argv with ' +
3540
'buildSimctlArgsForDevice or buildSimctlArgsForAddress';
@@ -53,14 +58,31 @@ export function appleSimulatorScopeViolations(
5358
const violations: LayeringViolation[] = [];
5459
for (const [file, source] of sources) {
5560
if (!isPolicedSimulatorScopeFile(file)) continue;
61+
const program = parseSync(file, source).program;
62+
const simctlBindings = simctlNameBindings(program);
63+
const isMint = BRAND_MINTS.has(file);
64+
const buildsSimctlArgv = (array: AstNode) => {
65+
const tool = firstElement(array);
66+
return (
67+
!isMint &&
68+
(namesSimctl(tool) || (tool?.type === 'Identifier' && simctlBindings.has(tool.name)))
69+
);
70+
};
5671
const report = (node: AstNode, message: string) =>
5772
violations.push({ rule: RULE, file, line: lineAt(source, node.start), message });
58-
visitAst(parseSync(file, source).program, (node) => {
73+
visitAst(program, (node) => {
74+
if (node.type === 'ArrayExpression' && buildsSimctlArgv(node)) {
75+
report(node, SIMCTL_ARGV_MESSAGE);
76+
}
5977
const argv = xcrunArgv(node);
60-
if (argv?.type === 'ArrayExpression' && !namesNonSimctlTool(argv)) {
78+
if (
79+
argv?.type === 'ArrayExpression' &&
80+
!buildsSimctlArgv(argv) &&
81+
!namesNonSimctlTool(argv)
82+
) {
6183
report(argv, HAND_BUILT_MESSAGE);
6284
}
63-
if (!BRAND_MINTS.has(file) && castsToBrand(node)) report(node, FORGED_MESSAGE);
85+
if (!isMint && castsToBrand(node)) report(node, FORGED_MESSAGE);
6486
});
6587
}
6688
return violations;
@@ -82,11 +104,39 @@ function xcrunArgv(node: AstNode): AstNode | undefined {
82104

83105
/** A spread passes an argv through like a variable; any other first element must name the tool. */
84106
function namesNonSimctlTool(argv: AstNode): boolean {
85-
const tool = (argv.elements as AstNode[])[0];
107+
const tool = firstElement(argv);
86108
if (tool?.type === 'SpreadElement') return true;
87109
return tool?.type === 'Literal' && typeof tool.value === 'string' && tool.value !== 'simctl';
88110
}
89111

112+
function firstElement(array: AstNode): AstNode | undefined {
113+
return (array.elements as (AstNode | null)[])[0] ?? undefined;
114+
}
115+
116+
/** `'simctl'` or `` `simctl` ``, the two spellings of the tool name as a constant. */
117+
function namesSimctl(node: AstNode | undefined): boolean {
118+
if (isLiteral(node, 'simctl')) return true;
119+
if (node?.type !== 'TemplateLiteral' || (node.expressions as unknown[]).length > 0) return false;
120+
const [quasi] = node.quasis as AstNode[];
121+
return (quasi?.value as { cooked?: string } | undefined)?.cooked === 'simctl';
122+
}
123+
124+
/** Names bound anywhere in the file to a constant spelling of `simctl`, whatever their scope. */
125+
function simctlNameBindings(program: unknown): Set<unknown> {
126+
const bindings = new Set<unknown>();
127+
visitAst(program, (node) => {
128+
const id = node.id as AstNode | undefined;
129+
if (
130+
node.type === 'VariableDeclarator' &&
131+
id?.type === 'Identifier' &&
132+
namesSimctl(node.init as AstNode)
133+
) {
134+
bindings.add(id.name);
135+
}
136+
});
137+
return bindings;
138+
}
139+
90140
function castsToBrand(node: AstNode): boolean {
91141
if (node.type !== 'TSAsExpression' && node.type !== 'TSTypeAssertion') return false;
92142
let names = false;

‎scripts/layering/check.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
// eager-closure-budgets gate holds at a fixed size, so a direct host-kit edge grows all seven;
4242
// host-kit reaches the runner only through `runner/host.ts`, bound in `core/runner-host.ts`.
4343
// - Over SIMCTL ARGV in production source: `tsc` holds `runXcrun` and the Apple tool port to
44-
// branded simctl argv; R79 holds the plain xcrun executors to a literal non-simctl tool name
45-
// and keeps brand casts inside `core/simctl.ts` and `core/tool-provider.ts`, so a udid never
46-
// runs outside the set that holds it.
44+
// branded simctl argv. The plain executors take any string argv, so R79 refuses every array
45+
// that names simctl first outside `core/simctl.ts` and `core/tool-provider.ts`, however it
46+
// reaches an executor, holds an inline xcrun argv to a literal non-simctl tool name, and keeps
47+
// brand casts inside those two modules, so a udid never runs outside the set that holds it.
4748
// - Over REQUEST-BOUND RUNTIME EXECUTION: facts remain the only admission authority and daemon
4849
// code cannot manufacture or repair a narrowed runtime proof (R66).
4950
// - Over CONTRACTS PRODUCTION SOURCE: contracts owns vocabulary only — host, process, and timer

0 commit comments

Comments
 (0)