1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package eval_test
// Parsing files in the background interns identifiers in a nondeterministic
// order, so Symbol IDs — and therefore the physical layout of every attrset,
// which is sorted by ID — vary between runs. Everything about background
// parsing rests on that being unobservable: every consumer is supposed to
// re-sort by the symbol's *string* before anything user-visible depends on
// the order.
//
// These tests check that claim by breaking the order on purpose and
// confirming the results do not move. Without them a future change could
// reintroduce an ID-order dependency and it would show up only as a
// mysteriously different derivation hash on a multi-core machine.
import (
"math/rand"
"testing"
"github.com/nixos/lix/gonix/eval"
)
// scrambled builds an EvalState whose symbol IDs have been permuted, by
// interning a shuffled pile of identifiers before evaluating anything.
//
// The identifiers are interned *after* registerBuiltins (which NewEvalState
// runs) because the builtins' own IDs are load-bearing: they must match the
// order the parser assumes for ExprVar.Displ, and shifting them corrupts
// variable resolution outright.
func scrambled(t *testing.T, seed int64) *eval.EvalState {
t.Helper()
s := eval.NewEvalState(eval.DefaultBuiltinNames)
words := make([]string, 0, 512)
for _, w := range []string{
"lib", "base", "description", "mkDerivation", "callPackage", "cfg",
"version", "pname", "license", "buildInputs", "meta", "src", "stdenv",
"name", "system", "config", "options", "type", "default", "value",
"a", "b", "c", "x", "y", "z", "foo", "bar", "baz", "qux",
} {
words = append(words, w)
}
rng := rand.New(rand.NewSource(seed))
rng.Shuffle(len(words), func(i, j int) { words[i], words[j] = words[j], words[i] })
for _, w := range words {
s.Symbols.Create(w)
}
return s
}
func evalWith(t *testing.T, s *eval.EvalState, src string) string {
t.Helper()
expr, err := eval.NativeParseExprIntoState(s, []byte(src), "/", "<test>")
if err != nil {
t.Fatalf("parse: %v", err)
}
v, err := s.Eval(expr)
if err != nil {
t.Fatalf("eval: %v", err)
}
if err := s.ForceValue(&v); err != nil {
t.Fatalf("force: %v", err)
}
out, err := eval.PrintValue(s, v)
if err != nil {
t.Fatalf("print: %v", err)
}
return out
}
// TestSymbolOrderUnobservable pins the invariant: the same expression must
// produce the same output no matter what order its identifiers were interned
// in. Each case is a place where attribute *order* could leak out.
func TestSymbolOrderUnobservable(t *testing.T) {
cases := map[string]string{
"attrNames": `builtins.attrNames { z = 1; a = 2; m = 3; foo = 4; bar = 5; }`,
"attrValues": `builtins.attrValues { z = 1; a = 2; m = 3; foo = 4; bar = 5; }`,
"toJSON": `builtins.toJSON { z = 1; a = 2; nested = { y = 1; b = 2; }; }`,
"toXML": `builtins.toXML { b = 1; a = 2; }`,
"printed set": `{ zebra = 1; apple = 2; mango = 3; }`,
"nested sets": `{ z.y.x = 1; a.b.c = 2; }`,
"mapAttrs": `builtins.mapAttrs (n: v: n) { z = 1; a = 2; foo = 3; }`,
"listToAttrs": `builtins.listToAttrs [ { name = "z"; value = 1; } { name = "a"; value = 2; } ]`,
"intersectAttrs": `builtins.intersectAttrs { z = 1; a = 2; } { z = 3; a = 4; b = 5; }`,
"removeAttrs": `builtins.removeAttrs { z = 1; a = 2; m = 3; } [ "m" ]`,
}
for name, src := range cases {
t.Run(name, func(t *testing.T) {
want := evalWith(t, eval.NewEvalState(eval.DefaultBuiltinNames), src)
for _, seed := range []int64{1, 2, 7, 42} {
got := evalWith(t, scrambled(t, seed), src)
if got != want {
t.Fatalf("symbol order is observable\n seed %d\n got: %s\n want: %s",
seed, got, want)
}
}
})
}
}
// TestSymbolScrambleActuallyPermutes guards the guard: if the helper above
// ever stopped changing the relative order of symbols, every test using it
// would still pass while proving nothing.
func TestSymbolScrambleActuallyPermutes(t *testing.T) {
probe := []string{"lib", "base", "mkDerivation", "cfg", "version", "meta", "src"}
ids := func(s *eval.EvalState) []eval.Symbol {
out := make([]eval.Symbol, len(probe))
for i, w := range probe {
out[i] = s.Symbols.Create(w)
}
return out
}
// Pairwise orderings that survive: with a real permutation this should be
// near half, not all.
rel := func(x []eval.Symbol) []bool {
var r []bool
for i := range x {
for j := i + 1; j < len(x); j++ {
r = append(r, x[i] < x[j])
}
}
return r
}
plain := rel(ids(eval.NewEvalState(eval.DefaultBuiltinNames)))
same := 0
got := rel(ids(scrambled(t, 1)))
for i := range plain {
if plain[i] == got[i] {
same++
}
}
if same == len(plain) {
t.Fatalf("scramble is a no-op: all %d pairwise orderings preserved", len(plain))
}
t.Logf("pairwise orderings preserved: %d/%d (a real permutation)", same, len(plain))
}
|