tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.868dd4c
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 +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- 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 +17 -16
- package/dist/run-cli.js +6 -5
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +259 -76
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +88 -20
- package/src/errors.ts +18 -2
- package/src/evaluate.ts +117 -147
- package/src/exit-codes.ts +21 -43
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +770 -0
- package/src/liveness.ts +54 -535
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +12 -1
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +58 -13
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +78 -14
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +128 -24
package/src/check.ts
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
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
|
+
TlaConfigError,
|
|
15
|
+
TlaEvaluationError,
|
|
16
|
+
TlaIncompleteStateError,
|
|
17
|
+
TlaModuleError,
|
|
18
|
+
UNEXPECTED_EXCEPTION,
|
|
19
|
+
} from "./errors";
|
|
20
|
+
import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
|
|
7
21
|
import {
|
|
8
22
|
describeRange,
|
|
9
23
|
evaluate,
|
|
10
24
|
expectBoolean,
|
|
11
25
|
formatRange,
|
|
12
|
-
getChildren,
|
|
13
26
|
getDefinition,
|
|
14
27
|
isConstantLevel,
|
|
15
28
|
isImpure,
|
|
16
|
-
isLocallyResolved,
|
|
17
29
|
type Context,
|
|
18
30
|
type Environment,
|
|
19
31
|
type Scope,
|
|
20
32
|
} from "./evaluate";
|
|
21
33
|
import {
|
|
22
34
|
createLivenessChecker,
|
|
23
|
-
expandTemporal,
|
|
24
|
-
getLevel,
|
|
25
35
|
type Lasso,
|
|
36
|
+
type LivenessChecker,
|
|
26
37
|
type LiveSuccessor,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} from "./liveness";
|
|
38
|
+
} from "./live-check";
|
|
39
|
+
import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
|
|
30
40
|
import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
|
|
31
41
|
import { prettyPrint } from "./pretty-print";
|
|
32
42
|
import {
|
|
@@ -38,12 +48,19 @@ import {
|
|
|
38
48
|
} from "./successors";
|
|
39
49
|
import {
|
|
40
50
|
getActionLabel,
|
|
51
|
+
getActionSignature,
|
|
41
52
|
getNextActions,
|
|
42
53
|
getSpecificationValue,
|
|
43
54
|
type SpecAction,
|
|
44
55
|
} from "./spec-description";
|
|
56
|
+
import {
|
|
57
|
+
createStateWriter,
|
|
58
|
+
type GraphState,
|
|
59
|
+
type StateDump,
|
|
60
|
+
type StateWriter,
|
|
61
|
+
} from "./state-graph";
|
|
45
62
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
46
|
-
import { getTlcStringOrder,
|
|
63
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
47
64
|
import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
|
|
48
65
|
import { createCanonicalizer } from "./canonical";
|
|
49
66
|
import { getPossibleCounts, type CheckerRuntime } from "./runtime";
|
|
@@ -51,6 +68,7 @@ import { createCounterExample } from "./tlc-ext";
|
|
|
51
68
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
52
69
|
import {
|
|
53
70
|
combineHash,
|
|
71
|
+
formatState,
|
|
54
72
|
formatValue,
|
|
55
73
|
getValueHash,
|
|
56
74
|
isFunctionValue,
|
|
@@ -74,6 +92,8 @@ export interface CheckOptions {
|
|
|
74
92
|
workers?: number;
|
|
75
93
|
// TLC's -lncheck: "final" checks liveness only on the complete state graph.
|
|
76
94
|
livenessCheck?: "default" | "final";
|
|
95
|
+
// TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
|
|
96
|
+
dump?: StateDump;
|
|
77
97
|
}
|
|
78
98
|
|
|
79
99
|
export type CheckStatus =
|
|
@@ -95,6 +115,8 @@ export interface TraceStep {
|
|
|
95
115
|
|
|
96
116
|
export interface Violation {
|
|
97
117
|
name: string;
|
|
118
|
+
// Every temporal property that a liveness counterexample violates, in the configuration's order.
|
|
119
|
+
properties?: string[];
|
|
98
120
|
trace: TraceStep[];
|
|
99
121
|
loopStart: number | undefined;
|
|
100
122
|
loopLabel?: string;
|
|
@@ -255,8 +277,11 @@ const getInvariants = (names: string[], environment: Environment) => {
|
|
|
255
277
|
levels.params.size === 0
|
|
256
278
|
? ""
|
|
257
279
|
: "\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.";
|
|
258
|
-
throw
|
|
259
|
-
|
|
280
|
+
throw setExitCode(
|
|
281
|
+
new TlaConfigError(
|
|
282
|
+
`The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
|
|
283
|
+
),
|
|
284
|
+
EXIT_STATUS.safetyEvaluationFailure,
|
|
260
285
|
);
|
|
261
286
|
}
|
|
262
287
|
return [{ name, expression: createIdentifier(name), scope: undefined }];
|
|
@@ -425,12 +450,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
425
450
|
config.properties.forEach((name) => {
|
|
426
451
|
const definition = getConfigPredicate("property", name, environment);
|
|
427
452
|
if (!definition) return;
|
|
428
|
-
expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
|
|
453
|
+
expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
|
|
429
454
|
const conjunct = item.expression;
|
|
430
455
|
if (isConstantTrue(item, environment, "property")) return;
|
|
431
456
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
432
457
|
properties.actionProperties.push({
|
|
433
|
-
name,
|
|
458
|
+
name: item.name,
|
|
434
459
|
box: conjunct.expression,
|
|
435
460
|
action: conjunct.expression.action,
|
|
436
461
|
subscript: conjunct.expression.subscript,
|
|
@@ -442,12 +467,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
442
467
|
conjunct.kind === "Always" &&
|
|
443
468
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
444
469
|
) {
|
|
445
|
-
|
|
470
|
+
// TLC names the invariant []P after P when P is an operator name.
|
|
471
|
+
const expression = conjunct.expression;
|
|
472
|
+
properties.invariants.push({
|
|
473
|
+
name: expression.kind === "Identifier" ? expression.name : item.name,
|
|
474
|
+
expression,
|
|
475
|
+
scope: item.scope,
|
|
476
|
+
});
|
|
446
477
|
return;
|
|
447
478
|
}
|
|
448
479
|
const level = getLevel(conjunct, item.scope, environment);
|
|
449
480
|
if (level < ACTION_LEVEL) {
|
|
450
|
-
properties.initialPredicates.push({
|
|
481
|
+
properties.initialPredicates.push({
|
|
482
|
+
name: item.name,
|
|
483
|
+
expression: conjunct,
|
|
484
|
+
scope: item.scope,
|
|
485
|
+
});
|
|
451
486
|
return;
|
|
452
487
|
}
|
|
453
488
|
if (level === ACTION_LEVEL) {
|
|
@@ -507,7 +542,9 @@ const getMembership = (
|
|
|
507
542
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
508
543
|
named.flatMap((item) =>
|
|
509
544
|
getConjuncts(item.expression, environment).map((expression) => {
|
|
510
|
-
|
|
545
|
+
// A LET or an argument in the scope can read variables the expression doesn't name.
|
|
546
|
+
const variables =
|
|
547
|
+
item.scope === undefined ? getReadVariables(expression, environment) : undefined;
|
|
511
548
|
return {
|
|
512
549
|
name: item.name,
|
|
513
550
|
expression,
|
|
@@ -754,11 +791,13 @@ export const createSearch = (
|
|
|
754
791
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
755
792
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
756
793
|
}
|
|
757
|
-
const
|
|
758
|
-
const
|
|
759
|
-
|
|
760
|
-
|
|
794
|
+
const fingerprints = new BigInt64Array(fingerprintStates.length);
|
|
795
|
+
const fingerprint = createTlcFingerprinter(
|
|
796
|
+
getTlcOrder(),
|
|
797
|
+
new Int32Array(fingerprints.buffer),
|
|
761
798
|
);
|
|
799
|
+
fingerprintStates.forEach(fingerprint);
|
|
800
|
+
return fingerprints;
|
|
762
801
|
},
|
|
763
802
|
getFingerprintState: (state) => {
|
|
764
803
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
@@ -820,7 +859,10 @@ export const createSearch = (
|
|
|
820
859
|
)
|
|
821
860
|
);
|
|
822
861
|
} catch (error) {
|
|
823
|
-
throw withHeadline(
|
|
862
|
+
throw withHeadline(
|
|
863
|
+
withNextStateExitCode(error),
|
|
864
|
+
`Evaluating action property ${property.name} failed.`,
|
|
865
|
+
);
|
|
824
866
|
}
|
|
825
867
|
}),
|
|
826
868
|
satisfiesActionConstraints: (state, next) =>
|
|
@@ -828,8 +870,7 @@ export const createSearch = (
|
|
|
828
870
|
holdsLikeTlc(
|
|
829
871
|
constraint.expression,
|
|
830
872
|
getContext(environment, state, next),
|
|
831
|
-
(expression) =>
|
|
832
|
-
`TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ${getExpectedBoolean(expression, environment)}`,
|
|
873
|
+
(expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
|
|
833
874
|
),
|
|
834
875
|
),
|
|
835
876
|
trackStatePossible:
|
|
@@ -857,6 +898,23 @@ export const createSearch = (
|
|
|
857
898
|
};
|
|
858
899
|
};
|
|
859
900
|
|
|
901
|
+
// TLC exits with the code of a standard module's failure only when the failure escapes the
|
|
902
|
+
// computation of next states. In every other phase, the phase decides the code.
|
|
903
|
+
const withNextStateExitCode = (error: unknown) =>
|
|
904
|
+
error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
|
|
905
|
+
|
|
906
|
+
// TLC reports a failure of the next-state relation as an unexpected exception, unless a standard
|
|
907
|
+
// module raised it as an EvalException.
|
|
908
|
+
const toNextStateError = (error: unknown) =>
|
|
909
|
+
error instanceof TlaEvaluationError &&
|
|
910
|
+
!(error instanceof TlaModuleError) &&
|
|
911
|
+
!(error instanceof TlaIncompleteStateError)
|
|
912
|
+
? setExitCode(
|
|
913
|
+
new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
|
|
914
|
+
getErrorExitCode(error),
|
|
915
|
+
)
|
|
916
|
+
: withNextStateExitCode(error);
|
|
917
|
+
|
|
860
918
|
const withHeadline = (error: unknown, headline: string) =>
|
|
861
919
|
error instanceof TlaEvaluationError
|
|
862
920
|
? setExitCode(
|
|
@@ -865,8 +923,15 @@ const withHeadline = (error: unknown, headline: string) =>
|
|
|
865
923
|
)
|
|
866
924
|
: error;
|
|
867
925
|
|
|
926
|
+
// TLC finishes the -dump file whether or not the search finds a violation.
|
|
868
927
|
export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
869
|
-
|
|
928
|
+
const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
|
|
929
|
+
const result = searchStates(options, stateWriter);
|
|
930
|
+
stateWriter?.close();
|
|
931
|
+
return result;
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
|
|
870
935
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
871
936
|
const onPrint = options.onPrint ?? console.log;
|
|
872
937
|
// TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
|
|
@@ -936,7 +1001,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
936
1001
|
});
|
|
937
1002
|
|
|
938
1003
|
const search = createSearch(config, environment, () =>
|
|
939
|
-
|
|
1004
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "variable"),
|
|
940
1005
|
);
|
|
941
1006
|
const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
|
|
942
1007
|
if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
|
|
@@ -962,8 +1027,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
962
1027
|
const getFingerprintState = search.getFingerprintState;
|
|
963
1028
|
const nextActions = { value: undefined as SpecAction[] | undefined };
|
|
964
1029
|
const isReducing = config.view !== undefined || config.symmetry !== undefined;
|
|
1030
|
+
const tlcFingerprints: number[] = [];
|
|
965
1031
|
const liveness = withLivenessExitCode(() =>
|
|
966
|
-
createLivenessChecker(search.properties.liveness, spec.temporals, environment
|
|
1032
|
+
createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
|
|
1033
|
+
getFingerprint: (index) =>
|
|
1034
|
+
(tlcFingerprints[index] ??= Number(
|
|
1035
|
+
BigInt.asIntN(32, runtime.getFingerprint(states[index])),
|
|
1036
|
+
)),
|
|
1037
|
+
getSuccessors: (state) => {
|
|
1038
|
+
const successors: LiveSuccessor[] = [];
|
|
1039
|
+
if (spec.next === undefined) return successors;
|
|
1040
|
+
forEachSuccessor(
|
|
1041
|
+
spec.next,
|
|
1042
|
+
state,
|
|
1043
|
+
environment,
|
|
1044
|
+
(successor) => {
|
|
1045
|
+
canonicalizeState(successor.state);
|
|
1046
|
+
const isInModel =
|
|
1047
|
+
search.satisfiesConstraints(successor.state, state) &&
|
|
1048
|
+
search.satisfiesActionConstraints(state, successor.state);
|
|
1049
|
+
if (!isInModel) return false;
|
|
1050
|
+
const fingerprintState = getFingerprintState(successor.state);
|
|
1051
|
+
const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
|
|
1052
|
+
successors.push({ index, state: successor.state });
|
|
1053
|
+
return false;
|
|
1054
|
+
},
|
|
1055
|
+
spec.scope,
|
|
1056
|
+
);
|
|
1057
|
+
return successors;
|
|
1058
|
+
},
|
|
1059
|
+
}),
|
|
967
1060
|
);
|
|
968
1061
|
const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
|
|
969
1062
|
|
|
@@ -972,7 +1065,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
972
1065
|
const parents: number[] = [];
|
|
973
1066
|
const actions: (string | undefined)[] = [];
|
|
974
1067
|
const depths: number[] = [];
|
|
975
|
-
|
|
1068
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1069
|
+
// and its index plus one side by side, probed by the hash.
|
|
1070
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
976
1071
|
const canonicalizeState = createCanonicalizer();
|
|
977
1072
|
|
|
978
1073
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -985,11 +1080,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
985
1080
|
position: number,
|
|
986
1081
|
loopStart: number | undefined,
|
|
987
1082
|
) => {
|
|
988
|
-
const state = steps[position].state;
|
|
989
|
-
const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
|
|
990
1083
|
// TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
|
|
991
1084
|
runtime.getLevel = () => position + 1;
|
|
992
1085
|
runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
|
|
1086
|
+
return evaluateAlias(
|
|
1087
|
+
alias,
|
|
1088
|
+
steps[position].state,
|
|
1089
|
+
(steps[position + 1] ?? steps[loopStart ?? position]).state,
|
|
1090
|
+
);
|
|
1091
|
+
};
|
|
1092
|
+
const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
|
|
993
1093
|
try {
|
|
994
1094
|
const body = getConfigDefinition("alias", alias, environment).body;
|
|
995
1095
|
const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
|
|
@@ -1104,7 +1204,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1104
1204
|
outDegrees: failureOutDegrees,
|
|
1105
1205
|
};
|
|
1106
1206
|
};
|
|
1107
|
-
const
|
|
1207
|
+
const getPathSteps = (path: number[], loopStart?: number) => {
|
|
1108
1208
|
const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
|
|
1109
1209
|
const last = steps[steps.length - 1].state;
|
|
1110
1210
|
const isClosed =
|
|
@@ -1113,7 +1213,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1113
1213
|
loopStart === path.length - 1 ||
|
|
1114
1214
|
findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
|
|
1115
1215
|
if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
|
|
1116
|
-
return
|
|
1216
|
+
return steps;
|
|
1117
1217
|
};
|
|
1118
1218
|
const getStateHash = (state: Value[]) => {
|
|
1119
1219
|
let hash = 0x811c9dc5;
|
|
@@ -1127,19 +1227,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1127
1227
|
}
|
|
1128
1228
|
return true;
|
|
1129
1229
|
};
|
|
1230
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1130
1231
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1131
|
-
const
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1232
|
+
const mask = seen.slots.length - 1;
|
|
1233
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1234
|
+
const entry = seen.slots[slot + 1];
|
|
1235
|
+
if (entry === 0) return -1;
|
|
1236
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1237
|
+
return entry - 1;
|
|
1238
|
+
}
|
|
1135
1239
|
}
|
|
1136
|
-
|
|
1240
|
+
};
|
|
1241
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1242
|
+
const mask = seen.slots.length - 1;
|
|
1243
|
+
let slot = getSlot(hash);
|
|
1244
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1245
|
+
seen.slots[slot] = hash;
|
|
1246
|
+
seen.slots[slot + 1] = entry;
|
|
1137
1247
|
};
|
|
1138
1248
|
const markSeen = (hash: number, index: number) => {
|
|
1139
|
-
|
|
1140
|
-
if (
|
|
1141
|
-
|
|
1142
|
-
|
|
1249
|
+
seen.size++;
|
|
1250
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1251
|
+
const slots = seen.slots;
|
|
1252
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1253
|
+
seen.shift--;
|
|
1254
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1255
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
insertSeen(hash, index + 1);
|
|
1143
1259
|
};
|
|
1144
1260
|
const countDistinctState = (depth: number) => {
|
|
1145
1261
|
runtime.distinctStates = states.length + 1;
|
|
@@ -1179,7 +1295,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1179
1295
|
};
|
|
1180
1296
|
runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
|
|
1181
1297
|
runtime.setLivenessSource = (index) => {
|
|
1182
|
-
current.livenessSource = index;
|
|
1298
|
+
current.livenessSource = index ?? -1;
|
|
1183
1299
|
};
|
|
1184
1300
|
runtime.getLevel = (state) => {
|
|
1185
1301
|
const source = current.livenessSource;
|
|
@@ -1190,6 +1306,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1190
1306
|
return current.isExpanding ? depths[current.index] + 1 : 0;
|
|
1191
1307
|
};
|
|
1192
1308
|
|
|
1309
|
+
const getGraphState = (state: Value[], level: number): GraphState => ({
|
|
1310
|
+
fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
|
|
1311
|
+
level,
|
|
1312
|
+
getLabel: () =>
|
|
1313
|
+
formatState(
|
|
1314
|
+
config.alias === undefined
|
|
1315
|
+
? toRecord(state)
|
|
1316
|
+
: evaluateAlias(config.alias, state, undefined),
|
|
1317
|
+
),
|
|
1318
|
+
getText: () => formatState(toRecord(state)),
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1193
1321
|
const visitInitialState = (state: Value[]) => {
|
|
1194
1322
|
canonicalizeState(state);
|
|
1195
1323
|
runtime.generatedStates++;
|
|
@@ -1206,6 +1334,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1206
1334
|
countDistinctState(1);
|
|
1207
1335
|
runtime.queueSize++;
|
|
1208
1336
|
liveness?.addInitialState(states.length, state);
|
|
1337
|
+
stateWriter?.writeInitialState(getGraphState(state, 1));
|
|
1209
1338
|
}
|
|
1210
1339
|
const violated = search.findViolatedInvariant(state);
|
|
1211
1340
|
if (violated) {
|
|
@@ -1226,7 +1355,13 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1226
1355
|
addState(state, fingerprintState, -1, undefined);
|
|
1227
1356
|
return undefined;
|
|
1228
1357
|
};
|
|
1229
|
-
const visitSuccessor = (
|
|
1358
|
+
const visitSuccessor = (
|
|
1359
|
+
index: number,
|
|
1360
|
+
successor: Successor,
|
|
1361
|
+
liveSuccessors: LiveSuccessor[],
|
|
1362
|
+
source: GraphState | undefined,
|
|
1363
|
+
action: SpecAction | undefined,
|
|
1364
|
+
) => {
|
|
1230
1365
|
const state = states[index];
|
|
1231
1366
|
runtime.generatedStates++;
|
|
1232
1367
|
current.candidate = successor.state;
|
|
@@ -1240,6 +1375,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1240
1375
|
const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
|
|
1241
1376
|
if (isInModel) search.trackActionPossible(state, successor.state);
|
|
1242
1377
|
const existing = isInModel ? found : -1;
|
|
1378
|
+
if (source && isInModel) {
|
|
1379
|
+
stateWriter?.writeSuccessor(
|
|
1380
|
+
source,
|
|
1381
|
+
getGraphState(successor.state, depths[index] + 1),
|
|
1382
|
+
existing !== -1,
|
|
1383
|
+
action && { name: action.name, signature: getActionSignature(action) },
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1243
1386
|
if (existing !== -1 && liveness) {
|
|
1244
1387
|
const stored = states[existing];
|
|
1245
1388
|
const isStored = stored.every((value, position) => value === successor.state[position]);
|
|
@@ -1311,23 +1454,55 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1311
1454
|
}
|
|
1312
1455
|
return createSuccess();
|
|
1313
1456
|
}
|
|
1457
|
+
// TLC labels and colors an edge of the -dump state graph by the action that generated it. When
|
|
1458
|
+
// the graph names its edges, the search generates successors action by action, as TLC does.
|
|
1459
|
+
const isNamingEdges = Boolean(
|
|
1460
|
+
options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
|
|
1461
|
+
);
|
|
1462
|
+
const forEachActionSuccessor = (
|
|
1463
|
+
state: Value[],
|
|
1464
|
+
onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
|
|
1465
|
+
) => {
|
|
1466
|
+
if (!isNamingEdges) {
|
|
1467
|
+
return forEachSuccessor(
|
|
1468
|
+
next,
|
|
1469
|
+
state,
|
|
1470
|
+
environment,
|
|
1471
|
+
(successor) => onSuccessor(successor, undefined),
|
|
1472
|
+
spec.scope,
|
|
1473
|
+
);
|
|
1474
|
+
}
|
|
1475
|
+
nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
|
|
1476
|
+
const progress = { isStopped: false };
|
|
1477
|
+
return nextActions.value.reduce((count, action) => {
|
|
1478
|
+
if (progress.isStopped) return count;
|
|
1479
|
+
return (
|
|
1480
|
+
count +
|
|
1481
|
+
forEachSuccessor(
|
|
1482
|
+
action.expression,
|
|
1483
|
+
state,
|
|
1484
|
+
environment,
|
|
1485
|
+
(successor) => {
|
|
1486
|
+
progress.isStopped = onSuccessor(successor, action);
|
|
1487
|
+
return progress.isStopped;
|
|
1488
|
+
},
|
|
1489
|
+
action.scope,
|
|
1490
|
+
)
|
|
1491
|
+
);
|
|
1492
|
+
}, 0);
|
|
1493
|
+
};
|
|
1314
1494
|
const expandState = (index: number) => {
|
|
1315
1495
|
const state = states[index];
|
|
1316
1496
|
const liveSuccessors: LiveSuccessor[] = [];
|
|
1317
|
-
const
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
(
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
}
|
|
1327
|
-
return outcome.result !== undefined;
|
|
1328
|
-
},
|
|
1329
|
-
spec.scope,
|
|
1330
|
-
);
|
|
1497
|
+
const source = stateWriter && getGraphState(state, depths[index]);
|
|
1498
|
+
const successorCount = forEachActionSuccessor(state, (successor, action) => {
|
|
1499
|
+
try {
|
|
1500
|
+
outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
|
|
1501
|
+
} catch (error) {
|
|
1502
|
+
outcome.result = toFailure(error, getTrace(index, successor));
|
|
1503
|
+
}
|
|
1504
|
+
return outcome.result !== undefined;
|
|
1505
|
+
});
|
|
1331
1506
|
current.isExpanding = false;
|
|
1332
1507
|
if (outcome.result) return outcome.result;
|
|
1333
1508
|
if (successorCount === 0 && config.checkDeadlock) {
|
|
@@ -1335,32 +1510,43 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1335
1510
|
}
|
|
1336
1511
|
try {
|
|
1337
1512
|
const accepted = liveness?.addState(index, state, liveSuccessors);
|
|
1338
|
-
|
|
1339
|
-
|
|
1513
|
+
if (!liveness || !accepted) return undefined;
|
|
1514
|
+
const steps = getPathSteps(accepted);
|
|
1515
|
+
const properties = liveness.getViolatedProperties(
|
|
1516
|
+
steps.map((step) => step.state),
|
|
1517
|
+
steps.length - 1,
|
|
1518
|
+
);
|
|
1340
1519
|
return reportViolation("property-violation", {
|
|
1341
|
-
name:
|
|
1342
|
-
|
|
1520
|
+
name: properties[0],
|
|
1521
|
+
properties,
|
|
1522
|
+
trace: toTrace(steps, accepted),
|
|
1343
1523
|
});
|
|
1344
1524
|
} catch (error) {
|
|
1345
1525
|
return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
|
|
1346
1526
|
}
|
|
1347
1527
|
};
|
|
1348
|
-
|
|
1528
|
+
// TLC names the properties and the step back into the cycle from the states it prints.
|
|
1529
|
+
const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
|
|
1349
1530
|
const path = [...lasso.prefix, ...lasso.cycle];
|
|
1350
|
-
const
|
|
1351
|
-
const
|
|
1531
|
+
const loopStart = lasso.prefix.length;
|
|
1532
|
+
const steps = getPathSteps(path, loopStart);
|
|
1533
|
+
const trace = toTrace(steps, path, loopStart);
|
|
1534
|
+
const properties = checker.getViolatedProperties(
|
|
1535
|
+
steps.map((step) => step.state),
|
|
1536
|
+
loopStart,
|
|
1537
|
+
);
|
|
1538
|
+
const back = path[loopStart];
|
|
1352
1539
|
const last = path[path.length - 1];
|
|
1540
|
+
const lastState = steps[steps.length - 1].state;
|
|
1353
1541
|
const loopLabel =
|
|
1354
|
-
back === last ? undefined : withoutOutput(() => getLabel(last,
|
|
1542
|
+
back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
|
|
1355
1543
|
return reportViolation(
|
|
1356
1544
|
"liveness-violation",
|
|
1357
|
-
{ name:
|
|
1545
|
+
{ name: properties[0], properties, trace, loopStart, loopLabel },
|
|
1358
1546
|
() =>
|
|
1359
1547
|
back === last
|
|
1360
1548
|
? trace[trace.length - 1].action
|
|
1361
|
-
:
|
|
1362
|
-
isSameState(successor.state, states[back]),
|
|
1363
|
-
)?.action,
|
|
1549
|
+
: findSuccessorWithFingerprint(lastState, back)?.action,
|
|
1364
1550
|
);
|
|
1365
1551
|
};
|
|
1366
1552
|
const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
|
|
@@ -1373,11 +1559,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1373
1559
|
if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
|
|
1374
1560
|
periodic.size = expanded;
|
|
1375
1561
|
const lasso = liveness?.findViolation();
|
|
1376
|
-
current.livenessSource = -1;
|
|
1377
1562
|
periodic.time += performance.now() - now;
|
|
1378
1563
|
return lasso;
|
|
1379
1564
|
};
|
|
1380
|
-
setNextStatePhase(true);
|
|
1381
1565
|
for (let index = 0; index < states.length && !runtime.isStopped; index++) {
|
|
1382
1566
|
current.index = index;
|
|
1383
1567
|
current.candidate = undefined;
|
|
@@ -1386,22 +1570,21 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1386
1570
|
try {
|
|
1387
1571
|
outcome.result = expandState(index);
|
|
1388
1572
|
} catch (error) {
|
|
1389
|
-
outcome.result = toFailure(error, getStateTrace(index));
|
|
1573
|
+
outcome.result = toFailure(toNextStateError(error), getStateTrace(index));
|
|
1390
1574
|
}
|
|
1391
1575
|
if (outcome.result) return outcome.result;
|
|
1392
1576
|
outDegrees[getOutDegreeBucket()]++;
|
|
1393
1577
|
expansion.newStates = 0;
|
|
1394
1578
|
if (isPeriodic && performance.now() >= periodic.at) {
|
|
1395
1579
|
const lasso = findPeriodicViolation(index + 1);
|
|
1396
|
-
if (lasso) return reportLasso(lasso);
|
|
1580
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1397
1581
|
}
|
|
1398
1582
|
}
|
|
1399
1583
|
|
|
1400
1584
|
current.index = -1;
|
|
1401
1585
|
current.candidate = undefined;
|
|
1402
1586
|
const lasso = liveness?.findViolation();
|
|
1403
|
-
|
|
1404
|
-
if (lasso) return reportLasso(lasso);
|
|
1587
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1405
1588
|
|
|
1406
1589
|
const postconditionViolation = search.findPostconditionViolation();
|
|
1407
1590
|
if (postconditionViolation) {
|
package/src/cli-options.ts
CHANGED
|
@@ -2,6 +2,8 @@ export interface OptionSpec {
|
|
|
2
2
|
flag: string;
|
|
3
3
|
isRequired?: boolean;
|
|
4
4
|
isRepeatable?: boolean;
|
|
5
|
+
// How many of the arguments after the flag the option takes, when that depends on them.
|
|
6
|
+
getArity?: (rest: string[]) => number;
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
export interface ParsedOption {
|
|
@@ -29,27 +31,31 @@ export const defineCommand = (usage: string, specs: OptionSpec[]) => {
|
|
|
29
31
|
const arities = new Map(
|
|
30
32
|
specs.map((spec) => {
|
|
31
33
|
const [name, ...values] = spec.flag.split(" ");
|
|
32
|
-
return [name, values.length];
|
|
34
|
+
return [name, spec.getArity ?? (() => values.length)];
|
|
33
35
|
}),
|
|
34
36
|
);
|
|
35
37
|
const parse = (args: string[]): ParsedArgs => {
|
|
36
38
|
const options: ParsedOption[] = [];
|
|
37
39
|
const positionals: string[] = [];
|
|
38
40
|
const unknown: string[] = [];
|
|
41
|
+
const incomplete: ParsedOption[] = [];
|
|
39
42
|
for (let index = 0; index < args.length; index++) {
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
43
|
+
const getArity = arities.get(args[index]);
|
|
44
|
+
if (getArity === undefined) {
|
|
42
45
|
(args[index].startsWith("-") ? unknown : positionals).push(args[index]);
|
|
43
46
|
continue;
|
|
44
47
|
}
|
|
45
|
-
|
|
48
|
+
const arity = getArity(args.slice(index + 1));
|
|
49
|
+
const option = { name: args[index], values: args.slice(index + 1, index + 1 + arity) };
|
|
50
|
+
options.push(option);
|
|
51
|
+
if (option.values.length < arity) incomplete.push(option);
|
|
46
52
|
index += arity;
|
|
47
53
|
}
|
|
48
54
|
return {
|
|
49
55
|
options,
|
|
50
56
|
positionals,
|
|
51
57
|
unknown,
|
|
52
|
-
incomplete:
|
|
58
|
+
incomplete: incomplete[0],
|
|
53
59
|
has: (name) => options.some((option) => option.name === name),
|
|
54
60
|
get: (name) => options.findLast((option) => option.name === name)?.values,
|
|
55
61
|
};
|