Skip to content

Commit 5077511

Browse files
committed
internal/core/adt: add generation tracking to closedness info
This change adds generation tracking to CloseInfo to prevent stale closedness information from being used across different evaluation contexts. Each CloseInfo now tracks the generation (opID) when it was created, allowing the evaluator to detect and handle misaligned conjuncts and constraints. Key changes: - Add opID field to CloseInfo struct for generation tracking - Add MisalignedConjunct and MisalignedConstraint counters to stats - Validate generation alignment when processing conjuncts/constraints - Drop misaligned items and increment appropriate counters - Reset opID when converting vertices in toDataAllRec This prevents cross-generation contamination that could lead to incorrect evaluation results or memory safety issues. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: If29fcdff2221b8295c2cb34427c34e9b3a1f4b53 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218835 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 19e2c74 commit 5077511

File tree

80 files changed

+252
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+252
-28
lines changed

cmd/cue/cmd/testdata/script/stats.txtar

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ contents overwritten
5555
"MaxConjunctInfos": 2,
5656
"MaxReqSets": 0,
5757
"GenerationMismatch": 0,
58+
"MisalignedConjunct": 0,
59+
"MisalignedConstraint": 0,
5860
"Freed": 6,
5961
"Reused": 0,
6062
"Allocs": 6,
@@ -67,20 +69,22 @@ contents overwritten
6769
}
6870
-- out/stats.cue --
6971
CUE: {
70-
EvalVersion: 3
71-
Unifications: 4
72-
Disjuncts: 2
73-
Notifications: 0
74-
Conjuncts: 8
75-
NumCloseIDs: 2
76-
ConjunctInfos: 6
77-
MaxConjunctInfos: 2
78-
MaxReqSets: 0
79-
GenerationMismatch: 0
80-
Freed: 6
81-
Reused: 0
82-
Allocs: 6
83-
Retained: 0
72+
EvalVersion: 3
73+
Unifications: 4
74+
Disjuncts: 2
75+
Notifications: 0
76+
Conjuncts: 8
77+
NumCloseIDs: 2
78+
ConjunctInfos: 6
79+
MaxConjunctInfos: 2
80+
MaxReqSets: 0
81+
GenerationMismatch: 0
82+
MisalignedConjunct: 0
83+
MisalignedConstraint: 0
84+
Freed: 6
85+
Reused: 0
86+
Allocs: 6
87+
Retained: 0
8488
}
8589
Go: {
8690
AllocBytes: 300456
@@ -98,6 +102,8 @@ CUE:
98102
MaxConjunctInfos: 2
99103
MaxReqSets: 0
100104
GenerationMismatch: 0
105+
MisalignedConjunct: 0
106+
MisalignedConstraint: 0
101107
Freed: 6
102108
Reused: 0
103109
Allocs: 6
@@ -118,6 +124,8 @@ Go:
118124
"MaxConjunctInfos": 2,
119125
"MaxReqSets": 0,
120126
"GenerationMismatch": 0,
127+
"MisalignedConjunct": 0,
128+
"MisalignedConstraint": 0,
121129
"Freed": 6,
122130
"Reused": 0,
123131
"Allocs": 6,

cue/stats/stats.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ type Counts struct {
8383
// with a different generation than the one it was created in.
8484
GenerationMismatch int64 // Number of exceptional unification cases
8585

86+
// MisalignedConjunct indicates the number of conjuncts that were dropped
87+
// because they were not aligned with the current generation of the context.
88+
// Generally this happens because a previously finalized vertex is unified
89+
// in as a value, not constraint, in which case it is okay to ignore
90+
// closedness info. If it were included as a schema, top-level conjuncts
91+
// would be unified and mapped to a local tree.
92+
MisalignedConjunct int64
93+
94+
// MisalignedConstraint indicates the number of constraints that were not
95+
// aligned. This is more likely to be a bug.
96+
MisalignedConstraint int64
97+
8698
// Buffer counters
8799
//
88100
// Each unification and disjunct operation is associated with an object
@@ -122,6 +134,8 @@ func (c *Counts) Add(other Counts) {
122134
c.Notifications += other.Notifications
123135

124136
c.GenerationMismatch += other.GenerationMismatch
137+
c.MisalignedConjunct += other.MisalignedConjunct
138+
c.MisalignedConstraint += other.MisalignedConstraint
125139

126140
c.NumCloseIDs += other.NumCloseIDs
127141
c.ConjunctInfos += other.ConjunctInfos
@@ -144,6 +158,8 @@ func (c Counts) Since(start Counts) Counts {
144158
c.Disjuncts -= start.Disjuncts
145159
c.Notifications -= start.Notifications
146160
c.GenerationMismatch -= start.GenerationMismatch
161+
c.MisalignedConjunct -= start.MisalignedConjunct
162+
c.MisalignedConstraint -= start.MisalignedConstraint
147163
c.NumCloseIDs -= start.NumCloseIDs
148164

149165
c.ConjunctInfos -= start.ConjunctInfos
@@ -177,9 +193,11 @@ Retain: {{.Retained}}
177193
Unifications: {{.Unifications}}
178194
Conjuncts: {{.Conjuncts}}
179195
Disjuncts: {{.Disjuncts}}{{if .Notifications}}
180-
Notifications: {{.Notifications}}{{end}}{{if .GenerationMismatch}}
181-
182-
GenerationMismatch: {{.GenerationMismatch}}{{end}}{{if .NumCloseIDs}}
196+
Notifications: {{.Notifications}}{{end}}{{if or .GenerationMismatch .MisalignedConjunct .MisalignedConstraint}}
197+
{{if .GenerationMismatch}}
198+
GenerationMismatch: {{.GenerationMismatch}}{{end}}{{if .MisalignedConjunct}}
199+
MisalignedConjunct: {{.MisalignedConjunct}}{{end}}{{if .MisalignedConstraint}}
200+
MisalignedConstraint: {{.MisalignedConstraint}}{{end}}{{end}}{{if .NumCloseIDs}}
183201
184202
NumCloseIDs: {{.NumCloseIDs}}{{end}}{{if or (ge .MaxReqSets 150) (ge .MaxConjunctInfos 8)}}
185203

cue/testdata/benchmarks/issue2176.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Unifications: 766
115115
Conjuncts: 3242
116116
Disjuncts: 1409
117117

118+
MisalignedConjunct: 1216
119+
118120
NumCloseIDs: 12
119121
-- out/evalalpha --
120122
(struct){

cue/testdata/benchmarks/issue572.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Unifications: 3001
5353
Conjuncts: 7498
5454
Disjuncts: 3001
5555

56+
MisalignedConjunct: 1498
57+
5658
NumCloseIDs: 1
5759
-- out/eval --
5860
(struct){

cue/testdata/benchmarks/mergeddisjunction.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Unifications: 99
5151
Conjuncts: 530
5252
Disjuncts: 283
5353

54+
MisalignedConjunct: 33
55+
5456
NumCloseIDs: 2
5557
-- out/eval --
5658
(struct){

cue/testdata/benchmarks/typocheck.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Unifications: 700
7373
Conjuncts: 1302
7474
Disjuncts: 807
7575

76+
MisalignedConjunct: 696
77+
7678
NumCloseIDs: 4
7779
-- out/eval --
7880
(struct){

cue/testdata/builtins/validators.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ Unifications: 195
231231
Conjuncts: 351
232232
Disjuncts: 218
233233

234+
MisalignedConjunct: 1
235+
234236
NumCloseIDs: 8
235237
-- out/evalalpha --
236238
Errors:

cue/testdata/comprehensions/045_comprehension_and_skipped_field.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Unifications: 8
5858
Conjuncts: 11
5959
Disjuncts: 12
6060

61+
MisalignedConjunct: 1
62+
6163
NumCloseIDs: 1
6264
-- out/eval --
6365
(struct){

cue/testdata/comprehensions/checkdefined.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Unifications: 100
9191
Conjuncts: 134
9292
Disjuncts: 105
9393

94+
MisalignedConjunct: 2
95+
9496
NumCloseIDs: 13
9597
-- out/eval --
9698
(struct){

cue/testdata/comprehensions/closed.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ Unifications: 68
121121
Conjuncts: 118
122122
Disjuncts: 81
123123

124+
MisalignedConjunct: 15
125+
124126
NumCloseIDs: 14
125127
-- out/evalalpha --
126128
Errors:

0 commit comments

Comments
 (0)