tla-ts 0.0.2 → 0.0.3-dev.07eab97
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 +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +98 -15
- package/dist/index.d.ts +98 -15
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +23 -2
- package/dist/run-cli.js +23 -2
- package/dist/transfer.cjs +36 -16
- package/dist/transfer.js +36 -16
- package/package.json +3 -1
- package/src/ast.ts +139 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +555 -107
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +156 -48
- package/src/errors.ts +123 -3
- package/src/evaluate.ts +258 -160
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +48 -12
- package/src/instances.ts +16 -15
- package/src/levels.ts +181 -45
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +350 -63
- package/src/resolve.ts +124 -44
- package/src/run-cli.ts +405 -45
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +183 -39
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/transfer.ts +22 -1
- package/src/value-sources.ts +29 -0
- package/src/values.ts +300 -83
package/src/check.ts
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Assumption,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type SourceLocation,
|
|
8
|
+
} from "./ast";
|
|
2
9
|
import { parseConfig, type ConfigValue, type ModelConfig } from "./config";
|
|
3
10
|
import { getReadVariables } from "./dependencies";
|
|
4
11
|
import { createEnvironment } from "./environment";
|
|
5
|
-
import {
|
|
6
|
-
|
|
12
|
+
import {
|
|
13
|
+
getErrorExitCode,
|
|
14
|
+
isStackOverflow,
|
|
15
|
+
formatRange,
|
|
16
|
+
STACK_OVERFLOW,
|
|
17
|
+
TlaConfigError,
|
|
18
|
+
TlaEvaluationError,
|
|
19
|
+
TlaIllegalStateError,
|
|
20
|
+
TlaIncompleteStateError,
|
|
21
|
+
TlaModuleError,
|
|
22
|
+
UNEXPECTED_EXCEPTION,
|
|
23
|
+
} from "./errors";
|
|
24
|
+
import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
|
|
7
25
|
import {
|
|
8
26
|
describeRange,
|
|
9
27
|
evaluate,
|
|
10
28
|
expectBoolean,
|
|
11
|
-
formatRange,
|
|
12
|
-
getChildren,
|
|
13
29
|
getDefinition,
|
|
14
30
|
isConstantLevel,
|
|
15
31
|
isImpure,
|
|
@@ -19,13 +35,12 @@ import {
|
|
|
19
35
|
} from "./evaluate";
|
|
20
36
|
import {
|
|
21
37
|
createLivenessChecker,
|
|
22
|
-
expandTemporal,
|
|
23
|
-
getLevel,
|
|
24
38
|
type Lasso,
|
|
39
|
+
type LiveGraph,
|
|
40
|
+
type LivenessChecker,
|
|
25
41
|
type LiveSuccessor,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} from "./liveness";
|
|
42
|
+
} from "./live-check";
|
|
43
|
+
import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
|
|
29
44
|
import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
|
|
30
45
|
import { prettyPrint } from "./pretty-print";
|
|
31
46
|
import {
|
|
@@ -37,23 +52,35 @@ import {
|
|
|
37
52
|
} from "./successors";
|
|
38
53
|
import {
|
|
39
54
|
getActionLabel,
|
|
55
|
+
getActionSignature,
|
|
40
56
|
getNextActions,
|
|
41
57
|
getSpecificationValue,
|
|
42
58
|
type SpecAction,
|
|
43
59
|
} from "./spec-description";
|
|
60
|
+
import {
|
|
61
|
+
createStateWriter,
|
|
62
|
+
type GraphState,
|
|
63
|
+
type StateDump,
|
|
64
|
+
type StateWriter,
|
|
65
|
+
} from "./state-graph";
|
|
44
66
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
45
|
-
import { getTlcStringOrder,
|
|
67
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
46
68
|
import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
|
|
47
69
|
import { createCanonicalizer } from "./canonical";
|
|
48
70
|
import { getPossibleCounts, type CheckerRuntime } from "./runtime";
|
|
49
71
|
import { createCounterExample } from "./tlc-ext";
|
|
72
|
+
import { getFingerprintTrace, valueSources } from "./value-sources";
|
|
50
73
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
51
74
|
import {
|
|
75
|
+
assertFingerprintable,
|
|
52
76
|
combineHash,
|
|
77
|
+
createModelValue,
|
|
78
|
+
formatState,
|
|
53
79
|
formatValue,
|
|
54
80
|
getValueHash,
|
|
55
81
|
isFunctionValue,
|
|
56
82
|
isSameValue,
|
|
83
|
+
isUpdatedElementOf,
|
|
57
84
|
setStringOrder,
|
|
58
85
|
toFiniteFunction,
|
|
59
86
|
type Value,
|
|
@@ -72,6 +99,8 @@ export interface CheckOptions {
|
|
|
72
99
|
workers?: number;
|
|
73
100
|
// TLC's -lncheck: "final" checks liveness only on the complete state graph.
|
|
74
101
|
livenessCheck?: "default" | "final";
|
|
102
|
+
// TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
|
|
103
|
+
dump?: StateDump;
|
|
75
104
|
}
|
|
76
105
|
|
|
77
106
|
export type CheckStatus =
|
|
@@ -93,6 +122,8 @@ export interface TraceStep {
|
|
|
93
122
|
|
|
94
123
|
export interface Violation {
|
|
95
124
|
name: string;
|
|
125
|
+
// Every temporal property that a liveness counterexample violates, in the configuration's order.
|
|
126
|
+
properties?: string[];
|
|
96
127
|
trace: TraceStep[];
|
|
97
128
|
loopStart: number | undefined;
|
|
98
129
|
loopLabel?: string;
|
|
@@ -113,6 +144,25 @@ export interface EvaluationFailure {
|
|
|
113
144
|
trace: TraceStep[];
|
|
114
145
|
initialState: Record<string, Value> | undefined;
|
|
115
146
|
exitCode: number;
|
|
147
|
+
replay?: Replay;
|
|
148
|
+
isWithoutStatistics?: boolean;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// TLC replays a failed step to print the expressions it was evaluating when the replay failed.
|
|
152
|
+
export interface Replay {
|
|
153
|
+
output: string[];
|
|
154
|
+
callStack: string[] | undefined;
|
|
155
|
+
fingerprint?: Fingerprint;
|
|
156
|
+
// The worker that fails to check liveness replays the step at once, so TLC prints the stack
|
|
157
|
+
// before the error, and only when it holds a frame.
|
|
158
|
+
isBeforeError?: boolean;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// TLC reports an error from a method of a value that has a source as one it was unable to
|
|
162
|
+
// fingerprint. With an empty stack, it lists the sources of the values instead.
|
|
163
|
+
export interface Fingerprint {
|
|
164
|
+
sources: string[];
|
|
165
|
+
reason: string;
|
|
116
166
|
}
|
|
117
167
|
|
|
118
168
|
export interface CheckResult {
|
|
@@ -139,6 +189,13 @@ interface NamedExpression {
|
|
|
139
189
|
|
|
140
190
|
interface StateCheck extends NamedExpression {
|
|
141
191
|
variables: number[] | undefined;
|
|
192
|
+
membership: Membership | undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// A conjunct `x \in S` whose S reads no variables.
|
|
196
|
+
interface Membership {
|
|
197
|
+
variable: number;
|
|
198
|
+
set: Expression;
|
|
142
199
|
}
|
|
143
200
|
|
|
144
201
|
interface ActionProperty {
|
|
@@ -217,7 +274,7 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
|
|
|
217
274
|
try {
|
|
218
275
|
return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
|
|
219
276
|
} catch (error) {
|
|
220
|
-
if (error instanceof TlaEvaluationError) return undefined;
|
|
277
|
+
if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
|
|
221
278
|
throw error;
|
|
222
279
|
}
|
|
223
280
|
};
|
|
@@ -246,8 +303,11 @@ const getInvariants = (names: string[], environment: Environment) => {
|
|
|
246
303
|
levels.params.size === 0
|
|
247
304
|
? ""
|
|
248
305
|
: "\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.";
|
|
249
|
-
throw
|
|
250
|
-
|
|
306
|
+
throw setExitCode(
|
|
307
|
+
new TlaConfigError(
|
|
308
|
+
`The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
|
|
309
|
+
),
|
|
310
|
+
EXIT_STATUS.safetyEvaluationFailure,
|
|
251
311
|
);
|
|
252
312
|
}
|
|
253
313
|
return [{ name, expression: createIdentifier(name), scope: undefined }];
|
|
@@ -345,8 +405,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
|
|
|
345
405
|
conjunct.kind === "Always" &&
|
|
346
406
|
getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
|
|
347
407
|
) {
|
|
348
|
-
throw
|
|
349
|
-
|
|
408
|
+
throw setExitCode(
|
|
409
|
+
new TlaConfigError(
|
|
410
|
+
`TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
|
|
411
|
+
),
|
|
412
|
+
EXIT_STATUS.error,
|
|
350
413
|
);
|
|
351
414
|
}
|
|
352
415
|
const level = getLevel(conjunct, item.scope, environment);
|
|
@@ -416,12 +479,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
416
479
|
config.properties.forEach((name) => {
|
|
417
480
|
const definition = getConfigPredicate("property", name, environment);
|
|
418
481
|
if (!definition) return;
|
|
419
|
-
expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
|
|
482
|
+
expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
|
|
420
483
|
const conjunct = item.expression;
|
|
421
484
|
if (isConstantTrue(item, environment, "property")) return;
|
|
422
485
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
423
486
|
properties.actionProperties.push({
|
|
424
|
-
name,
|
|
487
|
+
name: item.name,
|
|
425
488
|
box: conjunct.expression,
|
|
426
489
|
action: conjunct.expression.action,
|
|
427
490
|
subscript: conjunct.expression.subscript,
|
|
@@ -433,12 +496,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
433
496
|
conjunct.kind === "Always" &&
|
|
434
497
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
435
498
|
) {
|
|
436
|
-
|
|
499
|
+
// TLC names the invariant []P after P when P is an operator name.
|
|
500
|
+
const expression = conjunct.expression;
|
|
501
|
+
properties.invariants.push({
|
|
502
|
+
name: expression.kind === "Identifier" ? expression.name : item.name,
|
|
503
|
+
expression,
|
|
504
|
+
scope: item.scope,
|
|
505
|
+
});
|
|
437
506
|
return;
|
|
438
507
|
}
|
|
439
508
|
const level = getLevel(conjunct, item.scope, environment);
|
|
440
509
|
if (level < ACTION_LEVEL) {
|
|
441
|
-
properties.initialPredicates.push({
|
|
510
|
+
properties.initialPredicates.push({
|
|
511
|
+
name: item.name,
|
|
512
|
+
expression: conjunct,
|
|
513
|
+
scope: item.scope,
|
|
514
|
+
});
|
|
442
515
|
return;
|
|
443
516
|
}
|
|
444
517
|
if (level === ACTION_LEVEL) {
|
|
@@ -481,14 +554,34 @@ const getPossiblePredicates = (names: string[], environment: Environment) =>
|
|
|
481
554
|
return { name, expression, scope: undefined };
|
|
482
555
|
});
|
|
483
556
|
|
|
557
|
+
const getMembership = (
|
|
558
|
+
expression: Expression,
|
|
559
|
+
variables: number[] | undefined,
|
|
560
|
+
environment: Environment,
|
|
561
|
+
): Membership | undefined => {
|
|
562
|
+
if (expression.kind !== "Apply" || expression.operator !== "\\in" || variables?.length !== 1)
|
|
563
|
+
return undefined;
|
|
564
|
+
const element = expression.args[0];
|
|
565
|
+
if (element.kind !== "Identifier" || isLocallyResolved(element)) return undefined;
|
|
566
|
+
return environment.variableIndex.get(element.name) === variables[0]
|
|
567
|
+
? { variable: variables[0], set: expression.args[1] }
|
|
568
|
+
: undefined;
|
|
569
|
+
};
|
|
570
|
+
|
|
484
571
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
485
572
|
named.flatMap((item) =>
|
|
486
|
-
getConjuncts(item.expression, environment).map((expression) =>
|
|
487
|
-
name
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
573
|
+
getConjuncts(item.expression, environment).map((expression) => {
|
|
574
|
+
// A LET or an argument in the scope can read variables the expression doesn't name.
|
|
575
|
+
const variables =
|
|
576
|
+
item.scope === undefined ? getReadVariables(expression, environment) : undefined;
|
|
577
|
+
return {
|
|
578
|
+
name: item.name,
|
|
579
|
+
expression,
|
|
580
|
+
scope: item.scope,
|
|
581
|
+
variables,
|
|
582
|
+
membership: getMembership(expression, variables, environment),
|
|
583
|
+
};
|
|
584
|
+
}),
|
|
492
585
|
);
|
|
493
586
|
|
|
494
587
|
const isUnchangedFor = (stateCheck: StateCheck, state: Value[], parent: Value[] | undefined) => {
|
|
@@ -727,11 +820,13 @@ export const createSearch = (
|
|
|
727
820
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
728
821
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
729
822
|
}
|
|
730
|
-
const
|
|
731
|
-
const
|
|
732
|
-
|
|
733
|
-
|
|
823
|
+
const fingerprints = new BigInt64Array(fingerprintStates.length);
|
|
824
|
+
const fingerprint = createTlcFingerprinter(
|
|
825
|
+
getTlcOrder(),
|
|
826
|
+
new Int32Array(fingerprints.buffer),
|
|
734
827
|
);
|
|
828
|
+
fingerprintStates.forEach(fingerprint);
|
|
829
|
+
return fingerprints;
|
|
735
830
|
},
|
|
736
831
|
getFingerprintState: (state) => {
|
|
737
832
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
@@ -743,10 +838,16 @@ export const createSearch = (
|
|
|
743
838
|
invariants.find((invariant) => {
|
|
744
839
|
if (isUnchangedFor(invariant, state, parent)) return false;
|
|
745
840
|
try {
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
841
|
+
const context = getContext(environment, state, undefined, invariant.scope);
|
|
842
|
+
const membership = invariant.membership;
|
|
843
|
+
// The parent satisfies every invariant, so only the entries the step changed can fail.
|
|
844
|
+
return parent && membership
|
|
845
|
+
? !isUpdatedElementOf(
|
|
846
|
+
state[membership.variable],
|
|
847
|
+
parent[membership.variable],
|
|
848
|
+
evaluate(membership.set, context),
|
|
849
|
+
)
|
|
850
|
+
: !holdsLikeTlc(invariant.expression, context);
|
|
750
851
|
} catch (error) {
|
|
751
852
|
// TLC reports a failure only on a state after the initial states as an invariant failure.
|
|
752
853
|
if (!parent) throw error;
|
|
@@ -757,11 +858,17 @@ export const createSearch = (
|
|
|
757
858
|
}
|
|
758
859
|
}),
|
|
759
860
|
satisfiesConstraints: (state, parent) =>
|
|
760
|
-
constraints.every(
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
861
|
+
constraints.every((constraint) => {
|
|
862
|
+
try {
|
|
863
|
+
return (
|
|
864
|
+
isUnchangedFor(constraint, state, parent) ||
|
|
865
|
+
holdsLikeTlc(constraint.expression, getContext(environment, state, undefined))
|
|
866
|
+
);
|
|
867
|
+
} catch (error) {
|
|
868
|
+
// TLC reports a failing constraint on a successor like a failure of the next-state relation.
|
|
869
|
+
throw parent ? toNextStateError(error) : error;
|
|
870
|
+
}
|
|
871
|
+
}),
|
|
765
872
|
findFailedInitialPredicate: (state) =>
|
|
766
873
|
properties.initialPredicates.find(
|
|
767
874
|
(predicate) =>
|
|
@@ -787,18 +894,20 @@ export const createSearch = (
|
|
|
787
894
|
)
|
|
788
895
|
);
|
|
789
896
|
} catch (error) {
|
|
790
|
-
throw withHeadline(
|
|
897
|
+
throw withHeadline(
|
|
898
|
+
withNextStateExitCode(error),
|
|
899
|
+
`Evaluating action property ${property.name} failed.`,
|
|
900
|
+
);
|
|
791
901
|
}
|
|
792
902
|
}),
|
|
793
903
|
satisfiesActionConstraints: (state, next) =>
|
|
794
|
-
actionConstraints.every((constraint) =>
|
|
795
|
-
|
|
796
|
-
constraint.expression,
|
|
797
|
-
|
|
798
|
-
(
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
),
|
|
904
|
+
actionConstraints.every((constraint) => {
|
|
905
|
+
try {
|
|
906
|
+
return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
|
|
907
|
+
} catch (error) {
|
|
908
|
+
throw toNextStateError(error);
|
|
909
|
+
}
|
|
910
|
+
}),
|
|
802
911
|
trackStatePossible:
|
|
803
912
|
statePossible.length === 0
|
|
804
913
|
? ignorePossible
|
|
@@ -824,6 +933,64 @@ export const createSearch = (
|
|
|
824
933
|
};
|
|
825
934
|
};
|
|
826
935
|
|
|
936
|
+
// TLC exits with the code of a standard module's failure only when the failure escapes the
|
|
937
|
+
// computation of next states. In every other phase, the phase decides the code.
|
|
938
|
+
const withNextStateExitCode = (error: unknown) =>
|
|
939
|
+
error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
|
|
940
|
+
|
|
941
|
+
// TLC reports a failure of the next-state relation, or of the liveness check of a step, as an
|
|
942
|
+
// unexpected exception, unless a standard module raised it as an EvalException.
|
|
943
|
+
const toNextStateError = (error: unknown) =>
|
|
944
|
+
error instanceof TlaEvaluationError &&
|
|
945
|
+
!(error instanceof TlaModuleError) &&
|
|
946
|
+
!(error instanceof TlaIncompleteStateError)
|
|
947
|
+
? setExitCode(
|
|
948
|
+
new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
|
|
949
|
+
getErrorExitCode(error),
|
|
950
|
+
)
|
|
951
|
+
: withNextStateExitCode(error);
|
|
952
|
+
|
|
953
|
+
const isRepeated = (frame: string, index: number, frames: string[]) =>
|
|
954
|
+
index > 0 && frames[index - 1] === frame;
|
|
955
|
+
|
|
956
|
+
// FingerprintException#getTrace numbers the sources from the outermost value and lists the innermost
|
|
957
|
+
// first.
|
|
958
|
+
const getSourceTrace = (sources: Expression[]) =>
|
|
959
|
+
sources
|
|
960
|
+
.flatMap((source) => (source.location.range ? [formatRange(source.location.range)] : []))
|
|
961
|
+
.filter((source, index, all) => !isRepeated(source, index, all))
|
|
962
|
+
.map((source, index) => `${index}) ${source}`)
|
|
963
|
+
.reverse();
|
|
964
|
+
|
|
965
|
+
// TLC keeps the stack only when an evaluation fails, and prints each expression on it once, however
|
|
966
|
+
// many times in a row it pushed it.
|
|
967
|
+
const getReplayedCallStack = (
|
|
968
|
+
replay: () => void,
|
|
969
|
+
trace: Expression[],
|
|
970
|
+
): Pick<Replay, "callStack" | "fingerprint"> => {
|
|
971
|
+
try {
|
|
972
|
+
replay();
|
|
973
|
+
return { callStack: undefined };
|
|
974
|
+
} catch (error) {
|
|
975
|
+
if (!(error instanceof TlaEvaluationError)) return { callStack: [] };
|
|
976
|
+
const callStack = trace
|
|
977
|
+
.flatMap((expression) => {
|
|
978
|
+
const range = expression.location.range;
|
|
979
|
+
return range
|
|
980
|
+
? [
|
|
981
|
+
`Line ${range.start.line}, column ${range.start.column} to line ${range.end.line}, column ${range.end.column} in ${range.module}`,
|
|
982
|
+
]
|
|
983
|
+
: [];
|
|
984
|
+
})
|
|
985
|
+
.filter((frame, index, frames) => !isRepeated(frame, index, frames));
|
|
986
|
+
const sources = getFingerprintTrace(error);
|
|
987
|
+
return {
|
|
988
|
+
callStack,
|
|
989
|
+
fingerprint: sources && { sources: getSourceTrace(sources), reason: error.message },
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
|
|
827
994
|
const withHeadline = (error: unknown, headline: string) =>
|
|
828
995
|
error instanceof TlaEvaluationError
|
|
829
996
|
? setExitCode(
|
|
@@ -832,15 +999,30 @@ const withHeadline = (error: unknown, headline: string) =>
|
|
|
832
999
|
)
|
|
833
1000
|
: error;
|
|
834
1001
|
|
|
1002
|
+
// TLC finishes the -dump file whether or not the search finds a violation.
|
|
835
1003
|
export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
836
|
-
|
|
1004
|
+
const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
|
|
1005
|
+
try {
|
|
1006
|
+
return searchStates(options, stateWriter);
|
|
1007
|
+
} catch (error) {
|
|
1008
|
+
// TLC reports a stack overflow outside the search as a failure of its own, with exit code 255.
|
|
1009
|
+
throw isStackOverflow(error)
|
|
1010
|
+
? setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error)
|
|
1011
|
+
: error;
|
|
1012
|
+
} finally {
|
|
1013
|
+
stateWriter?.close();
|
|
1014
|
+
}
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
|
|
837
1018
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
838
1019
|
const onPrint = options.onPrint ?? console.log;
|
|
839
1020
|
// TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
|
|
840
1021
|
// before the trace would misplace them, so rebuilding a trace prints nothing.
|
|
841
|
-
const output = { isMuted: false };
|
|
1022
|
+
const output = { isMuted: false, replayed: undefined as string[] | undefined };
|
|
842
1023
|
const print = (text: string) => {
|
|
843
|
-
if (
|
|
1024
|
+
if (output.replayed) output.replayed.push(text);
|
|
1025
|
+
else if (!output.isMuted) onPrint(text);
|
|
844
1026
|
};
|
|
845
1027
|
const withoutOutput = <Result>(rebuild: () => Result) => {
|
|
846
1028
|
output.isMuted = true;
|
|
@@ -902,9 +1084,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
902
1084
|
),
|
|
903
1085
|
});
|
|
904
1086
|
|
|
905
|
-
const
|
|
906
|
-
|
|
907
|
-
);
|
|
1087
|
+
const getVariableOrder = () =>
|
|
1088
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "variable");
|
|
1089
|
+
const search = createSearch(config, environment, getVariableOrder);
|
|
908
1090
|
const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
|
|
909
1091
|
if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
|
|
910
1092
|
if (!search) return createSuccess();
|
|
@@ -928,9 +1110,114 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
928
1110
|
const spec = search.spec;
|
|
929
1111
|
const getFingerprintState = search.getFingerprintState;
|
|
930
1112
|
const nextActions = { value: undefined as SpecAction[] | undefined };
|
|
1113
|
+
const replayFailure = (
|
|
1114
|
+
result: CheckResult,
|
|
1115
|
+
replay: (tracedSearch: Search, traced: Environment) => void,
|
|
1116
|
+
isBeforeError = false,
|
|
1117
|
+
): CheckResult => {
|
|
1118
|
+
if (!result.failure) return result;
|
|
1119
|
+
const trace: Expression[] = [];
|
|
1120
|
+
const traced: Environment = { ...environment, evaluators: new WeakMap(), trace };
|
|
1121
|
+
const tracedSearch = createSearch(config, traced, getVariableOrder);
|
|
1122
|
+
if (!tracedSearch) return result;
|
|
1123
|
+
const replayed: string[] = [];
|
|
1124
|
+
output.replayed = replayed;
|
|
1125
|
+
valueSources.current = new WeakMap();
|
|
1126
|
+
const replayedStack = getReplayedCallStack(() => replay(tracedSearch, traced), trace);
|
|
1127
|
+
valueSources.current = undefined;
|
|
1128
|
+
output.replayed = undefined;
|
|
1129
|
+
return {
|
|
1130
|
+
...result,
|
|
1131
|
+
failure: { ...result.failure, replay: { output: replayed, ...replayedStack, isBeforeError } },
|
|
1132
|
+
};
|
|
1133
|
+
};
|
|
1134
|
+
// TLC fingerprints a state outside CallStackTool, so a failure of a value with a source leaves
|
|
1135
|
+
// the stack empty. Any other failure keeps the stack of the initial predicate.
|
|
1136
|
+
const fingerprintReplayed = (state: Value[], traced: Environment) => {
|
|
1137
|
+
try {
|
|
1138
|
+
getStateHash(getFingerprintState(state));
|
|
1139
|
+
} catch (error) {
|
|
1140
|
+
if (traced.trace && getFingerprintTrace(error)) traced.trace.length = 0;
|
|
1141
|
+
throw error;
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
// TLC checks every initial state again, as when it first generated them.
|
|
1145
|
+
const replayInitialStates = (result: CheckResult) =>
|
|
1146
|
+
replayFailure(result, (tracedSearch, traced) => {
|
|
1147
|
+
if (spec.init === undefined) return;
|
|
1148
|
+
forEachInitialState(
|
|
1149
|
+
spec.init,
|
|
1150
|
+
traced,
|
|
1151
|
+
(state) => {
|
|
1152
|
+
if (tracedSearch.satisfiesConstraints(state)) fingerprintReplayed(state, traced);
|
|
1153
|
+
return (
|
|
1154
|
+
tracedSearch.findViolatedInvariant(state) !== undefined ||
|
|
1155
|
+
tracedSearch.findFailedInitialPredicate(state) !== undefined
|
|
1156
|
+
);
|
|
1157
|
+
},
|
|
1158
|
+
spec.scope,
|
|
1159
|
+
);
|
|
1160
|
+
});
|
|
1161
|
+
// TLC rebuilds the behavior up to the state it expanded before it replays the step.
|
|
1162
|
+
const rebuildBehavior = (index: number): void => {
|
|
1163
|
+
const parent = parents[index];
|
|
1164
|
+
if (parent === -1) {
|
|
1165
|
+
getSpecInitialStates();
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
rebuildBehavior(parent);
|
|
1169
|
+
getLabel(parent, states[parent], states[index]);
|
|
1170
|
+
};
|
|
1171
|
+
// TLC generates the successors of each action before it checks them. It checks the invariants
|
|
1172
|
+
// only of a successor outside the model, since it has seen the others.
|
|
1173
|
+
const replaySuccessors = (result: CheckResult, index: number) =>
|
|
1174
|
+
replayFailure(result, (tracedSearch, traced) => {
|
|
1175
|
+
rebuildBehavior(index);
|
|
1176
|
+
const state = states[index];
|
|
1177
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1178
|
+
nextActions.value.find((action) =>
|
|
1179
|
+
getSuccessors(action.expression, state, traced, action.scope).some((successor) => {
|
|
1180
|
+
const isInModel =
|
|
1181
|
+
tracedSearch.satisfiesConstraints(successor.state) &&
|
|
1182
|
+
tracedSearch.satisfiesActionConstraints(state, successor.state);
|
|
1183
|
+
if (isInModel) fingerprintReplayed(successor.state, traced);
|
|
1184
|
+
return (
|
|
1185
|
+
(!isInModel && tracedSearch.findViolatedInvariant(successor.state) !== undefined) ||
|
|
1186
|
+
tracedSearch.findViolatedActionProperty(state, successor.state) !== undefined
|
|
1187
|
+
);
|
|
1188
|
+
}),
|
|
1189
|
+
);
|
|
1190
|
+
});
|
|
931
1191
|
const isReducing = config.view !== undefined || config.symmetry !== undefined;
|
|
1192
|
+
const tlcFingerprints: number[] = [];
|
|
1193
|
+
const liveGraph: LiveGraph = {
|
|
1194
|
+
getFingerprint: (index) =>
|
|
1195
|
+
(tlcFingerprints[index] ??= Number(BigInt.asIntN(32, runtime.getFingerprint(states[index])))),
|
|
1196
|
+
getSuccessors: (state) => {
|
|
1197
|
+
const successors: LiveSuccessor[] = [];
|
|
1198
|
+
if (spec.next === undefined) return successors;
|
|
1199
|
+
forEachSuccessor(
|
|
1200
|
+
spec.next,
|
|
1201
|
+
state,
|
|
1202
|
+
environment,
|
|
1203
|
+
(successor) => {
|
|
1204
|
+
canonicalizeState(successor.state);
|
|
1205
|
+
const isInModel =
|
|
1206
|
+
search.satisfiesConstraints(successor.state, state) &&
|
|
1207
|
+
search.satisfiesActionConstraints(state, successor.state);
|
|
1208
|
+
if (!isInModel) return false;
|
|
1209
|
+
const fingerprintState = getFingerprintState(successor.state);
|
|
1210
|
+
const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
|
|
1211
|
+
successors.push({ index, state: successor.state });
|
|
1212
|
+
return false;
|
|
1213
|
+
},
|
|
1214
|
+
spec.scope,
|
|
1215
|
+
);
|
|
1216
|
+
return successors;
|
|
1217
|
+
},
|
|
1218
|
+
};
|
|
932
1219
|
const liveness = withLivenessExitCode(() =>
|
|
933
|
-
createLivenessChecker(search.properties.liveness, spec.temporals, environment),
|
|
1220
|
+
createLivenessChecker(search.properties.liveness, spec.temporals, environment, liveGraph),
|
|
934
1221
|
);
|
|
935
1222
|
const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
|
|
936
1223
|
|
|
@@ -939,7 +1226,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
939
1226
|
const parents: number[] = [];
|
|
940
1227
|
const actions: (string | undefined)[] = [];
|
|
941
1228
|
const depths: number[] = [];
|
|
942
|
-
|
|
1229
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1230
|
+
// and its index plus one side by side, probed by the hash.
|
|
1231
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
943
1232
|
const canonicalizeState = createCanonicalizer();
|
|
944
1233
|
|
|
945
1234
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -952,11 +1241,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
952
1241
|
position: number,
|
|
953
1242
|
loopStart: number | undefined,
|
|
954
1243
|
) => {
|
|
955
|
-
const state = steps[position].state;
|
|
956
|
-
const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
|
|
957
1244
|
// TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
|
|
958
1245
|
runtime.getLevel = () => position + 1;
|
|
959
1246
|
runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
|
|
1247
|
+
return evaluateAlias(
|
|
1248
|
+
alias,
|
|
1249
|
+
steps[position].state,
|
|
1250
|
+
(steps[position + 1] ?? steps[loopStart ?? position]).state,
|
|
1251
|
+
);
|
|
1252
|
+
};
|
|
1253
|
+
const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
|
|
960
1254
|
try {
|
|
961
1255
|
const body = getConfigDefinition("alias", alias, environment).body;
|
|
962
1256
|
const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
|
|
@@ -1058,7 +1352,17 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1058
1352
|
trace: TraceStep[],
|
|
1059
1353
|
initialState?: Value[],
|
|
1060
1354
|
failureOutDegrees?: number[],
|
|
1061
|
-
) => {
|
|
1355
|
+
): CheckResult => {
|
|
1356
|
+
// TLC exits with 255 on a stack overflow while it computes next states, and prints the
|
|
1357
|
+
// behavior up to the state it was expanding.
|
|
1358
|
+
if (isStackOverflow(error)) {
|
|
1359
|
+
return current.isExpanding
|
|
1360
|
+
? toFailure(
|
|
1361
|
+
setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error),
|
|
1362
|
+
getStateTrace(current.index),
|
|
1363
|
+
)
|
|
1364
|
+
: toFailure(new TlaEvaluationError(STACK_OVERFLOW), trace, initialState);
|
|
1365
|
+
}
|
|
1062
1366
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1063
1367
|
return {
|
|
1064
1368
|
...createResult("error", undefined, {
|
|
@@ -1071,7 +1375,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1071
1375
|
outDegrees: failureOutDegrees,
|
|
1072
1376
|
};
|
|
1073
1377
|
};
|
|
1074
|
-
const
|
|
1378
|
+
const getPathSteps = (path: number[], loopStart?: number) => {
|
|
1075
1379
|
const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
|
|
1076
1380
|
const last = steps[steps.length - 1].state;
|
|
1077
1381
|
const isClosed =
|
|
@@ -1080,12 +1384,13 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1080
1384
|
loopStart === path.length - 1 ||
|
|
1081
1385
|
findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
|
|
1082
1386
|
if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
|
|
1083
|
-
return
|
|
1387
|
+
return steps;
|
|
1084
1388
|
};
|
|
1085
1389
|
const getStateHash = (state: Value[]) => {
|
|
1086
1390
|
let hash = 0x811c9dc5;
|
|
1087
1391
|
for (let index = 0; index < state.length; index++)
|
|
1088
1392
|
hash = combineHash(hash, getValueHash(state[index]));
|
|
1393
|
+
assertFingerprintable(state);
|
|
1089
1394
|
return hash & 0x3fffffff;
|
|
1090
1395
|
};
|
|
1091
1396
|
const isSameState = (stored: Value[], state: Value[]) => {
|
|
@@ -1094,19 +1399,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1094
1399
|
}
|
|
1095
1400
|
return true;
|
|
1096
1401
|
};
|
|
1402
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1097
1403
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1098
|
-
const
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1404
|
+
const mask = seen.slots.length - 1;
|
|
1405
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1406
|
+
const entry = seen.slots[slot + 1];
|
|
1407
|
+
if (entry === 0) return -1;
|
|
1408
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1409
|
+
return entry - 1;
|
|
1410
|
+
}
|
|
1102
1411
|
}
|
|
1103
|
-
|
|
1412
|
+
};
|
|
1413
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1414
|
+
const mask = seen.slots.length - 1;
|
|
1415
|
+
let slot = getSlot(hash);
|
|
1416
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1417
|
+
seen.slots[slot] = hash;
|
|
1418
|
+
seen.slots[slot + 1] = entry;
|
|
1104
1419
|
};
|
|
1105
1420
|
const markSeen = (hash: number, index: number) => {
|
|
1106
|
-
|
|
1107
|
-
if (
|
|
1108
|
-
|
|
1109
|
-
|
|
1421
|
+
seen.size++;
|
|
1422
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1423
|
+
const slots = seen.slots;
|
|
1424
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1425
|
+
seen.shift--;
|
|
1426
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1427
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
insertSeen(hash, index + 1);
|
|
1110
1431
|
};
|
|
1111
1432
|
const countDistinctState = (depth: number) => {
|
|
1112
1433
|
runtime.distinctStates = states.length + 1;
|
|
@@ -1132,6 +1453,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1132
1453
|
isExpanding: false,
|
|
1133
1454
|
candidate: undefined as Value[] | undefined,
|
|
1134
1455
|
candidateLevel: 0,
|
|
1456
|
+
isFingerprinting: false,
|
|
1135
1457
|
livenessSource: -1,
|
|
1136
1458
|
};
|
|
1137
1459
|
const getPath = (index: number): Value[][] =>
|
|
@@ -1146,7 +1468,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1146
1468
|
};
|
|
1147
1469
|
runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
|
|
1148
1470
|
runtime.setLivenessSource = (index) => {
|
|
1149
|
-
current.livenessSource = index;
|
|
1471
|
+
current.livenessSource = index ?? -1;
|
|
1150
1472
|
};
|
|
1151
1473
|
runtime.getLevel = (state) => {
|
|
1152
1474
|
const source = current.livenessSource;
|
|
@@ -1157,6 +1479,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1157
1479
|
return current.isExpanding ? depths[current.index] + 1 : 0;
|
|
1158
1480
|
};
|
|
1159
1481
|
|
|
1482
|
+
const getGraphState = (state: Value[], level: number): GraphState => ({
|
|
1483
|
+
fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
|
|
1484
|
+
level,
|
|
1485
|
+
getLabel: () =>
|
|
1486
|
+
formatState(
|
|
1487
|
+
config.alias === undefined
|
|
1488
|
+
? toRecord(state)
|
|
1489
|
+
: evaluateAlias(config.alias, state, undefined),
|
|
1490
|
+
),
|
|
1491
|
+
getText: () => formatState(toRecord(state)),
|
|
1492
|
+
});
|
|
1493
|
+
|
|
1160
1494
|
const visitInitialState = (state: Value[]) => {
|
|
1161
1495
|
canonicalizeState(state);
|
|
1162
1496
|
runtime.generatedStates++;
|
|
@@ -1173,6 +1507,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1173
1507
|
countDistinctState(1);
|
|
1174
1508
|
runtime.queueSize++;
|
|
1175
1509
|
liveness?.addInitialState(states.length, state);
|
|
1510
|
+
stateWriter?.writeInitialState(getGraphState(state, 1));
|
|
1176
1511
|
}
|
|
1177
1512
|
const violated = search.findViolatedInvariant(state);
|
|
1178
1513
|
if (violated) {
|
|
@@ -1193,20 +1528,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1193
1528
|
addState(state, fingerprintState, -1, undefined);
|
|
1194
1529
|
return undefined;
|
|
1195
1530
|
};
|
|
1196
|
-
const visitSuccessor = (
|
|
1531
|
+
const visitSuccessor = (
|
|
1532
|
+
index: number,
|
|
1533
|
+
successor: Successor,
|
|
1534
|
+
liveSuccessors: LiveSuccessor[],
|
|
1535
|
+
source: GraphState | undefined,
|
|
1536
|
+
action: SpecAction | undefined,
|
|
1537
|
+
) => {
|
|
1197
1538
|
const state = states[index];
|
|
1198
1539
|
runtime.generatedStates++;
|
|
1199
1540
|
current.candidate = successor.state;
|
|
1200
1541
|
current.candidateLevel = depths[index] + 1;
|
|
1201
1542
|
if (isCanonicalizedFirst) canonicalizeState(successor.state);
|
|
1543
|
+
current.isFingerprinting = true;
|
|
1202
1544
|
const fingerprintState = getFingerprintState(successor.state);
|
|
1203
1545
|
const hash = getStateHash(fingerprintState);
|
|
1546
|
+
current.isFingerprinting = false;
|
|
1204
1547
|
const found = findStateIndex(hash, fingerprintState);
|
|
1205
1548
|
const isStateInModel = search.satisfiesConstraints(successor.state, state);
|
|
1206
1549
|
if (isStateInModel) search.trackStatePossible(successor.state);
|
|
1207
1550
|
const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
|
|
1208
1551
|
if (isInModel) search.trackActionPossible(state, successor.state);
|
|
1209
1552
|
const existing = isInModel ? found : -1;
|
|
1553
|
+
if (source && isInModel) {
|
|
1554
|
+
stateWriter?.writeSuccessor(
|
|
1555
|
+
source,
|
|
1556
|
+
getGraphState(successor.state, depths[index] + 1),
|
|
1557
|
+
existing !== -1,
|
|
1558
|
+
action && { name: action.name, signature: getActionSignature(action) },
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1210
1561
|
if (existing !== -1 && liveness) {
|
|
1211
1562
|
const stored = states[existing];
|
|
1212
1563
|
const isStored = stored.every((value, position) => value === successor.state[position]);
|
|
@@ -1264,10 +1615,22 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1264
1615
|
spec.scope,
|
|
1265
1616
|
);
|
|
1266
1617
|
} catch (error) {
|
|
1267
|
-
|
|
1618
|
+
if (!(error instanceof TlaIllegalStateError))
|
|
1619
|
+
return replayInitialStates(toFailure(error, []));
|
|
1620
|
+
// TLC prints an illegal initial state with null for each variable it leaves undefined, and
|
|
1621
|
+
// stops before it prints the statistics.
|
|
1622
|
+
const illegal = toFailure(
|
|
1623
|
+
error,
|
|
1624
|
+
[],
|
|
1625
|
+
error.state.map((value) => value ?? createModelValue("null")),
|
|
1626
|
+
);
|
|
1627
|
+
return {
|
|
1628
|
+
...illegal,
|
|
1629
|
+
failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
|
|
1630
|
+
};
|
|
1268
1631
|
}
|
|
1269
1632
|
}
|
|
1270
|
-
if (outcome.result) return outcome.result;
|
|
1633
|
+
if (outcome.result) return replayInitialStates(outcome.result);
|
|
1271
1634
|
|
|
1272
1635
|
const next = spec.next;
|
|
1273
1636
|
if (next === undefined) {
|
|
@@ -1278,23 +1641,58 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1278
1641
|
}
|
|
1279
1642
|
return createSuccess();
|
|
1280
1643
|
}
|
|
1644
|
+
// TLC labels and colors an edge of the -dump state graph by the action that generated it. When
|
|
1645
|
+
// the graph names its edges, the search generates successors action by action, as TLC does.
|
|
1646
|
+
const isNamingEdges = Boolean(
|
|
1647
|
+
options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
|
|
1648
|
+
);
|
|
1649
|
+
const forEachActionSuccessor = (
|
|
1650
|
+
state: Value[],
|
|
1651
|
+
onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
|
|
1652
|
+
) => {
|
|
1653
|
+
if (!isNamingEdges) {
|
|
1654
|
+
return forEachSuccessor(
|
|
1655
|
+
next,
|
|
1656
|
+
state,
|
|
1657
|
+
environment,
|
|
1658
|
+
(successor) => onSuccessor(successor, undefined),
|
|
1659
|
+
spec.scope,
|
|
1660
|
+
);
|
|
1661
|
+
}
|
|
1662
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1663
|
+
const progress = { isStopped: false };
|
|
1664
|
+
return nextActions.value.reduce((count, action) => {
|
|
1665
|
+
if (progress.isStopped) return count;
|
|
1666
|
+
return (
|
|
1667
|
+
count +
|
|
1668
|
+
forEachSuccessor(
|
|
1669
|
+
action.expression,
|
|
1670
|
+
state,
|
|
1671
|
+
environment,
|
|
1672
|
+
(successor) => {
|
|
1673
|
+
progress.isStopped = onSuccessor(successor, action);
|
|
1674
|
+
return progress.isStopped;
|
|
1675
|
+
},
|
|
1676
|
+
action.scope,
|
|
1677
|
+
)
|
|
1678
|
+
);
|
|
1679
|
+
}, 0);
|
|
1680
|
+
};
|
|
1281
1681
|
const expandState = (index: number) => {
|
|
1282
1682
|
const state = states[index];
|
|
1283
1683
|
const liveSuccessors: LiveSuccessor[] = [];
|
|
1284
|
-
const
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
(
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
spec.scope,
|
|
1297
|
-
);
|
|
1684
|
+
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1685
|
+
const successorCount = forEachActionSuccessor(state, (successor, action) => {
|
|
1686
|
+
try {
|
|
1687
|
+
outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
|
|
1688
|
+
} catch (error) {
|
|
1689
|
+
// TLC reports a successor it cannot fingerprint as a failure of the state it expands.
|
|
1690
|
+
if (current.isFingerprinting) throw error;
|
|
1691
|
+
const failure = toFailure(error, getTrace(index, successor));
|
|
1692
|
+
outcome.result = isStackOverflow(error) ? failure : replaySuccessors(failure, index);
|
|
1693
|
+
}
|
|
1694
|
+
return outcome.result !== undefined;
|
|
1695
|
+
});
|
|
1298
1696
|
current.isExpanding = false;
|
|
1299
1697
|
if (outcome.result) return outcome.result;
|
|
1300
1698
|
if (successorCount === 0 && config.checkDeadlock) {
|
|
@@ -1302,32 +1700,78 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1302
1700
|
}
|
|
1303
1701
|
try {
|
|
1304
1702
|
const accepted = liveness?.addState(index, state, liveSuccessors);
|
|
1305
|
-
|
|
1306
|
-
|
|
1703
|
+
if (!liveness || !accepted) return undefined;
|
|
1704
|
+
const steps = getPathSteps(accepted);
|
|
1705
|
+
const properties = liveness.getViolatedProperties(
|
|
1706
|
+
steps.map((step) => step.state),
|
|
1707
|
+
steps.length - 1,
|
|
1708
|
+
);
|
|
1307
1709
|
return reportViolation("property-violation", {
|
|
1308
|
-
name:
|
|
1309
|
-
|
|
1710
|
+
name: properties[0],
|
|
1711
|
+
properties,
|
|
1712
|
+
trace: toTrace(steps, accepted),
|
|
1310
1713
|
});
|
|
1311
1714
|
} catch (error) {
|
|
1312
|
-
return
|
|
1715
|
+
return replayFailure(
|
|
1716
|
+
toFailure(toNextStateError(error), getStateTrace(index), undefined, getPartialOutDegrees()),
|
|
1717
|
+
(_, traced) =>
|
|
1718
|
+
createLivenessChecker(
|
|
1719
|
+
search.properties.liveness,
|
|
1720
|
+
spec.temporals,
|
|
1721
|
+
traced,
|
|
1722
|
+
liveGraph,
|
|
1723
|
+
)?.addState(index, state, liveSuccessors),
|
|
1724
|
+
true,
|
|
1725
|
+
);
|
|
1313
1726
|
}
|
|
1314
1727
|
};
|
|
1315
|
-
|
|
1728
|
+
// TLC names the action when the next-state relation has more than one, and prints the
|
|
1729
|
+
// incomplete successor with null for each variable it leaves undefined.
|
|
1730
|
+
const reportIncompleteSuccessor = (error: TlaIncompleteStateError, index: number) => {
|
|
1731
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1732
|
+
const actions = nextActions.value;
|
|
1733
|
+
const action =
|
|
1734
|
+
actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
|
|
1735
|
+
const variables = `variable${error.missing.length > 1 ? "s are" : " is"} not defined: ${error.missing.join(", ")}.\n`;
|
|
1736
|
+
runtime.generatedStates++;
|
|
1737
|
+
return toFailure(
|
|
1738
|
+
actions.length === 1
|
|
1739
|
+
? error
|
|
1740
|
+
: new TlaEvaluationError(
|
|
1741
|
+
`Successor state is not completely specified by action ${error.action} of the next-state relation. The following ${variables}`,
|
|
1742
|
+
),
|
|
1743
|
+
[
|
|
1744
|
+
...getStateTrace(index),
|
|
1745
|
+
{
|
|
1746
|
+
action: error.action,
|
|
1747
|
+
label: action && getActionLabel(action),
|
|
1748
|
+
state: toRecord(error.state.map((value) => value ?? createModelValue("null"))),
|
|
1749
|
+
},
|
|
1750
|
+
],
|
|
1751
|
+
);
|
|
1752
|
+
};
|
|
1753
|
+
// TLC names the properties and the step back into the cycle from the states it prints.
|
|
1754
|
+
const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
|
|
1316
1755
|
const path = [...lasso.prefix, ...lasso.cycle];
|
|
1317
|
-
const
|
|
1318
|
-
const
|
|
1756
|
+
const loopStart = lasso.prefix.length;
|
|
1757
|
+
const steps = getPathSteps(path, loopStart);
|
|
1758
|
+
const trace = toTrace(steps, path, loopStart);
|
|
1759
|
+
const properties = checker.getViolatedProperties(
|
|
1760
|
+
steps.map((step) => step.state),
|
|
1761
|
+
loopStart,
|
|
1762
|
+
);
|
|
1763
|
+
const back = path[loopStart];
|
|
1319
1764
|
const last = path[path.length - 1];
|
|
1765
|
+
const lastState = steps[steps.length - 1].state;
|
|
1320
1766
|
const loopLabel =
|
|
1321
|
-
back === last ? undefined : withoutOutput(() => getLabel(last,
|
|
1767
|
+
back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
|
|
1322
1768
|
return reportViolation(
|
|
1323
1769
|
"liveness-violation",
|
|
1324
|
-
{ name:
|
|
1770
|
+
{ name: properties[0], properties, trace, loopStart, loopLabel },
|
|
1325
1771
|
() =>
|
|
1326
1772
|
back === last
|
|
1327
1773
|
? trace[trace.length - 1].action
|
|
1328
|
-
:
|
|
1329
|
-
isSameState(successor.state, states[back]),
|
|
1330
|
-
)?.action,
|
|
1774
|
+
: findSuccessorWithFingerprint(lastState, back)?.action,
|
|
1331
1775
|
);
|
|
1332
1776
|
};
|
|
1333
1777
|
const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
|
|
@@ -1340,11 +1784,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1340
1784
|
if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
|
|
1341
1785
|
periodic.size = expanded;
|
|
1342
1786
|
const lasso = liveness?.findViolation();
|
|
1343
|
-
current.livenessSource = -1;
|
|
1344
1787
|
periodic.time += performance.now() - now;
|
|
1345
1788
|
return lasso;
|
|
1346
1789
|
};
|
|
1347
|
-
setNextStatePhase(true);
|
|
1348
1790
|
for (let index = 0; index < states.length && !runtime.isStopped; index++) {
|
|
1349
1791
|
current.index = index;
|
|
1350
1792
|
current.candidate = undefined;
|
|
@@ -1353,22 +1795,28 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1353
1795
|
try {
|
|
1354
1796
|
outcome.result = expandState(index);
|
|
1355
1797
|
} catch (error) {
|
|
1356
|
-
|
|
1798
|
+
const failure =
|
|
1799
|
+
error instanceof TlaIncompleteStateError
|
|
1800
|
+
? reportIncompleteSuccessor(error, index)
|
|
1801
|
+
: toFailure(toNextStateError(error), getStateTrace(index));
|
|
1802
|
+
outcome.result =
|
|
1803
|
+
error instanceof TlaIncompleteStateError || isStackOverflow(error)
|
|
1804
|
+
? failure
|
|
1805
|
+
: replaySuccessors(failure, index);
|
|
1357
1806
|
}
|
|
1358
1807
|
if (outcome.result) return outcome.result;
|
|
1359
1808
|
outDegrees[getOutDegreeBucket()]++;
|
|
1360
1809
|
expansion.newStates = 0;
|
|
1361
1810
|
if (isPeriodic && performance.now() >= periodic.at) {
|
|
1362
1811
|
const lasso = findPeriodicViolation(index + 1);
|
|
1363
|
-
if (lasso) return reportLasso(lasso);
|
|
1812
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1364
1813
|
}
|
|
1365
1814
|
}
|
|
1366
1815
|
|
|
1367
1816
|
current.index = -1;
|
|
1368
1817
|
current.candidate = undefined;
|
|
1369
1818
|
const lasso = liveness?.findViolation();
|
|
1370
|
-
|
|
1371
|
-
if (lasso) return reportLasso(lasso);
|
|
1819
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1372
1820
|
|
|
1373
1821
|
const postconditionViolation = search.findPostconditionViolation();
|
|
1374
1822
|
if (postconditionViolation) {
|