tla-ts 0.0.3-dev.51ddd47 → 0.0.3-dev.596cd91
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 +12 -12
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +150 -8
- package/dist/index.d.ts +150 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +45 -19
- package/dist/run-cli.js +45 -19
- package/dist/transfer.cjs +1391 -28
- package/dist/transfer.js +1391 -28
- package/package.json +5 -1
- package/src/ast.ts +57 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1393 -226
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -18
- package/src/config.ts +254 -181
- package/src/environment.ts +473 -132
- package/src/errors.ts +217 -3
- package/src/evaluate.ts +722 -112
- package/src/exit-codes.ts +5 -4
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +122 -29
- package/src/live-check.ts +220 -102
- package/src/liveness.ts +216 -52
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +8 -3
- package/src/parallel.ts +56 -5
- package/src/parser.ts +445 -147
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1465 -411
- package/src/run-cli.ts +1179 -155
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +26 -7
- package/src/standard-modules.ts +200 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +207 -103
- package/src/symmetry.ts +180 -52
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +46 -18
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +61 -7
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +713 -180
package/src/check.ts
CHANGED
|
@@ -11,22 +11,33 @@ import { getReadVariables } from "./dependencies";
|
|
|
11
11
|
import { createEnvironment } from "./environment";
|
|
12
12
|
import {
|
|
13
13
|
getErrorExitCode,
|
|
14
|
+
isStackOverflow,
|
|
14
15
|
TlaConfigError,
|
|
15
16
|
TlaEvaluationError,
|
|
17
|
+
TlaIllegalStateError,
|
|
18
|
+
TlaIncompleteSuccessorError,
|
|
16
19
|
TlaModuleError,
|
|
20
|
+
TlaStackOverflowError,
|
|
21
|
+
getUnexpectedException,
|
|
17
22
|
UNEXPECTED_EXCEPTION,
|
|
18
23
|
} from "./errors";
|
|
19
|
-
import {
|
|
24
|
+
import type { JavaOverride } from "./java-overrides";
|
|
25
|
+
import { EXIT_STATUS, getExitCode, setExitCode, withLivenessExitCode } from "./exit-codes";
|
|
20
26
|
import {
|
|
27
|
+
describeNode,
|
|
21
28
|
describeRange,
|
|
29
|
+
createTracingEnvironment,
|
|
22
30
|
evaluate,
|
|
31
|
+
evaluateStartupDefinitions,
|
|
23
32
|
expectBoolean,
|
|
24
33
|
formatRange,
|
|
25
34
|
getDefinition,
|
|
26
35
|
isConstantLevel,
|
|
27
36
|
isImpure,
|
|
37
|
+
type CallStack,
|
|
28
38
|
type Context,
|
|
29
39
|
type Environment,
|
|
40
|
+
type FingerprintFailure,
|
|
30
41
|
type Scope,
|
|
31
42
|
} from "./evaluate";
|
|
32
43
|
import {
|
|
@@ -48,8 +59,10 @@ import {
|
|
|
48
59
|
import {
|
|
49
60
|
getActionLabel,
|
|
50
61
|
getActionSignature,
|
|
62
|
+
getInitActions,
|
|
51
63
|
getNextActions,
|
|
52
64
|
getSpecificationValue,
|
|
65
|
+
toActionValue,
|
|
53
66
|
type SpecAction,
|
|
54
67
|
} from "./spec-description";
|
|
55
68
|
import {
|
|
@@ -60,20 +73,34 @@ import {
|
|
|
60
73
|
} from "./state-graph";
|
|
61
74
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
62
75
|
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
76
|
+
import {
|
|
77
|
+
formatNestedMessage,
|
|
78
|
+
getCodedMessage,
|
|
79
|
+
isEvalException,
|
|
80
|
+
MESSAGE_CODE,
|
|
81
|
+
MISSING_INIT_174,
|
|
82
|
+
} from "./tlc-messages";
|
|
83
|
+
import { replayInitialStates, replaySuccessors } from "./tlc-replay";
|
|
63
84
|
import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
|
|
64
85
|
import { createCanonicalizer } from "./canonical";
|
|
65
86
|
import { getPossibleCounts, type CheckerRuntime } from "./runtime";
|
|
66
|
-
import { createCounterExample } from "./tlc-ext";
|
|
87
|
+
import { createActionRecord, createCounterExample } from "./tlc-ext";
|
|
67
88
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
89
|
+
import type { TlcVersion } from "./tlc-version";
|
|
68
90
|
import {
|
|
69
91
|
combineHash,
|
|
92
|
+
createModelValue,
|
|
70
93
|
formatState,
|
|
71
94
|
formatValue,
|
|
95
|
+
getHeldValues,
|
|
96
|
+
getFingerprintError,
|
|
97
|
+
markFingerprinted,
|
|
72
98
|
getValueHash,
|
|
73
99
|
isFunctionValue,
|
|
74
100
|
isSameValue,
|
|
75
101
|
isUpdatedElementOf,
|
|
76
102
|
setStringOrder,
|
|
103
|
+
setValueVersion,
|
|
77
104
|
toFiniteFunction,
|
|
78
105
|
type Value,
|
|
79
106
|
} from "./values";
|
|
@@ -93,8 +120,71 @@ export interface CheckOptions {
|
|
|
93
120
|
livenessCheck?: "default" | "final";
|
|
94
121
|
// TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
|
|
95
122
|
dump?: StateDump;
|
|
123
|
+
// TLC's -tool, which changes the messages of TLC's errors.
|
|
124
|
+
isTool?: boolean;
|
|
125
|
+
// Whether TLC records a binary trace for its trace exploration spec, for which it loads Integers.
|
|
126
|
+
isTraceRecorded?: boolean;
|
|
127
|
+
// Whether the caller printed SANY's report, its warnings included, before the check.
|
|
128
|
+
isSanyReported?: boolean;
|
|
129
|
+
// False for TLC's -nowarning, which changes how TLC computes the successors of some actions.
|
|
130
|
+
isWarningEnabled?: boolean;
|
|
131
|
+
// The TLC whose output tla-ts reproduces, which changes how TLC labels an action.
|
|
132
|
+
version?: TlcVersion;
|
|
133
|
+
// Reports the points of the search at which TLC prints a message.
|
|
134
|
+
onProgress?: (progress: CheckProgress) => void;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface InitialStatesProgress {
|
|
138
|
+
kind: "initial-states-progress";
|
|
139
|
+
count: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// A check that TLC's DoInitFunctor reports as violated before it stops its replay of the initial
|
|
143
|
+
// states.
|
|
144
|
+
export interface InitialStateViolation {
|
|
145
|
+
kind: "initial-state-violation";
|
|
146
|
+
status: "invariant-violation" | "liveness-violation";
|
|
147
|
+
name: string;
|
|
148
|
+
state: Record<string, Value>;
|
|
96
149
|
}
|
|
97
150
|
|
|
151
|
+
// A line the spec prints, or what TLC reports as it replays the initial states.
|
|
152
|
+
export type OutputLine = string | InitialStatesProgress | InitialStateViolation;
|
|
153
|
+
|
|
154
|
+
// The spec is parsed, TLC warns about each invariant that is a true constant formula and each
|
|
155
|
+
// property that is a state formula, finds the branches of the temporal properties, computes the
|
|
156
|
+
// initial states, reporting each time their count reaches a power of two, reports how far the
|
|
157
|
+
// search got 3 seconds after it starts and every minute after, and checks the temporal properties
|
|
158
|
+
// on the state graph so far at those times and on the complete state graph at the end.
|
|
159
|
+
export type CheckProgress =
|
|
160
|
+
| { kind: "parsed" }
|
|
161
|
+
| { kind: "constant-level-invariant"; name: string }
|
|
162
|
+
| { kind: "state-level-property"; name: string }
|
|
163
|
+
| { kind: "liveness-initializing"; isSymmetric: boolean; isConstrained: boolean }
|
|
164
|
+
| { kind: "liveness-prepared"; branches: number }
|
|
165
|
+
| { kind: "initial-states-started" }
|
|
166
|
+
| InitialStatesProgress
|
|
167
|
+
| { kind: "initial-states-computed"; initialStates: InitialStates }
|
|
168
|
+
| { kind: "progress"; statistics: SearchStatistics }
|
|
169
|
+
| {
|
|
170
|
+
kind: "liveness-started";
|
|
171
|
+
branches: number;
|
|
172
|
+
// Whether TLC checks the complete state graph, and how many states of it each branch has.
|
|
173
|
+
isComplete: boolean;
|
|
174
|
+
states: number;
|
|
175
|
+
statistics: SearchStatistics;
|
|
176
|
+
}
|
|
177
|
+
| { kind: "liveness-finished" }
|
|
178
|
+
// A warning TLC prints as it evaluates the spec, with its message code.
|
|
179
|
+
| { kind: "warning"; code: number; text: string }
|
|
180
|
+
// A method of the class named after a module, which TLC reports as it loads the class.
|
|
181
|
+
| ({ kind: "java-override" } & JavaOverride);
|
|
182
|
+
|
|
183
|
+
export interface SearchStatistics extends Pick<
|
|
184
|
+
CheckResult,
|
|
185
|
+
"generatedStates" | "distinctStates" | "queuedStates" | "depth"
|
|
186
|
+
> {}
|
|
187
|
+
|
|
98
188
|
export type CheckStatus =
|
|
99
189
|
| "success"
|
|
100
190
|
| "assumption-violation"
|
|
@@ -110,6 +200,9 @@ export interface TraceStep {
|
|
|
110
200
|
// TLC's name, parameters and location for the action that took this step.
|
|
111
201
|
label: string | undefined;
|
|
112
202
|
state: Record<string, Value>;
|
|
203
|
+
// What the spec prints, before TLC prints this step, as TLC evaluates it again to rebuild the
|
|
204
|
+
// trace. The first step has what TLC prints before the trace.
|
|
205
|
+
output?: OutputLine[];
|
|
113
206
|
}
|
|
114
207
|
|
|
115
208
|
export interface Violation {
|
|
@@ -117,25 +210,64 @@ export interface Violation {
|
|
|
117
210
|
// Every temporal property that a liveness counterexample violates, in the configuration's order.
|
|
118
211
|
properties?: string[];
|
|
119
212
|
trace: TraceStep[];
|
|
213
|
+
// What the spec prints after TLC prints the states of the trace, as TLC finds the step back into
|
|
214
|
+
// the cycle of a lasso or rebuilds the trace once more.
|
|
215
|
+
traceOutput?: OutputLine[];
|
|
120
216
|
loopStart: number | undefined;
|
|
121
217
|
loopLabel?: string;
|
|
122
218
|
message?: string;
|
|
123
219
|
counts?: string;
|
|
124
220
|
unwitnessed?: Postcondition;
|
|
221
|
+
// A POSTCONDITION that is false or fails after the counterexample, which TLC checks as it
|
|
222
|
+
// prints one.
|
|
223
|
+
postcondition?: Postcondition;
|
|
224
|
+
// What TLC warns about after a counterexample, with its code.
|
|
225
|
+
warning?: { code: number; text: string };
|
|
125
226
|
}
|
|
126
227
|
|
|
127
228
|
interface Postcondition {
|
|
128
229
|
name: string;
|
|
230
|
+
// TLC's message code.
|
|
231
|
+
code: number;
|
|
129
232
|
message: string;
|
|
130
233
|
counts?: string;
|
|
131
234
|
}
|
|
132
235
|
|
|
133
236
|
// An evaluation that failed during the search, with the behavior up to the state TLC was working on.
|
|
134
237
|
export interface EvaluationFailure {
|
|
238
|
+
// TLC's code for the message, and its text with and without -tool.
|
|
239
|
+
code: number;
|
|
135
240
|
message: string;
|
|
241
|
+
toolMessage: string;
|
|
136
242
|
trace: TraceStep[];
|
|
243
|
+
traceOutput?: OutputLine[];
|
|
137
244
|
initialState: Record<string, Value> | undefined;
|
|
138
245
|
exitCode: number;
|
|
246
|
+
// The expressions TLC was evaluating when the failure occurred, outermost first, when TLC finds
|
|
247
|
+
// them by replaying the failure.
|
|
248
|
+
callStack: Expression[] | undefined;
|
|
249
|
+
// Whether TLC prints the call stack before the error, as it does for a failed liveness check.
|
|
250
|
+
isCallStackFirst?: boolean;
|
|
251
|
+
// The FingerprintException the replay raises, which TLC reports in place of the call stack.
|
|
252
|
+
fingerprint?: FingerprintFailure;
|
|
253
|
+
// How TLC reports failing again as it prints the trace, since it can't fingerprint the state the
|
|
254
|
+
// failure occurred in.
|
|
255
|
+
traceFailure?: FailureMessage;
|
|
256
|
+
// A POSTCONDITION that is false or fails after the trace, which TLC checks as it prints one.
|
|
257
|
+
postcondition?: Postcondition;
|
|
258
|
+
// Whether TLC stops before it prints the statistics, as it does for an illegal initial state.
|
|
259
|
+
isWithoutStatistics?: boolean;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Where TLC's replay of a failure fails.
|
|
263
|
+
interface ReplayFailure {
|
|
264
|
+
callStack: Expression[];
|
|
265
|
+
fingerprint: FingerprintFailure | undefined;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export interface InitialStates {
|
|
269
|
+
generated: number;
|
|
270
|
+
distinct: number;
|
|
139
271
|
}
|
|
140
272
|
|
|
141
273
|
export interface CheckResult {
|
|
@@ -146,14 +278,55 @@ export interface CheckResult {
|
|
|
146
278
|
distinctStates: number;
|
|
147
279
|
queuedStates: number;
|
|
148
280
|
depth: number;
|
|
281
|
+
// How many initial states TLC generated and how many of them were distinct, once it computed
|
|
282
|
+
// them all without a violation or an error.
|
|
283
|
+
initialStates: InitialStates | undefined;
|
|
149
284
|
// How many states that TLC counts as expanded found each number of new states, when TLC prints
|
|
150
285
|
// the outdegree of the state graph.
|
|
151
286
|
outDegrees: number[] | undefined;
|
|
152
287
|
// The smallest distance between the fingerprints of two distinct states, when TLC estimates a
|
|
153
288
|
// collision from the actual fingerprints.
|
|
154
289
|
fingerprintDistance: bigint | undefined;
|
|
290
|
+
// A temporal property TLC finds violated as it adds a deadlocked state to the behavior graph,
|
|
291
|
+
// which it reports after the deadlock.
|
|
292
|
+
additionalViolation?: Violation;
|
|
293
|
+
// Where TLC's worker fails to add the state to the behavior graph after it reports a violation.
|
|
294
|
+
liveCallStack?: Expression[];
|
|
295
|
+
// How many states that TLC counts as expanded found each number of new states, when its workers
|
|
296
|
+
// stop on a violation without an error code, which TLC 1.7.4 reports as a successful search.
|
|
297
|
+
violationOutDegrees?: number[];
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
interface Behavior {
|
|
301
|
+
trace: TraceStep[];
|
|
302
|
+
traceOutput?: OutputLine[];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
interface TraceOptions {
|
|
306
|
+
loopStart?: number;
|
|
307
|
+
known?: number;
|
|
155
308
|
}
|
|
156
309
|
|
|
310
|
+
interface FailureOptions {
|
|
311
|
+
initialState?: Value[];
|
|
312
|
+
isInitial?: boolean;
|
|
313
|
+
outDegrees?: number[];
|
|
314
|
+
callStack?: Expression[];
|
|
315
|
+
fingerprint?: FingerprintFailure;
|
|
316
|
+
isNextState?: boolean;
|
|
317
|
+
isCallStackFirst?: boolean;
|
|
318
|
+
traceFailure?: FailureMessage;
|
|
319
|
+
// Whether the call stack is of adding the state to the behavior graph, which TLC prints instead
|
|
320
|
+
// of replaying the error.
|
|
321
|
+
isLiveCallStack?: boolean;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// TLC prints what the spec prints as it replays a failure after the trace.
|
|
325
|
+
const appendTraceOutput = <Traced extends Behavior>(behavior: Traced, lines: OutputLine[]) => ({
|
|
326
|
+
...behavior,
|
|
327
|
+
traceOutput: [...(behavior.traceOutput ?? []), ...lines],
|
|
328
|
+
});
|
|
329
|
+
|
|
157
330
|
interface NamedExpression {
|
|
158
331
|
name: string;
|
|
159
332
|
expression: Expression;
|
|
@@ -163,6 +336,8 @@ interface NamedExpression {
|
|
|
163
336
|
interface StateCheck extends NamedExpression {
|
|
164
337
|
variables: number[] | undefined;
|
|
165
338
|
membership: Membership | undefined;
|
|
339
|
+
// The whole predicate that TLC evaluates for the check, of which the expression is a conjunct.
|
|
340
|
+
predicate: Expression;
|
|
166
341
|
}
|
|
167
342
|
|
|
168
343
|
// A conjunct `x \in S` whose S reads no variables.
|
|
@@ -231,14 +406,6 @@ const getConfigOperator = (kind: string, name: string, environment: Environment)
|
|
|
231
406
|
return definition;
|
|
232
407
|
};
|
|
233
408
|
|
|
234
|
-
const getConfigDefinition = (kind: string, name: string, environment: Environment) => {
|
|
235
|
-
const definition = getConfigOperator(kind, name, environment);
|
|
236
|
-
if (isConstantLevel(definition, environment)) {
|
|
237
|
-
throw new TlaConfigError(`The ${kind} ${name} cannot be a constant.`);
|
|
238
|
-
}
|
|
239
|
-
return definition;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
409
|
// TLC evaluates every constant definition before it reads the configuration. It keeps a literal
|
|
243
410
|
// number or string, and a definition that fails to evaluate, as an operator.
|
|
244
411
|
const getConstantValue = (name: string, definition: Definition, environment: Environment) => {
|
|
@@ -247,20 +414,77 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
|
|
|
247
414
|
try {
|
|
248
415
|
return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
|
|
249
416
|
} catch (error) {
|
|
250
|
-
|
|
417
|
+
// TLC swallows whatever the definition throws, a stack overflow included.
|
|
418
|
+
if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
|
|
251
419
|
throw error;
|
|
252
420
|
}
|
|
253
421
|
};
|
|
254
422
|
|
|
255
|
-
|
|
423
|
+
// TLC takes for an operator only a definition it hasn't replaced with its value.
|
|
424
|
+
const getConfigDefinition = (kind: string, name: string, environment: Environment) => {
|
|
256
425
|
const definition = getConfigOperator(kind, name, environment);
|
|
257
|
-
|
|
426
|
+
if (getConstantValue(name, definition, environment) !== undefined) {
|
|
427
|
+
throw new TlaConfigError(`The ${kind} ${name} cannot be a constant.`);
|
|
428
|
+
}
|
|
429
|
+
return definition;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// TLC also takes a constant that the configuration assigns a value, which must be TRUE.
|
|
433
|
+
const getConfigPredicate = (kind: string, name: string, environment: Environment) => {
|
|
434
|
+
const definition = environment.constants.has(name)
|
|
435
|
+
? undefined
|
|
436
|
+
: getConfigOperator(kind, name, environment);
|
|
437
|
+
const value = definition
|
|
438
|
+
? getConstantValue(name, definition, environment)
|
|
439
|
+
: environment.constants.get(name);
|
|
258
440
|
if (value !== undefined && value !== true) {
|
|
259
441
|
throw new TlaConfigError(`The ${kind} of ${name} is equal to ${formatValue(value)}`);
|
|
260
442
|
}
|
|
261
443
|
return value === undefined ? definition : undefined;
|
|
262
444
|
};
|
|
263
445
|
|
|
446
|
+
// TLC reports how many initial states it has computed each time the count is a power of two.
|
|
447
|
+
export const reportInitialStatesProgress = (
|
|
448
|
+
count: number,
|
|
449
|
+
onProgress: ((progress: InitialStatesProgress) => void) | undefined,
|
|
450
|
+
) => {
|
|
451
|
+
if (count > 1 && (count & (count - 1)) === 0) {
|
|
452
|
+
onProgress?.({ kind: "initial-states-progress", count });
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
// What TLC does once SANY is done: it computes the constant definitions, then warns about each
|
|
457
|
+
// invariant that is a true constant formula, which it doesn't check. TLC prints each distinct
|
|
458
|
+
// warning once.
|
|
459
|
+
export const reportStartup = (
|
|
460
|
+
specification: {
|
|
461
|
+
environment: Environment;
|
|
462
|
+
startupDefinitions: Definition[];
|
|
463
|
+
javaOverrides: JavaOverride[];
|
|
464
|
+
},
|
|
465
|
+
config: ModelConfig,
|
|
466
|
+
onProgress: CheckOptions["onProgress"],
|
|
467
|
+
) => {
|
|
468
|
+
const environment = specification.environment;
|
|
469
|
+
const warned = new Set<string>();
|
|
470
|
+
environment.runtime.warn = (code, text) => {
|
|
471
|
+
if (warned.has(text)) return;
|
|
472
|
+
warned.add(text);
|
|
473
|
+
onProgress?.({ kind: "warning", code, text });
|
|
474
|
+
};
|
|
475
|
+
onProgress?.({ kind: "parsed" });
|
|
476
|
+
// TLC loads the classes of the modules before it computes the constant definitions.
|
|
477
|
+
specification.javaOverrides.forEach((override) =>
|
|
478
|
+
onProgress?.({ kind: "java-override", ...override }),
|
|
479
|
+
);
|
|
480
|
+
evaluateStartupDefinitions(specification.startupDefinitions, environment);
|
|
481
|
+
config.invariants.forEach((name) => {
|
|
482
|
+
if (!getConfigPredicate("invariant", name, environment)) {
|
|
483
|
+
onProgress?.({ kind: "constant-level-invariant", name });
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
};
|
|
487
|
+
|
|
264
488
|
const getInvariants = (names: string[], environment: Environment) => {
|
|
265
489
|
const analyzer = createLevelAnalyzer({
|
|
266
490
|
constants: new Set(environment.constants.keys()),
|
|
@@ -276,8 +500,11 @@ const getInvariants = (names: string[], environment: Environment) => {
|
|
|
276
500
|
levels.params.size === 0
|
|
277
501
|
? ""
|
|
278
502
|
: "\nNote that a bug can cause TLC to incorrectly report this error.\nIf you believe your TLA+ or PlusCal specification to be correct,\nplease check if this bug described in LevelNode.java starting at line 590ff affects you.";
|
|
279
|
-
throw
|
|
280
|
-
|
|
503
|
+
throw setExitCode(
|
|
504
|
+
new TlaConfigError(
|
|
505
|
+
`The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
|
|
506
|
+
),
|
|
507
|
+
EXIT_STATUS.safetyEvaluationFailure,
|
|
281
508
|
);
|
|
282
509
|
}
|
|
283
510
|
return [{ name, expression: createIdentifier(name), scope: undefined }];
|
|
@@ -330,14 +557,8 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
330
557
|
if (config.specification === undefined) {
|
|
331
558
|
const getOperator = (name: string | undefined, description: string) => {
|
|
332
559
|
if (name === undefined) return undefined;
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
// TLC evaluates a literal body later and reports that it is not a boolean.
|
|
336
|
-
const isLiteral = body.kind === "Number" || body.kind === "String";
|
|
337
|
-
if (!isLiteral && getLevel(identifier, undefined, environment) === 0) {
|
|
338
|
-
throw new TlaConfigError(`The ${description} ${name} cannot be a constant.`);
|
|
339
|
-
}
|
|
340
|
-
return identifier;
|
|
560
|
+
getConfigDefinition(description, name, environment);
|
|
561
|
+
return createIdentifier(name);
|
|
341
562
|
};
|
|
342
563
|
return {
|
|
343
564
|
init: getOperator(config.init, "initial predicate"),
|
|
@@ -346,6 +567,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
346
567
|
temporals: [],
|
|
347
568
|
};
|
|
348
569
|
}
|
|
570
|
+
if (config.init !== undefined || config.next !== undefined) {
|
|
571
|
+
throw new TlaConfigError(
|
|
572
|
+
"The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.",
|
|
573
|
+
);
|
|
574
|
+
}
|
|
349
575
|
const initConjuncts: ScopedExpression[] = [];
|
|
350
576
|
const nextActions: ScopedExpression[] = [];
|
|
351
577
|
const temporals: ScopedExpression[] = [];
|
|
@@ -375,8 +601,12 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
375
601
|
conjunct.kind === "Always" &&
|
|
376
602
|
getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
|
|
377
603
|
) {
|
|
378
|
-
|
|
379
|
-
|
|
604
|
+
// TLC accepts the spec and fails when it builds the tableau of the temporal formulas.
|
|
605
|
+
throw setExitCode(
|
|
606
|
+
new TlaEvaluationError(
|
|
607
|
+
`TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
|
|
608
|
+
),
|
|
609
|
+
EXIT_STATUS.error,
|
|
380
610
|
);
|
|
381
611
|
}
|
|
382
612
|
const level = getLevel(conjunct, item.scope, environment);
|
|
@@ -436,7 +666,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
436
666
|
};
|
|
437
667
|
};
|
|
438
668
|
|
|
439
|
-
const getProperties = (
|
|
669
|
+
const getProperties = (
|
|
670
|
+
config: ModelConfig,
|
|
671
|
+
environment: Environment,
|
|
672
|
+
onProgress: CheckOptions["onProgress"],
|
|
673
|
+
): Properties => {
|
|
440
674
|
const properties: Properties = {
|
|
441
675
|
initialPredicates: [],
|
|
442
676
|
invariants: [],
|
|
@@ -446,12 +680,21 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
446
680
|
config.properties.forEach((name) => {
|
|
447
681
|
const definition = getConfigPredicate("property", name, environment);
|
|
448
682
|
if (!definition) return;
|
|
449
|
-
|
|
683
|
+
// TLC checks a property that is a state formula only on the initial states, so it warns about
|
|
684
|
+
// one as it processes the property, before the constraints.
|
|
685
|
+
if (getLevel(definition.body, undefined, environment) === 1) {
|
|
686
|
+
onProgress?.({ kind: "state-level-property", name });
|
|
687
|
+
}
|
|
688
|
+
// TLC 1.7.4 expands a property as it does a specification, so it doesn't unroll one that
|
|
689
|
+
// quantifies over a constant set.
|
|
690
|
+
const isProperty = environment.runtime.version !== "1.7.4";
|
|
691
|
+
expandTemporal(definition.body, undefined, environment, isProperty).forEach((item) => {
|
|
450
692
|
const conjunct = item.expression;
|
|
451
693
|
if (isConstantTrue(item, environment, "property")) return;
|
|
694
|
+
const partName = item.name ?? name;
|
|
452
695
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
453
696
|
properties.actionProperties.push({
|
|
454
|
-
name,
|
|
697
|
+
name: partName,
|
|
455
698
|
box: conjunct.expression,
|
|
456
699
|
action: conjunct.expression.action,
|
|
457
700
|
subscript: conjunct.expression.subscript,
|
|
@@ -463,12 +706,28 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
463
706
|
conjunct.kind === "Always" &&
|
|
464
707
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
465
708
|
) {
|
|
466
|
-
|
|
709
|
+
// TLC names the invariant by the operator the property asserts always holds, when it
|
|
710
|
+
// applies the operator to no arguments. The name of an operator another module defines
|
|
711
|
+
// resolves to its key, so the name comes from its definition.
|
|
712
|
+
const body = conjunct.expression;
|
|
713
|
+
const operator =
|
|
714
|
+
body.kind === "Identifier" && !isLocallyResolved(body)
|
|
715
|
+
? getDefinition(body.name, environment)
|
|
716
|
+
: undefined;
|
|
717
|
+
properties.invariants.push({
|
|
718
|
+
name: operator?.name ?? (body.kind === "Identifier" ? body.name : partName),
|
|
719
|
+
expression: body,
|
|
720
|
+
scope: item.scope,
|
|
721
|
+
});
|
|
467
722
|
return;
|
|
468
723
|
}
|
|
469
724
|
const level = getLevel(conjunct, item.scope, environment);
|
|
470
725
|
if (level < ACTION_LEVEL) {
|
|
471
|
-
properties.initialPredicates.push({
|
|
726
|
+
properties.initialPredicates.push({
|
|
727
|
+
name: partName,
|
|
728
|
+
expression: conjunct,
|
|
729
|
+
scope: item.scope,
|
|
730
|
+
});
|
|
472
731
|
return;
|
|
473
732
|
}
|
|
474
733
|
if (level === ACTION_LEVEL) {
|
|
@@ -525,6 +784,17 @@ const getMembership = (
|
|
|
525
784
|
: undefined;
|
|
526
785
|
};
|
|
527
786
|
|
|
787
|
+
// TLC evaluates the body of an invariant that the configuration names.
|
|
788
|
+
const getPredicate = (expression: Expression, environment: Environment) => {
|
|
789
|
+
if (expression.kind !== "Identifier" || environment.constants.has(expression.name)) {
|
|
790
|
+
return expression;
|
|
791
|
+
}
|
|
792
|
+
const definition = getDefinition(expression.name, environment);
|
|
793
|
+
return definition?.kind === "Operator" && definition.params.length === 0
|
|
794
|
+
? definition.body
|
|
795
|
+
: expression;
|
|
796
|
+
};
|
|
797
|
+
|
|
528
798
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
529
799
|
named.flatMap((item) =>
|
|
530
800
|
getConjuncts(item.expression, environment).map((expression) => {
|
|
@@ -537,6 +807,7 @@ const getStateChecks = (named: NamedExpression[], environment: Environment): Sta
|
|
|
537
807
|
scope: item.scope,
|
|
538
808
|
variables,
|
|
539
809
|
membership: getMembership(expression, variables, environment),
|
|
810
|
+
predicate: getPredicate(item.expression, environment),
|
|
540
811
|
};
|
|
541
812
|
}),
|
|
542
813
|
);
|
|
@@ -585,7 +856,25 @@ interface Search {
|
|
|
585
856
|
getStoredFingerprints: (states: Value[][], fingerprintStates: Value[][]) => BigInt64Array;
|
|
586
857
|
getTlcOrder: () => number[];
|
|
587
858
|
isSymmetric: boolean;
|
|
859
|
+
// TLC drops a constraint that is a true constant formula.
|
|
860
|
+
isConstrained: boolean;
|
|
588
861
|
findViolatedInvariant: (state: Value[], parent?: Value[]) => StateCheck | undefined;
|
|
862
|
+
// The checks of a state that TLC evaluates when it replays the computation of the initial states
|
|
863
|
+
// or of the successors of a state, with an environment that records the call stack. The first
|
|
864
|
+
// returns the check that stops the replay, and the second false where TLC stops it.
|
|
865
|
+
// TLC fingerprints a replayed state that satisfies the constraints before it checks the rest.
|
|
866
|
+
checkReplayedInitialState: (
|
|
867
|
+
state: Value[],
|
|
868
|
+
tracing: Environment,
|
|
869
|
+
addFingerprint: () => void,
|
|
870
|
+
) => Pick<InitialStateViolation, "status" | "name"> | undefined;
|
|
871
|
+
// Adding the fingerprint of a successor tells whether TLC had not seen it yet.
|
|
872
|
+
checkReplayedSuccessor: (
|
|
873
|
+
state: Value[],
|
|
874
|
+
next: Value[],
|
|
875
|
+
tracing: Environment,
|
|
876
|
+
addFingerprint: () => boolean,
|
|
877
|
+
) => boolean;
|
|
589
878
|
satisfiesConstraints: (state: Value[], parent?: Value[]) => boolean;
|
|
590
879
|
findFailedInitialPredicate: (state: Value[]) => NamedExpression | undefined;
|
|
591
880
|
findViolatedActionProperty: (state: Value[], next: Value[]) => ActionProperty | undefined;
|
|
@@ -593,7 +882,8 @@ interface Search {
|
|
|
593
882
|
trackStatePossible: (state: Value[]) => void;
|
|
594
883
|
trackActionPossible: (state: Value[], next: Value[]) => void;
|
|
595
884
|
findUnwitnessedPossible: () => Postcondition | undefined;
|
|
596
|
-
|
|
885
|
+
// After a counterexample, TLC skips the postconditions when one of them is constant.
|
|
886
|
+
findPostconditionViolation: (isAfterViolation?: boolean) => Postcondition | undefined;
|
|
597
887
|
}
|
|
598
888
|
|
|
599
889
|
const getContext = (
|
|
@@ -619,21 +909,23 @@ const holdsLikeTlc = (
|
|
|
619
909
|
return value;
|
|
620
910
|
};
|
|
621
911
|
|
|
622
|
-
// SANY prints a number by its digits and any other expression by its location.
|
|
623
912
|
const getExpectedBoolean = (expression: Expression, environment: Environment) => {
|
|
624
913
|
const definition =
|
|
625
914
|
expression.kind === "Identifier" ? getDefinition(expression.name, environment) : undefined;
|
|
626
915
|
const body = definition?.kind === "Operator" ? definition.body : expression;
|
|
627
916
|
const range = definition?.kind === "Operator" ? definition.bodyRange : undefined;
|
|
628
|
-
const location =
|
|
629
|
-
body.kind === "Number"
|
|
630
|
-
? (body.text ?? String(body.value))
|
|
631
|
-
: range
|
|
632
|
-
? formatRange(range)
|
|
633
|
-
: describeRange(body);
|
|
917
|
+
const location = range && body.kind !== "Number" ? formatRange(range) : describeNode(body);
|
|
634
918
|
return `TLC expected a boolean value, but did not find one. ${location}`;
|
|
635
919
|
};
|
|
636
920
|
|
|
921
|
+
// TLC evaluates the whole predicate of a check, which fails at a conjunct that isn't a boolean as
|
|
922
|
+
// the conjunction does.
|
|
923
|
+
const holdsAsConjunct = (check: StateCheck, context: Context) =>
|
|
924
|
+
holdsLikeTlc(check.expression, context, (expression, environment) => {
|
|
925
|
+
holdsLikeTlc(check.predicate, context);
|
|
926
|
+
return getExpectedBoolean(expression, environment);
|
|
927
|
+
});
|
|
928
|
+
|
|
637
929
|
const ignorePossible = () => {};
|
|
638
930
|
|
|
639
931
|
const trackPossible = (items: NamedExpression[], runtime: CheckerRuntime, context: Context) =>
|
|
@@ -675,11 +967,16 @@ export const createSearch = (
|
|
|
675
967
|
config: ModelConfig,
|
|
676
968
|
environment: Environment,
|
|
677
969
|
getVariableOrder: () => string[],
|
|
970
|
+
onProgress?: CheckOptions["onProgress"],
|
|
678
971
|
): Search | undefined => {
|
|
679
972
|
const configInvariants = getInvariants(config.invariants, environment);
|
|
680
973
|
const spec = getSpecification(config, environment);
|
|
681
|
-
const properties = getProperties(config, environment);
|
|
974
|
+
const properties = getProperties(config, environment, onProgress);
|
|
682
975
|
const invariants = getStateChecks([...configInvariants, ...properties.invariants], environment);
|
|
976
|
+
// The invariants TLC evaluates, each of which may have several conjuncts among the checks.
|
|
977
|
+
const invariantPredicates = [
|
|
978
|
+
...new Map(invariants.map((invariant) => [invariant.predicate, invariant])).values(),
|
|
979
|
+
];
|
|
683
980
|
const isCheckingBehaviors =
|
|
684
981
|
invariants.length > 0 ||
|
|
685
982
|
properties.actionProperties.length > 0 ||
|
|
@@ -690,7 +987,11 @@ export const createSearch = (
|
|
|
690
987
|
properties.initialPredicates.length > 0 ||
|
|
691
988
|
environment.variables.length > 0)
|
|
692
989
|
) {
|
|
693
|
-
throw new TlaConfigError(
|
|
990
|
+
throw new TlaConfigError(
|
|
991
|
+
environment.runtime.version === "1.7.4"
|
|
992
|
+
? MISSING_INIT_174
|
|
993
|
+
: "The configuration file did not specify the initial state predicate.",
|
|
994
|
+
);
|
|
694
995
|
}
|
|
695
996
|
if (spec.next === undefined && isCheckingBehaviors) {
|
|
696
997
|
throw new TlaConfigError("The configuration file did not specify the next state predicate.");
|
|
@@ -709,7 +1010,9 @@ export const createSearch = (
|
|
|
709
1010
|
const possible = getPossiblePredicates(config.possible, environment);
|
|
710
1011
|
if (environment.variables.length === 0) return undefined;
|
|
711
1012
|
const symmetryGroup =
|
|
712
|
-
config.symmetry === undefined
|
|
1013
|
+
config.symmetry === undefined
|
|
1014
|
+
? undefined
|
|
1015
|
+
: getSymmetryGroup(config.symmetry, environment, config.substitutions);
|
|
713
1016
|
if (config.view !== undefined) getConfigOperator("view function", config.view, environment);
|
|
714
1017
|
const view =
|
|
715
1018
|
config.view === undefined
|
|
@@ -741,14 +1044,12 @@ export const createSearch = (
|
|
|
741
1044
|
.sort((left, right) => getTlcPosition(left) - getTlcPosition(right));
|
|
742
1045
|
return tlcOrder.indices;
|
|
743
1046
|
};
|
|
744
|
-
// A representative in TLC's variable order is the one whose fingerprint TLC reports.
|
|
745
|
-
const order = symmetryGroup.length > 0 ? getTlcOrder() : undefined;
|
|
746
1047
|
const getViewValue = (expression: Expression, state: Value[]) =>
|
|
747
1048
|
evaluate(expression, getContext(environment, state, undefined));
|
|
748
1049
|
const getTlcStateFingerprint = (state: Value[]) => {
|
|
749
1050
|
const indices = getTlcOrder();
|
|
750
1051
|
const representative =
|
|
751
|
-
symmetryGroup
|
|
1052
|
+
symmetryGroup === undefined ? state : getRepresentative(state, symmetryGroup, indices);
|
|
752
1053
|
return getTlcFingerprint(
|
|
753
1054
|
view === undefined
|
|
754
1055
|
? indices.map((index) => representative[index])
|
|
@@ -764,6 +1065,7 @@ export const createSearch = (
|
|
|
764
1065
|
const location = range ? ` at ${formatRange(range)}` : "";
|
|
765
1066
|
return {
|
|
766
1067
|
name: unwitnessed.name,
|
|
1068
|
+
code: MESSAGE_CODE.possibleUnwitnessed,
|
|
767
1069
|
message: `The _POSSIBLE predicate ${unwitnessed.name}${location} was never witnessed.\nOthers may be unwitnessed too; only witnessed predicates appear with a\nnon-zero count in the record printed above.\nSee https://explain.tlapl.us/possible-conditions for additional details.`,
|
|
768
1070
|
counts: prettyPrint(formatValue(getPossibleCounts(runtime), true)),
|
|
769
1071
|
};
|
|
@@ -771,22 +1073,28 @@ export const createSearch = (
|
|
|
771
1073
|
return {
|
|
772
1074
|
properties,
|
|
773
1075
|
spec,
|
|
774
|
-
isSymmetric: symmetryGroup
|
|
1076
|
+
isSymmetric: symmetryGroup !== undefined,
|
|
1077
|
+
isConstrained: constraints.length > 0 || actionConstraints.length > 0,
|
|
775
1078
|
getTlcOrder,
|
|
776
1079
|
getStoredFingerprints: (states, fingerprintStates) => {
|
|
777
1080
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
778
1081
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
779
1082
|
}
|
|
780
|
-
const
|
|
781
|
-
const
|
|
782
|
-
|
|
783
|
-
|
|
1083
|
+
const fingerprints = new BigInt64Array(fingerprintStates.length);
|
|
1084
|
+
const fingerprint = createTlcFingerprinter(
|
|
1085
|
+
getTlcOrder(),
|
|
1086
|
+
new Int32Array(fingerprints.buffer),
|
|
784
1087
|
);
|
|
1088
|
+
fingerprintStates.forEach(fingerprint);
|
|
1089
|
+
return fingerprints;
|
|
785
1090
|
},
|
|
786
1091
|
getFingerprintState: (state) => {
|
|
787
1092
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
1093
|
+
// A representative in TLC's variable order is the one whose fingerprint TLC reports.
|
|
788
1094
|
const representative =
|
|
789
|
-
symmetryGroup
|
|
1095
|
+
symmetryGroup === undefined
|
|
1096
|
+
? state
|
|
1097
|
+
: getRepresentative(state, symmetryGroup, getTlcOrder());
|
|
790
1098
|
return view === undefined ? representative : [getViewValue(view.expression, representative)];
|
|
791
1099
|
},
|
|
792
1100
|
findViolatedInvariant: (state, parent) =>
|
|
@@ -802,21 +1110,63 @@ export const createSearch = (
|
|
|
802
1110
|
parent[membership.variable],
|
|
803
1111
|
evaluate(membership.set, context),
|
|
804
1112
|
)
|
|
805
|
-
: !
|
|
1113
|
+
: !holdsAsConjunct(invariant, context);
|
|
806
1114
|
} catch (error) {
|
|
807
1115
|
// TLC reports a failure only on a state after the initial states as an invariant failure.
|
|
808
1116
|
if (!parent) throw error;
|
|
809
1117
|
throw withHeadline(
|
|
810
1118
|
setExitCode(error, EXIT_STATUS.safetyEvaluationFailure),
|
|
1119
|
+
MESSAGE_CODE.invariantEvaluationFailed,
|
|
811
1120
|
`Evaluating invariant ${invariant.name} failed.`,
|
|
812
1121
|
);
|
|
813
1122
|
}
|
|
814
1123
|
}),
|
|
1124
|
+
checkReplayedInitialState: (state, tracing, addFingerprint) => {
|
|
1125
|
+
const holdsIn = (expression: Expression, scope: Scope | undefined) =>
|
|
1126
|
+
holdsLikeTlc(expression, getContext(tracing, state, undefined, scope));
|
|
1127
|
+
if (constraints.every((constraint) => holdsIn(constraint.predicate, constraint.scope))) {
|
|
1128
|
+
addFingerprint();
|
|
1129
|
+
}
|
|
1130
|
+
const invariant = invariantPredicates.find((check) => !holdsIn(check.predicate, check.scope));
|
|
1131
|
+
if (invariant) return { status: "invariant-violation", name: invariant.name };
|
|
1132
|
+
const predicate = properties.initialPredicates.find(
|
|
1133
|
+
(check) => !holdsIn(check.expression, check.scope),
|
|
1134
|
+
);
|
|
1135
|
+
return predicate && { status: "liveness-violation", name: predicate.name };
|
|
1136
|
+
},
|
|
1137
|
+
checkReplayedSuccessor: (state, next, tracing, addFingerprint) => {
|
|
1138
|
+
// TLC evaluates the action constraints only on a state that satisfies the constraints.
|
|
1139
|
+
const isInModel =
|
|
1140
|
+
constraints.every((constraint) =>
|
|
1141
|
+
holdsLikeTlc(
|
|
1142
|
+
constraint.predicate,
|
|
1143
|
+
getContext(tracing, next, undefined, constraint.scope),
|
|
1144
|
+
),
|
|
1145
|
+
) &&
|
|
1146
|
+
actionConstraints.every((constraint) =>
|
|
1147
|
+
holdsLikeTlc(constraint.expression, getContext(tracing, state, next, constraint.scope)),
|
|
1148
|
+
);
|
|
1149
|
+
// ModelChecker.doNext checks the invariants of a successor it has not seen, which includes
|
|
1150
|
+
// one outside the model.
|
|
1151
|
+
const isUnseen = isInModel ? addFingerprint() : true;
|
|
1152
|
+
return (
|
|
1153
|
+
(!isUnseen ||
|
|
1154
|
+
invariantPredicates.every((invariant) =>
|
|
1155
|
+
holdsLikeTlc(
|
|
1156
|
+
invariant.predicate,
|
|
1157
|
+
getContext(tracing, next, undefined, invariant.scope),
|
|
1158
|
+
),
|
|
1159
|
+
)) &&
|
|
1160
|
+
actionProperties.every((property) =>
|
|
1161
|
+
holdsLikeTlc(property.box, getContext(tracing, state, next, property.scope)),
|
|
1162
|
+
)
|
|
1163
|
+
);
|
|
1164
|
+
},
|
|
815
1165
|
satisfiesConstraints: (state, parent) =>
|
|
816
1166
|
constraints.every(
|
|
817
1167
|
(constraint) =>
|
|
818
1168
|
isUnchangedFor(constraint, state, parent) ||
|
|
819
|
-
|
|
1169
|
+
holdsAsConjunct(constraint, getContext(environment, state, undefined)),
|
|
820
1170
|
),
|
|
821
1171
|
findFailedInitialPredicate: (state) =>
|
|
822
1172
|
properties.initialPredicates.find(
|
|
@@ -845,6 +1195,7 @@ export const createSearch = (
|
|
|
845
1195
|
} catch (error) {
|
|
846
1196
|
throw withHeadline(
|
|
847
1197
|
withNextStateExitCode(error),
|
|
1198
|
+
MESSAGE_CODE.actionPropertyEvaluationFailed,
|
|
848
1199
|
`Evaluating action property ${property.name} failed.`,
|
|
849
1200
|
);
|
|
850
1201
|
}
|
|
@@ -868,14 +1219,40 @@ export const createSearch = (
|
|
|
868
1219
|
: (state, next) =>
|
|
869
1220
|
trackPossible(actionPossible, runtime, getContext(environment, state, next)),
|
|
870
1221
|
findUnwitnessedPossible,
|
|
871
|
-
findPostconditionViolation: () => {
|
|
1222
|
+
findPostconditionViolation: (isAfterViolation = false) => {
|
|
1223
|
+
// TLC replaces a definition it computes when it starts by its value, and then rejects it as
|
|
1224
|
+
// a postcondition before it checks any of them.
|
|
1225
|
+
const constant = config.postconditions.find((name) => {
|
|
1226
|
+
const definition = getDefinition(name, environment);
|
|
1227
|
+
return definition !== undefined && environment.definitionValues.has(definition);
|
|
1228
|
+
});
|
|
1229
|
+
if (constant !== undefined) {
|
|
1230
|
+
if (isAfterViolation) return undefined;
|
|
1231
|
+
throw setExitCode(
|
|
1232
|
+
new TlaEvaluationError(
|
|
1233
|
+
`${UNEXPECTED_EXCEPTION}The post condition ${constant} cannot be a constant.`,
|
|
1234
|
+
),
|
|
1235
|
+
EXIT_STATUS.error,
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
872
1238
|
const unwitnessed = findUnwitnessedPossible();
|
|
873
1239
|
if (unwitnessed) return unwitnessed;
|
|
874
1240
|
for (const name of config.postconditions) {
|
|
875
1241
|
const failure = getConstantFailure(createIdentifier(name), environment, "a postcondition");
|
|
876
|
-
if (failure)
|
|
877
|
-
|
|
878
|
-
}
|
|
1242
|
+
if (!failure) continue;
|
|
1243
|
+
const range = getBodyRange(name, environment);
|
|
1244
|
+
const location = range ? ` at ${formatRange(range)}` : "";
|
|
1245
|
+
return failure.message === undefined
|
|
1246
|
+
? {
|
|
1247
|
+
name,
|
|
1248
|
+
code: MESSAGE_CODE.postconditionFalse,
|
|
1249
|
+
message: `Postcondition ${name}${location} is false.`,
|
|
1250
|
+
}
|
|
1251
|
+
: {
|
|
1252
|
+
name,
|
|
1253
|
+
code: MESSAGE_CODE.postconditionFailed,
|
|
1254
|
+
message: `Evaluating postcondition ${name}${location} failed.\n${failure.message}`,
|
|
1255
|
+
};
|
|
879
1256
|
}
|
|
880
1257
|
return undefined;
|
|
881
1258
|
},
|
|
@@ -887,38 +1264,179 @@ export const createSearch = (
|
|
|
887
1264
|
const withNextStateExitCode = (error: unknown) =>
|
|
888
1265
|
error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
|
|
889
1266
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1267
|
+
// Stops a replay where TLC stops it without a failure, as at a violated invariant.
|
|
1268
|
+
const STOP_REPLAY = new Error("The replay stopped");
|
|
1269
|
+
|
|
1270
|
+
// The codes of the errors in the configuration and the spec that TLC's EC.ExitStatus maps to a
|
|
1271
|
+
// configuration error.
|
|
1272
|
+
const CONFIG_ERROR_CODES = new Set([
|
|
1273
|
+
...Array.from({ length: 24 }, (_, index) => 2222 + index),
|
|
1274
|
+
2272,
|
|
1275
|
+
2273,
|
|
1276
|
+
2274,
|
|
1277
|
+
2280,
|
|
1278
|
+
2281,
|
|
1279
|
+
]);
|
|
1280
|
+
|
|
1281
|
+
// When TLC's replay of the initial states doesn't fail again, it exits with the code of the
|
|
1282
|
+
// TLCRuntimeException it reported.
|
|
1283
|
+
const getInitialExitCode = (error: TlaEvaluationError) => {
|
|
1284
|
+
const coded = getCodedMessage(error.message);
|
|
1285
|
+
return coded && !coded.isEvalException && CONFIG_ERROR_CODES.has(coded.code)
|
|
1286
|
+
? EXIT_STATUS.configParseError
|
|
1287
|
+
: getErrorExitCode(error);
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
// When TLC's replay of an error fails again, an EvalException from a standard module's Java code
|
|
1291
|
+
// decides the exit status, and any other failure exits as a nested expression failure. An error
|
|
1292
|
+
// that heads or wraps another exits as the other.
|
|
1293
|
+
const getReplayExitCode = (error: TlaEvaluationError): number => {
|
|
1294
|
+
const inner = error.cause;
|
|
1295
|
+
if (inner instanceof TlaEvaluationError) return getReplayExitCode(inner);
|
|
1296
|
+
return error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.specEvaluationFailure;
|
|
1297
|
+
};
|
|
1298
|
+
|
|
1299
|
+
// An error that TLC reports with a message of its own, by that message's code and first line, and
|
|
1300
|
+
// the error whose message follows.
|
|
1301
|
+
interface HeadedError {
|
|
1302
|
+
code: number;
|
|
1303
|
+
headline: string;
|
|
1304
|
+
inner: TlaEvaluationError;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
const headedErrors = new WeakMap<Error, HeadedError>();
|
|
1308
|
+
|
|
1309
|
+
const withHeadline = (error: unknown, code: number, headline: string) => {
|
|
1310
|
+
if (!(error instanceof TlaEvaluationError)) return error;
|
|
1311
|
+
const headed = setExitCode(
|
|
1312
|
+
new TlaEvaluationError(`${headline}\n${error.message}`, error),
|
|
1313
|
+
getErrorExitCode(error),
|
|
1314
|
+
);
|
|
1315
|
+
headedErrors.set(headed, { code, headline, inner: error });
|
|
1316
|
+
return headed;
|
|
1317
|
+
};
|
|
1318
|
+
|
|
1319
|
+
const UNEXPECTED_HEADLINE = UNEXPECTED_EXCEPTION.slice(0, UNEXPECTED_EXCEPTION.indexOf("\n"));
|
|
1320
|
+
|
|
1321
|
+
// TLC 1.7.4's Worker wraps an error in adding a successor in a RuntimeException, whose message
|
|
1322
|
+
// names the class of the error.
|
|
1323
|
+
const WRAPPED_EVAL_EXCEPTION = `${UNEXPECTED_EXCEPTION}tlc2.tool.EvalException: `;
|
|
1324
|
+
const WRAPPED_RUNTIME_EXCEPTION = `${UNEXPECTED_EXCEPTION}util.Assert$TLCRuntimeException: `;
|
|
1325
|
+
|
|
1326
|
+
const UNEXPECTED_EVAL_EXCEPTION = getUnexpectedException("tlc2.tool.EvalException");
|
|
1327
|
+
|
|
1328
|
+
const getWrappedException = (error: TlaEvaluationError) =>
|
|
1329
|
+
isEvalException(error) ? WRAPPED_EVAL_EXCEPTION : WRAPPED_RUNTIME_EXCEPTION;
|
|
1330
|
+
|
|
1331
|
+
// TLC reports an EvalException, or a TLCRuntimeException without a message of its own, as an
|
|
1332
|
+
// exception it didn't expect.
|
|
1333
|
+
const withUnexpectedException = <Result>(compute: () => Result) => {
|
|
1334
|
+
try {
|
|
1335
|
+
return compute();
|
|
1336
|
+
} catch (error) {
|
|
1337
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1338
|
+
if (isEvalException(error)) {
|
|
1339
|
+
throw new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`);
|
|
1340
|
+
}
|
|
1341
|
+
if (getCodedMessage(error.message)) throw error;
|
|
1342
|
+
throw new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`);
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
// The starts of TLC's messages for the exceptions it didn't expect.
|
|
1347
|
+
const UNEXPECTED_EXCEPTIONS = [
|
|
1348
|
+
WRAPPED_EVAL_EXCEPTION,
|
|
1349
|
+
WRAPPED_RUNTIME_EXCEPTION,
|
|
1350
|
+
UNEXPECTED_EXCEPTION,
|
|
1351
|
+
UNEXPECTED_EVAL_EXCEPTION,
|
|
1352
|
+
];
|
|
1353
|
+
|
|
1354
|
+
// The text of an error's message, with the message of a coded error inside it marked in -tool
|
|
1355
|
+
// output as TLC marks it.
|
|
1356
|
+
const formatErrorMessage = (error: TlaEvaluationError, isTool: boolean): string => {
|
|
1357
|
+
const headed = headedErrors.get(error);
|
|
1358
|
+
if (headed) return `${headed.headline}\n${formatErrorMessage(headed.inner, isTool)}`;
|
|
1359
|
+
const message = error.message;
|
|
1360
|
+
const prefix = UNEXPECTED_EXCEPTIONS.find((start) => message.startsWith(start)) ?? "";
|
|
1361
|
+
return `${prefix}${formatNestedMessage(message.slice(prefix.length), isTool)}`;
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
// How TLC reports an error: with the code of its own message for an error that it reports with a
|
|
1365
|
+
// headline; and, when the error escapes the computation of next states, with the code of an
|
|
1366
|
+
// EvalException, or as an unexpected exception for any other error.
|
|
1367
|
+
interface FailureMessage {
|
|
1368
|
+
code: number;
|
|
1369
|
+
message: string;
|
|
1370
|
+
toolMessage: string;
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
const getFailureMessage = (error: TlaEvaluationError, isNextState: boolean): FailureMessage => {
|
|
1374
|
+
const headed = headedErrors.get(error);
|
|
1375
|
+
const message = error.message;
|
|
1376
|
+
if (headed) {
|
|
1377
|
+
return {
|
|
1378
|
+
code: headed.code,
|
|
1379
|
+
message,
|
|
1380
|
+
toolMessage: formatErrorMessage(error, true),
|
|
1381
|
+
};
|
|
1382
|
+
}
|
|
1383
|
+
if (isNextState && isEvalException(error)) {
|
|
1384
|
+
return {
|
|
1385
|
+
code: getCodedMessage(message)?.code ?? MESSAGE_CODE.general,
|
|
1386
|
+
message,
|
|
1387
|
+
toolMessage: message,
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
1390
|
+
const isUnexpected = isNextState && !message.startsWith(UNEXPECTED_HEADLINE);
|
|
1391
|
+
const prefix = isUnexpected ? UNEXPECTED_EXCEPTION : "";
|
|
1392
|
+
return {
|
|
1393
|
+
code: MESSAGE_CODE.general,
|
|
1394
|
+
message: `${prefix}${message}`,
|
|
1395
|
+
toolMessage: `${prefix}${formatErrorMessage(error, true)}`,
|
|
1396
|
+
};
|
|
1397
|
+
};
|
|
897
1398
|
|
|
898
1399
|
// TLC finishes the -dump file whether or not the search finds a violation.
|
|
899
1400
|
export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
900
|
-
const stateWriter =
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
1401
|
+
const stateWriter =
|
|
1402
|
+
options.dump && createStateWriter(options.dump, options.writeFile, options.version);
|
|
1403
|
+
try {
|
|
1404
|
+
return searchStates(options, stateWriter);
|
|
1405
|
+
} catch (error) {
|
|
1406
|
+
// TLC reports a stack overflow outside the search on its own, and exits with 255.
|
|
1407
|
+
throw isStackOverflow(error) ? createStackOverflowError() : error;
|
|
1408
|
+
} finally {
|
|
1409
|
+
stateWriter?.close();
|
|
1410
|
+
}
|
|
904
1411
|
};
|
|
905
1412
|
|
|
1413
|
+
const createStackOverflowError = () => setExitCode(new TlaStackOverflowError(), EXIT_STATUS.error);
|
|
1414
|
+
|
|
906
1415
|
const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
|
|
907
|
-
const config =
|
|
1416
|
+
const config =
|
|
1417
|
+
typeof options.config === "string"
|
|
1418
|
+
? parseConfig(options.config, options.version)
|
|
1419
|
+
: options.config;
|
|
908
1420
|
const onPrint = options.onPrint ?? console.log;
|
|
909
|
-
// TLC prints again while it rebuilds a trace, between the states it prints
|
|
910
|
-
//
|
|
911
|
-
const output = {
|
|
1421
|
+
// TLC prints again while it rebuilds a trace, between the states it prints, so rebuilding a trace
|
|
1422
|
+
// keeps what it prints for the trace.
|
|
1423
|
+
const output = { lines: undefined as OutputLine[] | undefined };
|
|
912
1424
|
const print = (text: string) => {
|
|
913
|
-
if (
|
|
1425
|
+
if (output.lines) output.lines.push(text);
|
|
1426
|
+
else onPrint(text);
|
|
914
1427
|
};
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
const
|
|
918
|
-
output.
|
|
919
|
-
|
|
1428
|
+
const getOutput = <Result>(rebuild: () => Result) => {
|
|
1429
|
+
const outer = output.lines;
|
|
1430
|
+
const lines: OutputLine[] = [];
|
|
1431
|
+
output.lines = lines;
|
|
1432
|
+
try {
|
|
1433
|
+
return { result: rebuild(), lines };
|
|
1434
|
+
} finally {
|
|
1435
|
+
output.lines = outer;
|
|
1436
|
+
}
|
|
920
1437
|
};
|
|
921
1438
|
setStringOrder(getTlcStringOrder(options.source, config, options.loadModule));
|
|
1439
|
+
setValueVersion(options.version);
|
|
922
1440
|
const specification = createEnvironment(config, {
|
|
923
1441
|
source: options.source,
|
|
924
1442
|
loadModule: options.loadModule,
|
|
@@ -926,9 +1444,16 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
926
1444
|
print,
|
|
927
1445
|
files: { readFile: options.readFile, writeFile: options.writeFile },
|
|
928
1446
|
seed: options.seed,
|
|
1447
|
+
isTool: options.isTool,
|
|
1448
|
+
isTraceRecorded: options.isTraceRecorded,
|
|
1449
|
+
isSanyReported: options.isSanyReported,
|
|
1450
|
+
isWarningEnabled: options.isWarningEnabled,
|
|
1451
|
+
version: options.version,
|
|
929
1452
|
});
|
|
1453
|
+
reportStartup(specification, config, options.onProgress);
|
|
930
1454
|
const environment = specification.environment;
|
|
931
1455
|
const runtime = environment.runtime;
|
|
1456
|
+
const is174 = runtime.version === "1.7.4";
|
|
932
1457
|
runtime.pause = options.onPause ?? (() => {});
|
|
933
1458
|
runtime.getSpecification = () =>
|
|
934
1459
|
getSpecificationValue(
|
|
@@ -937,7 +1462,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
937
1462
|
specification.variableLocations,
|
|
938
1463
|
specification.instanceActions,
|
|
939
1464
|
);
|
|
940
|
-
const statistics = { depth: 0 };
|
|
1465
|
+
const statistics = { depth: 0, initialStates: undefined as InitialStates | undefined };
|
|
941
1466
|
const createResult = (
|
|
942
1467
|
status: CheckStatus,
|
|
943
1468
|
violation?: Omit<Violation, "loopStart"> & { loopStart?: number },
|
|
@@ -950,15 +1475,26 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
950
1475
|
distinctStates: runtime.distinctStates,
|
|
951
1476
|
queuedStates: runtime.queueSize,
|
|
952
1477
|
depth: runtime.generatedStates === 0 ? 0 : Math.max(statistics.depth, 1),
|
|
1478
|
+
initialStates: statistics.initialStates,
|
|
953
1479
|
outDegrees: undefined,
|
|
954
1480
|
fingerprintDistance: undefined,
|
|
955
1481
|
});
|
|
956
1482
|
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
957
1483
|
const expansion = { newStates: 0 };
|
|
958
1484
|
const getOutDegreeBucket = () => Math.min(expansion.newStates, OUT_DEGREE_BUCKETS - 1);
|
|
959
|
-
// TLC counts the state it was expanding when an evaluation failed.
|
|
1485
|
+
// TLC counts the state it was expanding when an evaluation failed, except TLC 1.7.4, whose
|
|
1486
|
+
// worker stops before it counts the state.
|
|
960
1487
|
const getPartialOutDegrees = () =>
|
|
961
1488
|
outDegrees.map((count, degree) => (degree === getOutDegreeBucket() ? count + 1 : count));
|
|
1489
|
+
const getFailureOutDegrees = () => (is174 ? [...outDegrees] : getPartialOutDegrees());
|
|
1490
|
+
// TLC 1.7.4 doesn't name the properties a counterexample violates, so it evaluates none of them.
|
|
1491
|
+
const getViolatedProperties = (checker: LivenessChecker, path: Value[][], cycleStart: number) =>
|
|
1492
|
+
is174 ? [] : checker.getViolatedProperties(path, cycleStart);
|
|
1493
|
+
// A violation in a successor ends the expansion of a state before TLC counts it.
|
|
1494
|
+
const withOutDegrees = (result: CheckResult, degrees = [...outDegrees]): CheckResult => ({
|
|
1495
|
+
...result,
|
|
1496
|
+
violationOutDegrees: degrees,
|
|
1497
|
+
});
|
|
962
1498
|
const createSuccess = (): CheckResult => ({
|
|
963
1499
|
...createResult("success"),
|
|
964
1500
|
outDegrees: [...outDegrees],
|
|
@@ -972,64 +1508,252 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
972
1508
|
),
|
|
973
1509
|
});
|
|
974
1510
|
|
|
975
|
-
const search = createSearch(
|
|
976
|
-
|
|
1511
|
+
const search = createSearch(
|
|
1512
|
+
config,
|
|
1513
|
+
environment,
|
|
1514
|
+
() => getTlcVariableOrder(options.source, options.loadModule),
|
|
1515
|
+
options.onProgress,
|
|
977
1516
|
);
|
|
978
|
-
const
|
|
979
|
-
|
|
980
|
-
if (!search)
|
|
1517
|
+
const findViolatedAssumption = () =>
|
|
1518
|
+
findAssumptionViolation(specification.assumptions, environment);
|
|
1519
|
+
if (!search) {
|
|
1520
|
+
runtime.isStarted = true;
|
|
1521
|
+
const assumptionViolation = findViolatedAssumption();
|
|
1522
|
+
if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
|
|
1523
|
+
// TLC computes the initial states of a spec without variables, of which there are none.
|
|
1524
|
+
statistics.initialStates = { generated: 0, distinct: 0 };
|
|
1525
|
+
options.onProgress?.({ kind: "initial-states-started" });
|
|
1526
|
+
options.onProgress?.({
|
|
1527
|
+
kind: "initial-states-computed",
|
|
1528
|
+
initialStates: statistics.initialStates,
|
|
1529
|
+
});
|
|
1530
|
+
return createSuccess();
|
|
1531
|
+
}
|
|
981
1532
|
const hasPostconditions = config.postconditions.length > 0 || config.possible.length > 0;
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1533
|
+
// The state of each step of a trace without the alias, and the action TLC finds for the step,
|
|
1534
|
+
// which CounterExample records.
|
|
1535
|
+
const traceSteps = new WeakMap<
|
|
1536
|
+
TraceStep,
|
|
1537
|
+
{ state: Record<string, Value>; action: SpecAction | undefined }
|
|
1538
|
+
>();
|
|
1539
|
+
const getStepState = (step: TraceStep) => traceSteps.get(step)?.state ?? step.state;
|
|
1540
|
+
const getStepAction = (step: TraceStep) => {
|
|
1541
|
+
const action = traceSteps.get(step)?.action;
|
|
1542
|
+
return action ? toActionValue(action) : createActionRecord(step.action);
|
|
1543
|
+
};
|
|
1544
|
+
// Worker#printTrace checks the postconditions on the CounterExample of each trace it prints, so
|
|
1545
|
+
// what they print follows the trace.
|
|
1546
|
+
const checkTracePostconditions = (
|
|
1547
|
+
trace: TraceStep[],
|
|
1548
|
+
loop: { start: number; action: Value } | undefined,
|
|
986
1549
|
) => {
|
|
987
1550
|
if (hasPostconditions) {
|
|
988
|
-
const start = violation.loopStart;
|
|
989
1551
|
runtime.counterExample = createCounterExample(
|
|
990
|
-
|
|
991
|
-
|
|
1552
|
+
trace.map(getStepState),
|
|
1553
|
+
trace.slice(1).map(getStepAction),
|
|
1554
|
+
loop,
|
|
992
1555
|
);
|
|
993
1556
|
}
|
|
994
1557
|
const unwitnessed = search.findUnwitnessedPossible();
|
|
995
|
-
|
|
996
|
-
|
|
1558
|
+
const checked = unwitnessed
|
|
1559
|
+
? { result: undefined, lines: [] }
|
|
1560
|
+
: getOutput(() => search.findPostconditionViolation(true));
|
|
1561
|
+
return { unwitnessed, postcondition: checked.result, lines: checked.lines };
|
|
1562
|
+
};
|
|
1563
|
+
const reportViolation = (
|
|
1564
|
+
status: CheckStatus,
|
|
1565
|
+
violation: Omit<Violation, "loopStart"> & { loopStart?: number },
|
|
1566
|
+
getLoopAction?: () => Value,
|
|
1567
|
+
) => {
|
|
1568
|
+
const start = violation.loopStart;
|
|
1569
|
+
const checked = checkTracePostconditions(
|
|
1570
|
+
violation.trace,
|
|
1571
|
+
start === undefined || !getLoopAction ? undefined : { start, action: getLoopAction() },
|
|
1572
|
+
);
|
|
1573
|
+
const traceOutput = [...(violation.traceOutput ?? []), ...checked.lines];
|
|
1574
|
+
return createResult(status, {
|
|
1575
|
+
...violation,
|
|
1576
|
+
...(traceOutput.length > 0 && { traceOutput }),
|
|
1577
|
+
...(checked.unwitnessed && { unwitnessed: checked.unwitnessed }),
|
|
1578
|
+
...(checked.postcondition && { postcondition: checked.postcondition }),
|
|
1579
|
+
});
|
|
997
1580
|
};
|
|
998
1581
|
const spec = search.spec;
|
|
999
1582
|
const getFingerprintState = search.getFingerprintState;
|
|
1000
1583
|
const nextActions = { value: undefined as SpecAction[] | undefined };
|
|
1584
|
+
const getSpecNextActions = () =>
|
|
1585
|
+
(nextActions.value ??= getNextActions(config, environment, specification.instanceActions));
|
|
1586
|
+
// Where TLC's replay of a failed computation fails, or undefined when the replay completes.
|
|
1587
|
+
const getReplayFailure = (replay: (tracing: Environment) => void): ReplayFailure | undefined => {
|
|
1588
|
+
const tracing = createTracingEnvironment(environment);
|
|
1589
|
+
const callStack = tracing.callStack;
|
|
1590
|
+
try {
|
|
1591
|
+
replay(tracing);
|
|
1592
|
+
return undefined;
|
|
1593
|
+
} catch (error) {
|
|
1594
|
+
// A replay that overflows the stack again has an empty call stack.
|
|
1595
|
+
if (isStackOverflow(error)) return { callStack: [], fingerprint: undefined };
|
|
1596
|
+
if (!(error instanceof TlaEvaluationError) || !callStack) return undefined;
|
|
1597
|
+
return { callStack: callStack.expressions, fingerprint: callStack.fingerprint };
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
// The sources of the values a fingerprint fails inside, outermost first: each value fails as it
|
|
1601
|
+
// fingerprints a value it holds.
|
|
1602
|
+
const findFailedSources = (value: Value, sources: CallStack["sources"]): Expression[] => {
|
|
1603
|
+
if (typeof value !== "object") return [];
|
|
1604
|
+
const source = sources.get(value);
|
|
1605
|
+
const failed = getHeldValues(value).find((held) => getFingerprintError(held) !== undefined);
|
|
1606
|
+
return [
|
|
1607
|
+
...(source ? [source] : []),
|
|
1608
|
+
...(failed === undefined ? [] : findFailedSources(failed, sources)),
|
|
1609
|
+
];
|
|
1610
|
+
};
|
|
1611
|
+
// TLC's replay fingerprints the states it finds, and a value the replay built raises a
|
|
1612
|
+
// FingerprintException when it fails to.
|
|
1613
|
+
const fingerprintReplayed = (state: Value[], callStack: CallStack | undefined) => {
|
|
1614
|
+
for (const value of getFingerprintState(state)) {
|
|
1615
|
+
const error = getFingerprintError(value);
|
|
1616
|
+
if (!error) continue;
|
|
1617
|
+
const sources = callStack ? findFailedSources(value, callStack.sources) : [];
|
|
1618
|
+
// TLC fingerprints a state outside any evaluation, so its call stack is empty.
|
|
1619
|
+
if (callStack && sources.length > 0) {
|
|
1620
|
+
callStack.expressions.length = 0;
|
|
1621
|
+
callStack.isFrozen = true;
|
|
1622
|
+
callStack.fingerprint = { sources, reason: error.message };
|
|
1623
|
+
}
|
|
1624
|
+
throw error;
|
|
1625
|
+
}
|
|
1626
|
+
state.forEach(markFingerprinted);
|
|
1627
|
+
};
|
|
1628
|
+
// The replay adds the states it hasn't seen to TLC's set of states, which already holds the state
|
|
1629
|
+
// that failed after TLC added it.
|
|
1630
|
+
const createReplayedStates = () => {
|
|
1631
|
+
const replayed = unstoredState.fingerprint ? [unstoredState.fingerprint] : [];
|
|
1632
|
+
return (state: Value[], callStack: CallStack | undefined) => {
|
|
1633
|
+
fingerprintReplayed(state, callStack);
|
|
1634
|
+
const fingerprintState = getFingerprintState(state);
|
|
1635
|
+
const isSeen =
|
|
1636
|
+
findStateIndex(getStateHash(fingerprintState), fingerprintState) !== -1 ||
|
|
1637
|
+
replayed.some((seen) => isSameState(seen, fingerprintState));
|
|
1638
|
+
if (isSeen) return false;
|
|
1639
|
+
replayed.push(fingerprintState);
|
|
1640
|
+
runtime.distinctStates++;
|
|
1641
|
+
return true;
|
|
1642
|
+
};
|
|
1643
|
+
};
|
|
1644
|
+
// TLC's DoInitFunctor reports its progress again as it replays the initial states, and adds each
|
|
1645
|
+
// one it hasn't seen to the behavior graph. After a FingerprintException, it only counts the
|
|
1646
|
+
// states left, then throws the exception.
|
|
1647
|
+
const replayInit = () => {
|
|
1648
|
+
const replayed = { count: 0 };
|
|
1649
|
+
const deferred: TlaEvaluationError[] = [];
|
|
1650
|
+
const addReplayed = createReplayedStates();
|
|
1651
|
+
return getReplayFailure((tracing) => {
|
|
1652
|
+
replayInitialStates(
|
|
1653
|
+
getInitActions(config, environment, specification.instanceActions),
|
|
1654
|
+
tracing,
|
|
1655
|
+
(state) => {
|
|
1656
|
+
reportInitialStatesProgress(replayed.count, (progress) => output.lines?.push(progress));
|
|
1657
|
+
replayed.count++;
|
|
1658
|
+
runtime.generatedStates = replayed.count;
|
|
1659
|
+
if (deferred.length > 0) return;
|
|
1660
|
+
try {
|
|
1661
|
+
const addFingerprint = () => {
|
|
1662
|
+
if (!addReplayed(state, tracing.callStack)) return;
|
|
1663
|
+
runtime.queueSize++;
|
|
1664
|
+
liveness?.addInitialState(states.length, state, tracing);
|
|
1665
|
+
};
|
|
1666
|
+
const violation = search.checkReplayedInitialState(state, tracing, addFingerprint);
|
|
1667
|
+
if (violation) {
|
|
1668
|
+
const traced = toTrace([{ action: undefined, state }], []).trace[0].state;
|
|
1669
|
+
output.lines?.push({ kind: "initial-state-violation", ...violation, state: traced });
|
|
1670
|
+
throw STOP_REPLAY;
|
|
1671
|
+
}
|
|
1672
|
+
} catch (error) {
|
|
1673
|
+
if (!(error instanceof TlaEvaluationError) || !tracing.callStack?.fingerprint) {
|
|
1674
|
+
throw error;
|
|
1675
|
+
}
|
|
1676
|
+
deferred.push(error);
|
|
1677
|
+
}
|
|
1678
|
+
},
|
|
1679
|
+
);
|
|
1680
|
+
if (deferred.length > 0) throw deferred[0];
|
|
1681
|
+
});
|
|
1682
|
+
};
|
|
1683
|
+
// TLC replays the expansion of a state with ModelChecker.doNext, which writes each successor in
|
|
1684
|
+
// the model to the -dump state graph again.
|
|
1685
|
+
const replayNext = (index: number) =>
|
|
1686
|
+
getReplayFailure((tracing) => {
|
|
1687
|
+
const state = states[index];
|
|
1688
|
+
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1689
|
+
const addReplayed = createReplayedStates();
|
|
1690
|
+
replaySuccessors(getSpecNextActions(), state, tracing, (next, action) => {
|
|
1691
|
+
const addFingerprint = () => {
|
|
1692
|
+
const isUnseen = addReplayed(next, tracing.callStack);
|
|
1693
|
+
if (source) {
|
|
1694
|
+
stateWriter?.writeSuccessor(
|
|
1695
|
+
source,
|
|
1696
|
+
getGraphState(next, depths[index] + 1),
|
|
1697
|
+
!isUnseen,
|
|
1698
|
+
isNamingEdges
|
|
1699
|
+
? { name: action.name, signature: getActionSignature(action) }
|
|
1700
|
+
: undefined,
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
return isUnseen;
|
|
1704
|
+
};
|
|
1705
|
+
if (!search.checkReplayedSuccessor(state, next, tracing, addFingerprint)) {
|
|
1706
|
+
throw STOP_REPLAY;
|
|
1707
|
+
}
|
|
1708
|
+
});
|
|
1709
|
+
});
|
|
1001
1710
|
const isReducing = config.view !== undefined || config.symmetry !== undefined;
|
|
1002
1711
|
const tlcFingerprints: number[] = [];
|
|
1712
|
+
// TLC warns about the liveness checker before it builds it.
|
|
1713
|
+
if (search.properties.liveness.length > 0) {
|
|
1714
|
+
options.onProgress?.({
|
|
1715
|
+
kind: "liveness-initializing",
|
|
1716
|
+
isSymmetric: search.isSymmetric,
|
|
1717
|
+
isConstrained: search.isConstrained,
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1003
1720
|
const liveness = withLivenessExitCode(() =>
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
(
|
|
1007
|
-
BigInt.asIntN(32, runtime.getFingerprint(
|
|
1008
|
-
)
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1721
|
+
withUnexpectedException(() =>
|
|
1722
|
+
createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
|
|
1723
|
+
getFingerprint: ({ index, state }) =>
|
|
1724
|
+
(tlcFingerprints[index] ??= Number(BigInt.asIntN(32, runtime.getFingerprint(state)))),
|
|
1725
|
+
getSuccessors: (source, state) => {
|
|
1726
|
+
const successors: LiveSuccessor[] = [];
|
|
1727
|
+
if (spec.next === undefined) return successors;
|
|
1728
|
+
// The constraints read the level of the state whose successors they check, as when TLC
|
|
1729
|
+
// explored it.
|
|
1730
|
+
const livenessSource = current.livenessSource;
|
|
1731
|
+
current.livenessSource = source;
|
|
1732
|
+
forEachSuccessor(
|
|
1733
|
+
spec.next,
|
|
1734
|
+
state,
|
|
1735
|
+
environment,
|
|
1736
|
+
(successor) => {
|
|
1737
|
+
canonicalizeState(successor.state);
|
|
1738
|
+
const isInModel =
|
|
1739
|
+
search.satisfiesConstraints(successor.state, state) &&
|
|
1740
|
+
search.satisfiesActionConstraints(state, successor.state);
|
|
1741
|
+
if (!isInModel) return false;
|
|
1742
|
+
const fingerprintState = getFingerprintState(successor.state);
|
|
1743
|
+
const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
|
|
1744
|
+
successors.push({ index, state: successor.state });
|
|
1745
|
+
return false;
|
|
1746
|
+
},
|
|
1747
|
+
spec.scope,
|
|
1748
|
+
);
|
|
1749
|
+
current.livenessSource = livenessSource;
|
|
1750
|
+
return successors;
|
|
1751
|
+
},
|
|
1752
|
+
}),
|
|
1753
|
+
),
|
|
1032
1754
|
);
|
|
1755
|
+
// TLC 1.7.4 creates its model checker, which TLCGet reads, once it has built the liveness checker.
|
|
1756
|
+
runtime.isStarted = true;
|
|
1033
1757
|
const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
|
|
1034
1758
|
|
|
1035
1759
|
const states: Value[][] = [];
|
|
@@ -1037,7 +1761,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1037
1761
|
const parents: number[] = [];
|
|
1038
1762
|
const actions: (string | undefined)[] = [];
|
|
1039
1763
|
const depths: number[] = [];
|
|
1040
|
-
|
|
1764
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1765
|
+
// and its index plus one side by side, probed by the hash.
|
|
1766
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
1041
1767
|
const canonicalizeState = createCanonicalizer();
|
|
1042
1768
|
|
|
1043
1769
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -1067,58 +1793,107 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1067
1793
|
return toAliasRecord(value, fieldOrder) ?? toRecord(state);
|
|
1068
1794
|
} catch (error) {
|
|
1069
1795
|
if (!(error instanceof TlaConfigError || error instanceof TlaEvaluationError)) throw error;
|
|
1070
|
-
return {
|
|
1796
|
+
return {
|
|
1797
|
+
...toRecord(state),
|
|
1798
|
+
_ALIASEvalError: formatNestedMessage(error.message, options.isTool ?? false),
|
|
1799
|
+
};
|
|
1071
1800
|
}
|
|
1072
1801
|
};
|
|
1073
|
-
// TLC
|
|
1074
|
-
// the previous state.
|
|
1075
|
-
|
|
1076
|
-
|
|
1802
|
+
// TLC finds the action of a step as the first action that takes it, evaluated as when the search
|
|
1803
|
+
// expanded the previous state.
|
|
1804
|
+
// TLC finds the action of a successor it holds by the successor's values, and of any other state
|
|
1805
|
+
// by its fingerprint.
|
|
1806
|
+
const findStepAction = (index: number, previous: Value[], state: Value[], isHeld: boolean) => {
|
|
1807
|
+
const actions = getSpecNextActions();
|
|
1077
1808
|
const expanding = { ...current };
|
|
1078
1809
|
Object.assign(current, { index, isExpanding: true, candidate: undefined });
|
|
1079
|
-
const
|
|
1080
|
-
const
|
|
1810
|
+
const getKey = isHeld ? (values: Value[]) => values : getFingerprintState;
|
|
1811
|
+
const key = getKey(state);
|
|
1812
|
+
const action = actions.find((item) =>
|
|
1081
1813
|
getSuccessors(item.expression, previous, environment, item.scope).some((successor) =>
|
|
1082
|
-
isSameState(
|
|
1814
|
+
isSameState(getKey(successor.state), key),
|
|
1083
1815
|
),
|
|
1084
1816
|
);
|
|
1085
1817
|
Object.assign(current, expanding);
|
|
1086
|
-
return action
|
|
1818
|
+
return action;
|
|
1087
1819
|
};
|
|
1088
|
-
|
|
1820
|
+
// Before TLC prints a trace, it rebuilds from the initial predicate the states it does not keep,
|
|
1821
|
+
// all but the last `known` ones. As it prints each later step, it finds the action that takes
|
|
1822
|
+
// the step. Then it rebuilds the trace once more, unless the trace is a liveness counterexample
|
|
1823
|
+
// or TLC is 1.7.4.
|
|
1824
|
+
const toTrace = (
|
|
1825
|
+
steps: Successor[],
|
|
1826
|
+
path: number[],
|
|
1827
|
+
traceOptions: TraceOptions = {},
|
|
1828
|
+
): Behavior => {
|
|
1089
1829
|
const alias = config.alias;
|
|
1090
|
-
const
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1830
|
+
const known = traceOptions.known ?? 1;
|
|
1831
|
+
// Rebuilding the trace from the initial predicate can reset what TLCSet stored.
|
|
1832
|
+
const initialOutput = steps.length > 1 ? getOutput(getSpecInitialStates).lines : [];
|
|
1833
|
+
// TLC finds the action that takes each step before it evaluates the alias.
|
|
1834
|
+
const found = steps.map((step, position) =>
|
|
1835
|
+
position === 0 || step.action === "Stuttering"
|
|
1836
|
+
? { result: undefined, lines: [] }
|
|
1837
|
+
: getOutput(() =>
|
|
1838
|
+
findStepAction(
|
|
1839
|
+
path[position - 1],
|
|
1840
|
+
steps[position - 1].state,
|
|
1841
|
+
step.state,
|
|
1842
|
+
known === 2 && position === steps.length - 1,
|
|
1843
|
+
),
|
|
1844
|
+
),
|
|
1845
|
+
);
|
|
1846
|
+
const outputs = found.map((step) => step.lines);
|
|
1847
|
+
const loopStart = traceOptions.loopStart;
|
|
1848
|
+
// 1.7.4's LiveWorker#printTrace rebuilds a lasso up to the start of its cycle at once, and then
|
|
1849
|
+
// each state of the cycle as it prints it.
|
|
1850
|
+
const rebuilt = is174 && loopStart !== undefined ? loopStart + 1 : steps.length - known;
|
|
1851
|
+
const prefixOutput = rebuilt > 0 ? [initialOutput, ...outputs.slice(1, rebuilt)].flat() : [];
|
|
1852
|
+
return {
|
|
1853
|
+
trace: steps.map((step, position) => {
|
|
1854
|
+
const action = found[position].result;
|
|
1855
|
+
const traceStep: TraceStep = {
|
|
1856
|
+
action: step.action,
|
|
1857
|
+
label: action && getActionLabel(action, options.version),
|
|
1858
|
+
state:
|
|
1859
|
+
alias === undefined
|
|
1860
|
+
? toRecord(step.state)
|
|
1861
|
+
: getAliasState(alias, steps, position, loopStart),
|
|
1862
|
+
output: position === 0 ? prefixOutput : position < rebuilt ? [] : outputs[position],
|
|
1863
|
+
};
|
|
1864
|
+
if (alias !== undefined || action) {
|
|
1865
|
+
traceSteps.set(traceStep, { state: toRecord(step.state), action });
|
|
1866
|
+
}
|
|
1867
|
+
return traceStep;
|
|
1868
|
+
}),
|
|
1869
|
+
traceOutput: known > 0 && !is174 ? [initialOutput, ...outputs].flat() : [],
|
|
1870
|
+
};
|
|
1108
1871
|
};
|
|
1109
1872
|
const getSpecInitialStates = () =>
|
|
1110
1873
|
spec.init === undefined ? [] : getInitialStates(spec.init, environment, spec.scope);
|
|
1111
|
-
|
|
1112
|
-
|
|
1874
|
+
// TLC's Tool#getState rebuilds a successor action by action, up to the first action that
|
|
1875
|
+
// generates it. A trace keeps what the spec prints as TLC rebuilds it, so finding its states
|
|
1876
|
+
// prints nothing.
|
|
1877
|
+
const findTraceSuccessor = (state: Value[], isMatch: (successor: Successor) => boolean) =>
|
|
1878
|
+
getOutput(() => {
|
|
1879
|
+
for (const action of getSpecNextActions()) {
|
|
1880
|
+
const successor = getSuccessors(action.expression, state, environment, action.scope).find(
|
|
1881
|
+
isMatch,
|
|
1882
|
+
);
|
|
1883
|
+
if (successor) return { ...successor, action: successor.action ?? action.name };
|
|
1884
|
+
}
|
|
1885
|
+
return undefined;
|
|
1886
|
+
}).result;
|
|
1113
1887
|
const hasFingerprint = (state: Value[], index: number) =>
|
|
1114
1888
|
isSameState(getFingerprintState(state), fingerprintStates[index]);
|
|
1115
1889
|
const findSuccessorWithFingerprint = (state: Value[], index: number) =>
|
|
1116
|
-
|
|
1890
|
+
findTraceSuccessor(state, (successor) => hasFingerprint(successor.state, index));
|
|
1117
1891
|
const getPathStates = (path: number[], nextFailure: string) => {
|
|
1118
|
-
const initial =
|
|
1892
|
+
const initial = getOutput(() =>
|
|
1119
1893
|
isReducing || isImpure(environment)
|
|
1120
1894
|
? getSpecInitialStates().find((state) => hasFingerprint(state, path[0]))
|
|
1121
|
-
: states[path[0]]
|
|
1895
|
+
: states[path[0]],
|
|
1896
|
+
).result;
|
|
1122
1897
|
if (!initial) throw new TlaEvaluationError(INITIAL_RECOVERY_FAILURE);
|
|
1123
1898
|
const steps: Successor[] = [{ state: initial, action: undefined }];
|
|
1124
1899
|
for (let position = 1; position < path.length; position++) {
|
|
@@ -1144,36 +1919,119 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1144
1919
|
: path.map((position) => ({ state: states[position], action: actions[position] }));
|
|
1145
1920
|
if (!last) return toTrace(steps, path);
|
|
1146
1921
|
const action = isReducing
|
|
1147
|
-
?
|
|
1148
|
-
|
|
1149
|
-
)?.action
|
|
1922
|
+
? findTraceSuccessor(states[index], (successor) => isSameState(successor.state, last.state))
|
|
1923
|
+
?.action
|
|
1150
1924
|
: last.action;
|
|
1151
|
-
return toTrace([...steps, { state: last.state, action }], path);
|
|
1925
|
+
return toTrace([...steps, { state: last.state, action }], path, { known: 2 });
|
|
1152
1926
|
};
|
|
1153
1927
|
// TLC prints an initial state on its own without evaluating the alias.
|
|
1154
|
-
|
|
1928
|
+
// TLCTrace#getTrace recovers even a lone initial state from the initial predicate, printing
|
|
1929
|
+
// again, though TLC 1.7.4 doesn't.
|
|
1930
|
+
const getStateTrace = (index: number): Behavior =>
|
|
1155
1931
|
parents[index] === -1
|
|
1156
|
-
?
|
|
1932
|
+
? {
|
|
1933
|
+
trace: [{ action: undefined, label: undefined, state: toRecord(states[index]) }],
|
|
1934
|
+
traceOutput: is174 ? [] : getOutput(getSpecInitialStates).lines,
|
|
1935
|
+
}
|
|
1157
1936
|
: getTrace(index);
|
|
1937
|
+
// TLC rebuilds the trace again for the postcondition after it prints a violation, but not after
|
|
1938
|
+
// an evaluation fails.
|
|
1939
|
+
const getFailedTrace = (index: number, successor: Successor): Behavior => ({
|
|
1940
|
+
trace: getTrace(index, successor).trace,
|
|
1941
|
+
});
|
|
1158
1942
|
// TLC names the initial state it was checking unless a standard module's Java code failed.
|
|
1159
|
-
const
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1943
|
+
const getFailureExitCode = (error: TlaEvaluationError, failureOptions: FailureOptions) => {
|
|
1944
|
+
// TLC 1.7.4 exits with the result of its replay of the initial states, which only fails again
|
|
1945
|
+
// when the error isn't in adding a state it has already seen to the behavior graph.
|
|
1946
|
+
if (
|
|
1947
|
+
failureOptions.isInitial &&
|
|
1948
|
+
!failureOptions.callStack &&
|
|
1949
|
+
!failureOptions.fingerprint &&
|
|
1950
|
+
is174
|
|
1951
|
+
) {
|
|
1952
|
+
return EXIT_STATUS.success;
|
|
1953
|
+
}
|
|
1954
|
+
if (!failureOptions.callStack) {
|
|
1955
|
+
return failureOptions.isInitial ? getInitialExitCode(error) : getErrorExitCode(error);
|
|
1956
|
+
}
|
|
1957
|
+
if (failureOptions.isCallStackFirst) return getErrorExitCode(error);
|
|
1958
|
+
// Without a replay, TLC exits with the code of the error, as for any error it didn't expect.
|
|
1959
|
+
if (failureOptions.isLiveCallStack) {
|
|
1960
|
+
return (
|
|
1961
|
+
getExitCode(error) ?? (error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.error)
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
// TLC exits with the code of a failed replay of the initial states regardless of the error, as
|
|
1965
|
+
// it does for a replay that fails to fingerprint.
|
|
1966
|
+
return failureOptions.isInitial || failureOptions.fingerprint
|
|
1967
|
+
? EXIT_STATUS.specEvaluationFailure
|
|
1968
|
+
: getReplayExitCode(error);
|
|
1969
|
+
};
|
|
1970
|
+
const toFailure = (failed: unknown, behavior: Behavior, failureOptions: FailureOptions = {}) => {
|
|
1971
|
+
const error = isStackOverflow(failed) ? new TlaStackOverflowError() : failed;
|
|
1165
1972
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1973
|
+
const checked =
|
|
1974
|
+
behavior.trace.length > 0 && !failureOptions.traceFailure
|
|
1975
|
+
? checkTracePostconditions(behavior.trace, undefined)
|
|
1976
|
+
: undefined;
|
|
1977
|
+
const postcondition = checked?.unwitnessed ?? checked?.postcondition;
|
|
1978
|
+
const traceOutput = [...(behavior.traceOutput ?? []), ...(checked?.lines ?? [])];
|
|
1166
1979
|
return {
|
|
1167
1980
|
...createResult("error", undefined, {
|
|
1168
|
-
|
|
1169
|
-
|
|
1981
|
+
...getFailureMessage(error, failureOptions.isNextState ?? false),
|
|
1982
|
+
...behavior,
|
|
1983
|
+
...(traceOutput.length > 0 && { traceOutput }),
|
|
1984
|
+
...(postcondition && { postcondition }),
|
|
1170
1985
|
initialState:
|
|
1171
|
-
initialState && !(error instanceof TlaModuleError)
|
|
1172
|
-
|
|
1986
|
+
failureOptions.initialState && !(error instanceof TlaModuleError)
|
|
1987
|
+
? toRecord(failureOptions.initialState)
|
|
1988
|
+
: undefined,
|
|
1989
|
+
exitCode: getFailureExitCode(error, failureOptions),
|
|
1990
|
+
callStack: failureOptions.callStack,
|
|
1991
|
+
...(failureOptions.isCallStackFirst && { isCallStackFirst: true }),
|
|
1992
|
+
...(failureOptions.fingerprint && { fingerprint: failureOptions.fingerprint }),
|
|
1993
|
+
...(failureOptions.traceFailure && { traceFailure: failureOptions.traceFailure }),
|
|
1173
1994
|
}),
|
|
1174
|
-
outDegrees:
|
|
1995
|
+
outDegrees: failureOptions.outDegrees,
|
|
1175
1996
|
};
|
|
1176
1997
|
};
|
|
1998
|
+
// TLC reports a successor state that an action leaves incomplete with the trace to it, which ends
|
|
1999
|
+
// with the partial state. It names the action when the next-state relation has several.
|
|
2000
|
+
const reportIncompleteSuccessor = (index: number, error: TlaIncompleteSuccessorError) => {
|
|
2001
|
+
runtime.generatedStates++;
|
|
2002
|
+
const actions = getSpecNextActions();
|
|
2003
|
+
const action =
|
|
2004
|
+
actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
|
|
2005
|
+
const missing = error.variables;
|
|
2006
|
+
const subject =
|
|
2007
|
+
actions.length === 1
|
|
2008
|
+
? "the next-state action"
|
|
2009
|
+
: `action ${error.action} of the next-state relation`;
|
|
2010
|
+
// TLC 1.7.4 says a variable is not assigned rather than not defined.
|
|
2011
|
+
const verb = is174 ? "assigned" : "defined";
|
|
2012
|
+
const message = `Successor state is not completely specified by ${subject}. The following variable${missing.length > 1 ? "s are" : " is"} not ${verb}: ${missing.join(", ")}.\n`;
|
|
2013
|
+
const undefinedValue = createModelValue("null");
|
|
2014
|
+
const partial = error.state.map((value) => value ?? undefinedValue);
|
|
2015
|
+
const failure = toFailure(error, getStateTrace(index));
|
|
2016
|
+
return withOutDegrees({
|
|
2017
|
+
...failure,
|
|
2018
|
+
failure: failure.failure && {
|
|
2019
|
+
...failure.failure,
|
|
2020
|
+
code: MESSAGE_CODE.incompleteSuccessor,
|
|
2021
|
+
message,
|
|
2022
|
+
toolMessage: message,
|
|
2023
|
+
exitCode: EXIT_STATUS.specEvaluationFailure,
|
|
2024
|
+
trace: [
|
|
2025
|
+
...failure.failure.trace,
|
|
2026
|
+
{
|
|
2027
|
+
action: error.action,
|
|
2028
|
+
label: action && getActionLabel(action, options.version),
|
|
2029
|
+
state: toRecord(partial),
|
|
2030
|
+
},
|
|
2031
|
+
],
|
|
2032
|
+
},
|
|
2033
|
+
});
|
|
2034
|
+
};
|
|
1177
2035
|
const getPathSteps = (path: number[], loopStart?: number) => {
|
|
1178
2036
|
const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
|
|
1179
2037
|
const last = steps[steps.length - 1].state;
|
|
@@ -1191,26 +2049,52 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1191
2049
|
hash = combineHash(hash, getValueHash(state[index]));
|
|
1192
2050
|
return hash & 0x3fffffff;
|
|
1193
2051
|
};
|
|
2052
|
+
// TLC fingerprints a state once it finds the state in the model.
|
|
2053
|
+
const fingerprintModelState = (state: Value[]) => {
|
|
2054
|
+
for (const value of state) {
|
|
2055
|
+
const error = getFingerprintError(value);
|
|
2056
|
+
if (error) throw error;
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
1194
2059
|
const isSameState = (stored: Value[], state: Value[]) => {
|
|
1195
2060
|
for (let position = 0; position < state.length; position++) {
|
|
1196
2061
|
if (!isSameValue(stored[position], state[position])) return false;
|
|
1197
2062
|
}
|
|
1198
2063
|
return true;
|
|
1199
2064
|
};
|
|
2065
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1200
2066
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1201
|
-
const
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
2067
|
+
const mask = seen.slots.length - 1;
|
|
2068
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
2069
|
+
const entry = seen.slots[slot + 1];
|
|
2070
|
+
if (entry === 0) return -1;
|
|
2071
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
2072
|
+
return entry - 1;
|
|
2073
|
+
}
|
|
1205
2074
|
}
|
|
1206
|
-
|
|
2075
|
+
};
|
|
2076
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
2077
|
+
const mask = seen.slots.length - 1;
|
|
2078
|
+
let slot = getSlot(hash);
|
|
2079
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
2080
|
+
seen.slots[slot] = hash;
|
|
2081
|
+
seen.slots[slot + 1] = entry;
|
|
1207
2082
|
};
|
|
1208
2083
|
const markSeen = (hash: number, index: number) => {
|
|
1209
|
-
|
|
1210
|
-
if (
|
|
1211
|
-
|
|
1212
|
-
|
|
2084
|
+
seen.size++;
|
|
2085
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
2086
|
+
const slots = seen.slots;
|
|
2087
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
2088
|
+
seen.shift--;
|
|
2089
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
2090
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
insertSeen(hash, index + 1);
|
|
1213
2094
|
};
|
|
2095
|
+
// A new successor that TLC has added to its set of states before it checks the successor, which
|
|
2096
|
+
// the check stores after.
|
|
2097
|
+
const unstoredState = { fingerprint: undefined as Value[] | undefined };
|
|
1214
2098
|
const countDistinctState = (depth: number) => {
|
|
1215
2099
|
runtime.distinctStates = states.length + 1;
|
|
1216
2100
|
statistics.depth = Math.max(statistics.depth, depth);
|
|
@@ -1253,6 +2137,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1253
2137
|
};
|
|
1254
2138
|
runtime.getLevel = (state) => {
|
|
1255
2139
|
const source = current.livenessSource;
|
|
2140
|
+
// TLC 1.7.4 reads the level of the state its worker explores, whichever state it evaluates.
|
|
2141
|
+
if (is174) {
|
|
2142
|
+
const explored = source !== -1 ? source : current.isExpanding ? current.index : -1;
|
|
2143
|
+
return explored === -1 ? 0 : depths[explored];
|
|
2144
|
+
}
|
|
1256
2145
|
if (source !== -1) return state === states[source] ? depths[source] : depths[source] + 1;
|
|
1257
2146
|
if (state === current.candidate) return current.candidateLevel;
|
|
1258
2147
|
if (current.index === -1) return 0;
|
|
@@ -1281,9 +2170,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1281
2170
|
const fingerprintState = getFingerprintState(state);
|
|
1282
2171
|
const hash = getStateHash(fingerprintState);
|
|
1283
2172
|
const isInModel = search.satisfiesConstraints(state);
|
|
1284
|
-
if (isInModel)
|
|
2173
|
+
if (isInModel) {
|
|
2174
|
+
search.trackStatePossible(state);
|
|
2175
|
+
fingerprintModelState(fingerprintState);
|
|
2176
|
+
state.forEach(markFingerprinted);
|
|
2177
|
+
}
|
|
1285
2178
|
const isSeen = isInModel && findStateIndex(hash, fingerprintState) !== -1;
|
|
1286
2179
|
if (isSeen) return undefined;
|
|
2180
|
+
unstoredState.fingerprint = isInModel ? fingerprintState : undefined;
|
|
1287
2181
|
if (isInModel) {
|
|
1288
2182
|
countDistinctState(1);
|
|
1289
2183
|
runtime.queueSize++;
|
|
@@ -1294,14 +2188,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1294
2188
|
if (violated) {
|
|
1295
2189
|
return reportViolation("invariant-violation", {
|
|
1296
2190
|
name: violated.name,
|
|
1297
|
-
|
|
2191
|
+
...toTrace([{ action: undefined, state }], []),
|
|
1298
2192
|
});
|
|
1299
2193
|
}
|
|
1300
2194
|
const failedPredicate = search.findFailedInitialPredicate(state);
|
|
1301
2195
|
if (failedPredicate) {
|
|
1302
2196
|
return reportViolation("liveness-violation", {
|
|
1303
2197
|
name: failedPredicate.name,
|
|
1304
|
-
|
|
2198
|
+
...toTrace([{ action: undefined, state }], []),
|
|
1305
2199
|
});
|
|
1306
2200
|
}
|
|
1307
2201
|
if (!isInModel) return undefined;
|
|
@@ -1327,7 +2221,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1327
2221
|
const isStateInModel = search.satisfiesConstraints(successor.state, state);
|
|
1328
2222
|
if (isStateInModel) search.trackStatePossible(successor.state);
|
|
1329
2223
|
const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
|
|
1330
|
-
if (isInModel)
|
|
2224
|
+
if (isInModel) {
|
|
2225
|
+
search.trackActionPossible(state, successor.state);
|
|
2226
|
+
fingerprintModelState(fingerprintState);
|
|
2227
|
+
successor.state.forEach(markFingerprinted);
|
|
2228
|
+
}
|
|
1331
2229
|
const existing = isInModel ? found : -1;
|
|
1332
2230
|
if (source && isInModel) {
|
|
1333
2231
|
stateWriter?.writeSuccessor(
|
|
@@ -1343,6 +2241,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1343
2241
|
liveSuccessors.push({ index: existing, state: isStored ? stored : successor.state });
|
|
1344
2242
|
}
|
|
1345
2243
|
const isNew = isInModel && existing === -1;
|
|
2244
|
+
// TLC adds a new state to the behavior graph's successors before it checks the invariants.
|
|
2245
|
+
if (isNew && liveness) liveSuccessors.push({ index: states.length, state: successor.state });
|
|
2246
|
+
unstoredState.fingerprint = isNew ? fingerprintState : undefined;
|
|
1346
2247
|
if (isNew) {
|
|
1347
2248
|
countDistinctState(depths[index] + 1);
|
|
1348
2249
|
expansion.newStates++;
|
|
@@ -1351,53 +2252,105 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1351
2252
|
try {
|
|
1352
2253
|
const violated = search.findViolatedInvariant(successor.state, state);
|
|
1353
2254
|
if (violated) {
|
|
1354
|
-
return
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
2255
|
+
return withOutDegrees(
|
|
2256
|
+
reportViolation("invariant-violation", {
|
|
2257
|
+
name: violated.name,
|
|
2258
|
+
...getTrace(index, successor),
|
|
2259
|
+
}),
|
|
2260
|
+
);
|
|
1358
2261
|
}
|
|
1359
2262
|
} catch (error) {
|
|
1360
|
-
|
|
1361
|
-
|
|
2263
|
+
if (isStackOverflow(error)) throw error;
|
|
2264
|
+
// TLC replays the expansion and skips the invariants of a state it has seen, the failed one
|
|
2265
|
+
// included unless the constraints exclude it. When the replay fails, TLC reports where and
|
|
2266
|
+
// leaves out the outdegree.
|
|
2267
|
+
const replay = getOutput(() => replayNext(index));
|
|
2268
|
+
return toFailure(
|
|
2269
|
+
error,
|
|
2270
|
+
appendTraceOutput(getFailedTrace(index, successor), replay.lines),
|
|
2271
|
+
replay.result ?? { outDegrees: getFailureOutDegrees() },
|
|
2272
|
+
);
|
|
1362
2273
|
}
|
|
1363
2274
|
}
|
|
1364
2275
|
const violatedProperty = search.findViolatedActionProperty(state, successor.state);
|
|
1365
2276
|
if (violatedProperty) {
|
|
1366
|
-
return
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
2277
|
+
return withOutDegrees(
|
|
2278
|
+
reportViolation("liveness-violation", {
|
|
2279
|
+
name: violatedProperty.name,
|
|
2280
|
+
...getTrace(index, successor),
|
|
2281
|
+
}),
|
|
2282
|
+
);
|
|
1370
2283
|
}
|
|
1371
2284
|
if (!isNew) return undefined;
|
|
1372
2285
|
if (!isCanonicalizedFirst) canonicalizeState(successor.state);
|
|
1373
2286
|
markSeen(hash, states.length);
|
|
1374
|
-
if (liveness) liveSuccessors.push({ index: states.length, state: successor.state });
|
|
1375
2287
|
addState(successor.state, fingerprintState, index, successor.action);
|
|
1376
2288
|
runtime.queueSize++;
|
|
1377
2289
|
return undefined;
|
|
1378
2290
|
};
|
|
1379
2291
|
|
|
1380
2292
|
const outcome: { result: CheckResult | undefined } = { result: undefined };
|
|
2293
|
+
if (liveness) {
|
|
2294
|
+
options.onProgress?.({ kind: "liveness-prepared", branches: liveness.branches });
|
|
2295
|
+
}
|
|
2296
|
+
// TLC checks the assumptions once it has built the liveness checker in ModelChecker's
|
|
2297
|
+
// constructor.
|
|
2298
|
+
const assumptionViolation = findViolatedAssumption();
|
|
2299
|
+
if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
|
|
2300
|
+
options.onProgress?.({ kind: "initial-states-started" });
|
|
1381
2301
|
if (spec.init !== undefined) {
|
|
1382
2302
|
try {
|
|
1383
2303
|
forEachInitialState(
|
|
1384
2304
|
spec.init,
|
|
1385
2305
|
environment,
|
|
1386
2306
|
(state) => {
|
|
2307
|
+
reportInitialStatesProgress(runtime.initialStates, options.onProgress);
|
|
1387
2308
|
try {
|
|
1388
2309
|
outcome.result = visitInitialState(state);
|
|
1389
2310
|
} catch (error) {
|
|
1390
|
-
|
|
2311
|
+
const replay = getOutput(replayInit);
|
|
2312
|
+
outcome.result = toFailure(
|
|
2313
|
+
error,
|
|
2314
|
+
{ trace: [], traceOutput: replay.lines },
|
|
2315
|
+
{ initialState: state, ...replay.result, isInitial: true },
|
|
2316
|
+
);
|
|
1391
2317
|
}
|
|
1392
2318
|
return outcome.result !== undefined;
|
|
1393
2319
|
},
|
|
1394
2320
|
spec.scope,
|
|
1395
2321
|
);
|
|
1396
2322
|
} catch (error) {
|
|
1397
|
-
|
|
2323
|
+
// TLC prints an illegal initial state with null for each variable it leaves undefined, and
|
|
2324
|
+
// stops before it prints the statistics.
|
|
2325
|
+
if (error instanceof TlaIllegalStateError) {
|
|
2326
|
+
const illegal = toFailure(
|
|
2327
|
+
error,
|
|
2328
|
+
{ trace: [] },
|
|
2329
|
+
{ initialState: error.state.map((value) => value ?? createModelValue("null")) },
|
|
2330
|
+
);
|
|
2331
|
+
return {
|
|
2332
|
+
...illegal,
|
|
2333
|
+
failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
|
|
2334
|
+
};
|
|
2335
|
+
}
|
|
2336
|
+
// TLC prints the error before it replays the initial predicate with its call stack.
|
|
2337
|
+
const replay = getOutput(replayInit);
|
|
2338
|
+
return toFailure(
|
|
2339
|
+
error,
|
|
2340
|
+
{ trace: [], traceOutput: replay.lines },
|
|
2341
|
+
{ ...replay.result, isInitial: true },
|
|
2342
|
+
);
|
|
1398
2343
|
}
|
|
1399
2344
|
}
|
|
1400
2345
|
if (outcome.result) return outcome.result;
|
|
2346
|
+
statistics.initialStates = {
|
|
2347
|
+
generated: runtime.initialStates,
|
|
2348
|
+
distinct: runtime.distinctStates,
|
|
2349
|
+
};
|
|
2350
|
+
options.onProgress?.({
|
|
2351
|
+
kind: "initial-states-computed",
|
|
2352
|
+
initialStates: statistics.initialStates,
|
|
2353
|
+
});
|
|
1401
2354
|
|
|
1402
2355
|
const next = spec.next;
|
|
1403
2356
|
if (next === undefined) {
|
|
@@ -1426,9 +2379,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1426
2379
|
spec.scope,
|
|
1427
2380
|
);
|
|
1428
2381
|
}
|
|
1429
|
-
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1430
2382
|
const progress = { isStopped: false };
|
|
1431
|
-
return
|
|
2383
|
+
return getSpecNextActions().reduce((count, action) => {
|
|
1432
2384
|
if (progress.isStopped) return count;
|
|
1433
2385
|
return (
|
|
1434
2386
|
count +
|
|
@@ -1445,62 +2397,240 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1445
2397
|
);
|
|
1446
2398
|
}, 0);
|
|
1447
2399
|
};
|
|
1448
|
-
|
|
2400
|
+
// TLC's worker adds a state to the behavior graph even after its next states fail or violate a
|
|
2401
|
+
// property, and prints the call stack of an error in that, unless the error is a
|
|
2402
|
+
// FingerprintException, which the worker doesn't catch. TLC 1.7.4 prints none.
|
|
2403
|
+
const getLiveCallStack = (index: number, liveSuccessors: LiveSuccessor[]) => {
|
|
2404
|
+
if (!liveness || is174) return undefined;
|
|
2405
|
+
const replay = getReplayFailure((tracing) =>
|
|
2406
|
+
liveness.addState(index, states[index], liveSuccessors, tracing),
|
|
2407
|
+
);
|
|
2408
|
+
return replay?.callStack.length && !replay.fingerprint ? replay.callStack : undefined;
|
|
2409
|
+
};
|
|
2410
|
+
// TLC's worker adds the state to the behavior graph after it prints the trace. On an error in that,
|
|
2411
|
+
// it adds the state again to print the call stack instead of replaying the next-state relation,
|
|
2412
|
+
// which TLC 1.7.4 doesn't.
|
|
2413
|
+
const addFailedLiveState = (index: number, liveSuccessors: LiveSuccessor[]) => {
|
|
2414
|
+
try {
|
|
2415
|
+
liveness?.addState(index, states[index], liveSuccessors);
|
|
2416
|
+
return undefined;
|
|
2417
|
+
} catch (error) {
|
|
2418
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
2419
|
+
return getLiveCallStack(index, liveSuccessors);
|
|
2420
|
+
}
|
|
2421
|
+
};
|
|
2422
|
+
const replayFailedNext = (index: number, liveSuccessors: LiveSuccessor[]) => {
|
|
2423
|
+
const live = getOutput(() => addFailedLiveState(index, liveSuccessors));
|
|
2424
|
+
if (live.result) {
|
|
2425
|
+
const result = { callStack: live.result, fingerprint: undefined, isLiveCallStack: true };
|
|
2426
|
+
return { result, lines: live.lines };
|
|
2427
|
+
}
|
|
2428
|
+
const replay = getOutput(() => replayNext(index));
|
|
2429
|
+
return { result: replay.result, lines: [...live.lines, ...replay.lines] };
|
|
2430
|
+
};
|
|
2431
|
+
// TLC reports a stack overflow while it computes next states without replaying it, with the
|
|
2432
|
+
// behavior up to the state it expands.
|
|
2433
|
+
const reportNextStateOverflow = (index: number) =>
|
|
2434
|
+
toFailure(createStackOverflowError(), getStateTrace(index));
|
|
2435
|
+
// After the trace, TLC replays the next-state relation with its call stack, printing again.
|
|
2436
|
+
const reportNextStateFailure = (
|
|
2437
|
+
error: unknown,
|
|
2438
|
+
index: number,
|
|
2439
|
+
liveSuccessors: LiveSuccessor[],
|
|
2440
|
+
) => {
|
|
2441
|
+
if (isStackOverflow(error)) return reportNextStateOverflow(index);
|
|
2442
|
+
const replay = replayFailedNext(index, liveSuccessors);
|
|
2443
|
+
return toFailure(
|
|
2444
|
+
withNextStateExitCode(error),
|
|
2445
|
+
appendTraceOutput(getStateTrace(index), replay.lines),
|
|
2446
|
+
{ isNextState: true, ...replay.result },
|
|
2447
|
+
);
|
|
2448
|
+
};
|
|
2449
|
+
const expandState = (index: number, liveSuccessors: LiveSuccessor[]) => {
|
|
1449
2450
|
const state = states[index];
|
|
1450
|
-
const liveSuccessors: LiveSuccessor[] = [];
|
|
1451
2451
|
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1452
2452
|
const successorCount = forEachActionSuccessor(state, (successor, action) => {
|
|
1453
2453
|
try {
|
|
1454
2454
|
outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
|
|
1455
2455
|
} catch (error) {
|
|
1456
|
-
|
|
2456
|
+
if (isStackOverflow(error)) {
|
|
2457
|
+
outcome.result = reportNextStateOverflow(index);
|
|
2458
|
+
return true;
|
|
2459
|
+
}
|
|
2460
|
+
// TLC 1.7.4 reports the wrapped error without the trace, except for a failed property,
|
|
2461
|
+
// which it reports with the trace before it rethrows. The error stops its worker before it
|
|
2462
|
+
// adds the state to the behavior graph.
|
|
2463
|
+
const replay = is174
|
|
2464
|
+
? getOutput(() => replayNext(index))
|
|
2465
|
+
: replayFailedNext(index, liveSuccessors);
|
|
2466
|
+
if (is174 && error instanceof TlaEvaluationError && !headedErrors.has(error)) {
|
|
2467
|
+
outcome.result = toFailure(
|
|
2468
|
+
new TlaEvaluationError(`${getWrappedException(error)}${error.message}`, error),
|
|
2469
|
+
appendTraceOutput({ trace: [] }, replay.lines),
|
|
2470
|
+
{ isNextState: true, ...replay.result },
|
|
2471
|
+
);
|
|
2472
|
+
return true;
|
|
2473
|
+
}
|
|
2474
|
+
// A successor TLC can't fingerprint fails again as TLC prints it in the trace, which TLC
|
|
2475
|
+
// reports as an exception it didn't expect.
|
|
2476
|
+
const isTraceFailed = getFingerprintState(successor.state).some(
|
|
2477
|
+
(value) => getFingerprintError(value) !== undefined,
|
|
2478
|
+
);
|
|
2479
|
+
const traceFailure =
|
|
2480
|
+
isTraceFailed && error instanceof TlaEvaluationError
|
|
2481
|
+
? getFailureMessage(
|
|
2482
|
+
isEvalException(error)
|
|
2483
|
+
? new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`)
|
|
2484
|
+
: error,
|
|
2485
|
+
true,
|
|
2486
|
+
)
|
|
2487
|
+
: undefined;
|
|
2488
|
+
outcome.result = toFailure(
|
|
2489
|
+
error,
|
|
2490
|
+
appendTraceOutput(
|
|
2491
|
+
traceFailure ? getStateTrace(index) : getFailedTrace(index, successor),
|
|
2492
|
+
replay.lines,
|
|
2493
|
+
),
|
|
2494
|
+
{ isNextState: true, ...replay.result, traceFailure },
|
|
2495
|
+
);
|
|
1457
2496
|
}
|
|
1458
2497
|
return outcome.result !== undefined;
|
|
1459
2498
|
});
|
|
1460
2499
|
current.isExpanding = false;
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
2500
|
+
const result = outcome.result;
|
|
2501
|
+
// TLC 1.7.4's worker stops at a violation.
|
|
2502
|
+
if (result?.violation && !is174) {
|
|
2503
|
+
const live = getOutput(() => addFailedLiveState(index, liveSuccessors));
|
|
2504
|
+
return {
|
|
2505
|
+
...result,
|
|
2506
|
+
violation: appendTraceOutput(result.violation, live.lines),
|
|
2507
|
+
...(live.result && { liveCallStack: live.result }),
|
|
2508
|
+
};
|
|
1464
2509
|
}
|
|
1465
|
-
|
|
2510
|
+
if (result) return result;
|
|
2511
|
+
const addLiveState = () => {
|
|
2512
|
+
if (liveness && source && is174) stateWriter?.writeStuttering(source);
|
|
1466
2513
|
const accepted = liveness?.addState(index, state, liveSuccessors);
|
|
1467
2514
|
if (!liveness || !accepted) return undefined;
|
|
1468
2515
|
const steps = getPathSteps(accepted);
|
|
1469
|
-
const properties =
|
|
2516
|
+
const properties = getViolatedProperties(
|
|
2517
|
+
liveness,
|
|
1470
2518
|
steps.map((step) => step.state),
|
|
1471
2519
|
steps.length - 1,
|
|
1472
2520
|
);
|
|
1473
2521
|
return reportViolation("property-violation", {
|
|
1474
2522
|
name: properties[0],
|
|
1475
2523
|
properties,
|
|
1476
|
-
|
|
2524
|
+
...toTrace(steps, accepted, { known: 0 }),
|
|
2525
|
+
});
|
|
2526
|
+
};
|
|
2527
|
+
if (successorCount === 0 && config.checkDeadlock) {
|
|
2528
|
+
const deadlock = reportViolation("deadlock", { name: "deadlock", ...getStateTrace(index) });
|
|
2529
|
+
// TLC's worker still adds the deadlocked state to the behavior graph after it reports the
|
|
2530
|
+
// deadlock, and reports a temporal property that this violates. Then it counts the state as
|
|
2531
|
+
// expanded.
|
|
2532
|
+
const live = getOutput(() => {
|
|
2533
|
+
try {
|
|
2534
|
+
return { additional: addLiveState()?.violation };
|
|
2535
|
+
} catch (error) {
|
|
2536
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
2537
|
+
return { isFailed: true, callStack: getLiveCallStack(index, liveSuccessors) };
|
|
2538
|
+
}
|
|
1477
2539
|
});
|
|
2540
|
+
const reported = deadlock.violation
|
|
2541
|
+
? { ...deadlock, violation: appendTraceOutput(deadlock.violation, live.lines) }
|
|
2542
|
+
: deadlock;
|
|
2543
|
+
// An error in adding the state stops TLC 1.7.4's worker before it counts the state.
|
|
2544
|
+
const counted = withOutDegrees(
|
|
2545
|
+
reported,
|
|
2546
|
+
live.result.isFailed ? getFailureOutDegrees() : getPartialOutDegrees(),
|
|
2547
|
+
);
|
|
2548
|
+
if (live.result.additional)
|
|
2549
|
+
return { ...counted, additionalViolation: live.result.additional };
|
|
2550
|
+
return live.result.callStack ? { ...counted, liveCallStack: live.result.callStack } : counted;
|
|
2551
|
+
}
|
|
2552
|
+
try {
|
|
2553
|
+
return addLiveState();
|
|
1478
2554
|
} catch (error) {
|
|
1479
|
-
|
|
2555
|
+
// TLC 1.7.4's worker reports an error in adding a state to the behavior graph as an exception
|
|
2556
|
+
// it didn't expect, without the trace or a call stack, and exits as for any general error.
|
|
2557
|
+
if (is174 && error instanceof TlaEvaluationError) {
|
|
2558
|
+
const unexpected = new TlaEvaluationError(
|
|
2559
|
+
`${isEvalException(error) ? UNEXPECTED_EVAL_EXCEPTION : UNEXPECTED_EXCEPTION}${error.message}`,
|
|
2560
|
+
);
|
|
2561
|
+
return toFailure(
|
|
2562
|
+
setExitCode(unexpected, EXIT_STATUS.error),
|
|
2563
|
+
{ trace: [] },
|
|
2564
|
+
{ outDegrees: getFailureOutDegrees() },
|
|
2565
|
+
);
|
|
2566
|
+
}
|
|
2567
|
+
// TLC's Worker#doNextCheckLiveness adds the state again to find the call stack, prints it
|
|
2568
|
+
// before the error, and reports the error as one in computing next states.
|
|
2569
|
+
const replay = getReplayFailure((tracing) =>
|
|
2570
|
+
liveness?.addState(index, state, liveSuccessors, tracing),
|
|
2571
|
+
);
|
|
2572
|
+
return toFailure(error, getStateTrace(index), {
|
|
2573
|
+
outDegrees: getFailureOutDegrees(),
|
|
2574
|
+
callStack: replay?.callStack.length ? replay.callStack : undefined,
|
|
2575
|
+
isNextState: true,
|
|
2576
|
+
isCallStackFirst: true,
|
|
2577
|
+
});
|
|
2578
|
+
}
|
|
2579
|
+
};
|
|
2580
|
+
// TLC's Liveness#printStutteringCounterExampleWarning.
|
|
2581
|
+
const getStutteringWarning = () => {
|
|
2582
|
+
const name = config.specification;
|
|
2583
|
+
const book =
|
|
2584
|
+
"(compare Chapter 8, page 87ff of Specifying Systems at https://lamport.azurewebsites.net/tla/book.html).";
|
|
2585
|
+
if (name === undefined) {
|
|
2586
|
+
return {
|
|
2587
|
+
code: MESSAGE_CODE.noSpecificationButProperty,
|
|
2588
|
+
text: `The stuttering counterexample above may be caused by the absence of a behavior specification (SPECIFICATION). Only INIT and NEXT have been provided, so TLC permits infinite stuttering. To rule out such counterexamples, use SPECIFICATION Spec, with Spec asserting a suitable fairness constraint ${book}`,
|
|
2589
|
+
};
|
|
1480
2590
|
}
|
|
2591
|
+
const range = getDefinition(name, environment)?.location.range;
|
|
2592
|
+
if (spec.temporals.length > 0 || !range) return undefined;
|
|
2593
|
+
return {
|
|
2594
|
+
code: MESSAGE_CODE.noFairnessButProperty,
|
|
2595
|
+
text: `The stuttering counterexample above may be caused by the absence of a fairness constraint in the behavior specification ${name} defined at ${formatRange(range)}. To rule out such counterexamples, conjoin a suitable fairness constraint to ${name} ${book}`,
|
|
2596
|
+
};
|
|
1481
2597
|
};
|
|
1482
2598
|
// TLC names the properties and the step back into the cycle from the states it prints.
|
|
1483
2599
|
const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
|
|
1484
2600
|
const path = [...lasso.prefix, ...lasso.cycle];
|
|
1485
2601
|
const loopStart = lasso.prefix.length;
|
|
1486
2602
|
const steps = getPathSteps(path, loopStart);
|
|
1487
|
-
const trace = toTrace(steps, path, loopStart);
|
|
1488
|
-
const properties =
|
|
2603
|
+
const trace = toTrace(steps, path, { loopStart, known: 0 }).trace;
|
|
2604
|
+
const properties = getViolatedProperties(
|
|
2605
|
+
checker,
|
|
1489
2606
|
steps.map((step) => step.state),
|
|
1490
2607
|
loopStart,
|
|
1491
2608
|
);
|
|
1492
2609
|
const back = path[loopStart];
|
|
1493
2610
|
const last = path[path.length - 1];
|
|
1494
2611
|
const lastState = steps[steps.length - 1].state;
|
|
1495
|
-
const
|
|
1496
|
-
back === last
|
|
2612
|
+
const loop =
|
|
2613
|
+
back === last
|
|
2614
|
+
? { result: undefined, lines: [] }
|
|
2615
|
+
: getOutput(() => findStepAction(last, lastState, states[back], false));
|
|
2616
|
+
const isStutteringWarned = back === last && lasso.isCycleConstrained;
|
|
1497
2617
|
return reportViolation(
|
|
1498
2618
|
"liveness-violation",
|
|
1499
|
-
{
|
|
2619
|
+
{
|
|
2620
|
+
name: properties[0],
|
|
2621
|
+
properties,
|
|
2622
|
+
trace,
|
|
2623
|
+
traceOutput: loop.lines,
|
|
2624
|
+
loopStart,
|
|
2625
|
+
loopLabel: loop.result && getActionLabel(loop.result, options.version),
|
|
2626
|
+
warning: isStutteringWarned ? getStutteringWarning() : undefined,
|
|
2627
|
+
},
|
|
1500
2628
|
() =>
|
|
1501
2629
|
back === last
|
|
1502
|
-
? trace[trace.length - 1]
|
|
1503
|
-
:
|
|
2630
|
+
? getStepAction(trace[trace.length - 1])
|
|
2631
|
+
: loop.result
|
|
2632
|
+
? toActionValue(loop.result)
|
|
2633
|
+
: createActionRecord(findSuccessorWithFingerprint(lastState, back)?.action),
|
|
1504
2634
|
);
|
|
1505
2635
|
};
|
|
1506
2636
|
const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
|
|
@@ -1508,11 +2638,19 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1508
2638
|
const periodic = { at: startedAt + PERIODIC_START, size: 1, time: 0 };
|
|
1509
2639
|
const findPeriodicViolation = (expanded: number) => {
|
|
1510
2640
|
const now = performance.now();
|
|
1511
|
-
periodic.at = now + PROGRESS_INTERVAL;
|
|
1512
2641
|
const isGrown = (expanded - periodic.size) / periodic.size > LIVENESS_GROWTH;
|
|
1513
2642
|
if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
|
|
1514
2643
|
periodic.size = expanded;
|
|
1515
|
-
|
|
2644
|
+
if (!liveness) return undefined;
|
|
2645
|
+
options.onProgress?.({
|
|
2646
|
+
kind: "liveness-started",
|
|
2647
|
+
branches: liveness.branches,
|
|
2648
|
+
isComplete: false,
|
|
2649
|
+
states: expanded,
|
|
2650
|
+
statistics: createResult("success"),
|
|
2651
|
+
});
|
|
2652
|
+
const lasso = liveness.findViolation();
|
|
2653
|
+
if (!lasso) options.onProgress?.({ kind: "liveness-finished" });
|
|
1516
2654
|
periodic.time += performance.now() - now;
|
|
1517
2655
|
return lasso;
|
|
1518
2656
|
};
|
|
@@ -1521,28 +2659,57 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1521
2659
|
current.candidate = undefined;
|
|
1522
2660
|
runtime.queueSize = states.length - index - 1;
|
|
1523
2661
|
current.isExpanding = true;
|
|
2662
|
+
const liveSuccessors: LiveSuccessor[] = [];
|
|
1524
2663
|
try {
|
|
1525
|
-
outcome.result = expandState(index);
|
|
2664
|
+
outcome.result = expandState(index, liveSuccessors);
|
|
1526
2665
|
} catch (error) {
|
|
1527
|
-
outcome.result =
|
|
2666
|
+
outcome.result =
|
|
2667
|
+
error instanceof TlaIncompleteSuccessorError
|
|
2668
|
+
? reportIncompleteSuccessor(index, error)
|
|
2669
|
+
: reportNextStateFailure(error, index, liveSuccessors);
|
|
1528
2670
|
}
|
|
1529
2671
|
if (outcome.result) return outcome.result;
|
|
1530
2672
|
outDegrees[getOutDegreeBucket()]++;
|
|
1531
2673
|
expansion.newStates = 0;
|
|
1532
|
-
|
|
1533
|
-
|
|
2674
|
+
// TLC's AbstractChecker#runTLC checks the liveness properties on the state graph so far, then
|
|
2675
|
+
// reports how far the search got.
|
|
2676
|
+
if ((isPeriodic || options.onProgress) && performance.now() >= periodic.at) {
|
|
2677
|
+
periodic.at = performance.now() + PROGRESS_INTERVAL;
|
|
2678
|
+
const lasso = isPeriodic ? findPeriodicViolation(index + 1) : undefined;
|
|
1534
2679
|
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
2680
|
+
options.onProgress?.({ kind: "progress", statistics: createResult("success") });
|
|
1535
2681
|
}
|
|
1536
2682
|
}
|
|
1537
2683
|
|
|
1538
2684
|
current.index = -1;
|
|
1539
2685
|
current.candidate = undefined;
|
|
2686
|
+
if (liveness) {
|
|
2687
|
+
options.onProgress?.({
|
|
2688
|
+
kind: "liveness-started",
|
|
2689
|
+
branches: liveness.branches,
|
|
2690
|
+
isComplete: true,
|
|
2691
|
+
states: runtime.distinctStates,
|
|
2692
|
+
statistics: createResult("success"),
|
|
2693
|
+
});
|
|
2694
|
+
}
|
|
1540
2695
|
const lasso = liveness?.findViolation();
|
|
1541
2696
|
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1542
2697
|
|
|
1543
|
-
|
|
1544
|
-
if (
|
|
1545
|
-
|
|
2698
|
+
// TLC finishes the check of the temporal properties before it checks the postconditions.
|
|
2699
|
+
if (liveness) options.onProgress?.({ kind: "liveness-finished" });
|
|
2700
|
+
try {
|
|
2701
|
+
const postcondition = search.findPostconditionViolation();
|
|
2702
|
+
if (postcondition) {
|
|
2703
|
+
return createResult("postcondition-violation", {
|
|
2704
|
+
name: postcondition.name,
|
|
2705
|
+
message: postcondition.message,
|
|
2706
|
+
counts: postcondition.counts,
|
|
2707
|
+
trace: [],
|
|
2708
|
+
postcondition,
|
|
2709
|
+
});
|
|
2710
|
+
}
|
|
2711
|
+
} catch (error) {
|
|
2712
|
+
return toFailure(error, { trace: [] });
|
|
1546
2713
|
}
|
|
1547
2714
|
return createSuccess();
|
|
1548
2715
|
};
|