1 package lib
2 3 // Regression: a method whose only mutation is a map update was classified as
4 // READ-ONLY, so it pushed its own frame arena instead of borrowing the
5 // receiver's. Everything it stored in the receiver - the value header, and any
6 // backing a push or a concatenation allocated for it - lived in a frame that
7 // was released at return, and the receiver was left pointing at recycled
8 // memory.
9 //
10 // Three parts are needed to expose it, and all three are here:
11 //
12 // - the type must be sovereign, which needs at least one method that stores
13 // a field (Reset and Mark below); otherwise the map-only method gets a
14 // plain frame arena either way and the classification does not matter;
15 // - the method under test (Grow, AddExt) must write ONLY through a map: one
16 // field store anywhere in it would have made the detector fire;
17 // - the caller (Load) must have a frame arena of its own, which it gets by
18 // returning a value that needs a codec (an error).
19 //
20 // mime.TypeByExtension was the live case: the store was created in init, the
21 // mime.types loader parsed lines into its own frame and called a map-only
22 // method to record them, and every extension answered with garbage. The keys
23 // and values here are literals on purpose - a frame-local argument would
24 // dangle for a second, independent reason (the store must take ownership of
25 // data it is handed, which mime does with ownBytes).
26 type Store struct {
27 m map[string][]byte
28 ext map[string][][]byte
29 n int32
30 }
31 32 var st = newStore()
33 34 func newStore() (s *Store) {
35 s = &Store{}
36 s.m = map[string][]byte{}
37 s.ext = map[string][][]byte{}
38 return s
39 }
40 41 func (s *Store) Reset() {
42 s.n = 0
43 s.m = map[string][]byte{}
44 s.ext = map[string][][]byte{}
45 }
46 47 func (s *Store) Mark() {
48 s.n++
49 }
50 51 // Grow appends to a stored value. It writes only through the map: the new
52 // bytes come from a concatenation allocated inside the method.
53 func (s *Store) Grow(k, v []byte) {
54 s.m[string(k)] = s.m[string(k)] | v
55 }
56 57 // AddExt appends to a stored list. It writes only through the map: the new
58 // backing comes from push.
59 func (s *Store) AddExt(typ, ext []byte) {
60 cur := s.ext[string(typ)]
61 s.ext[string(typ)] = push(cur, ext)
62 }
63 64 func Value(k []byte) []byte { return st.m[string(k)] }
65 func Exts(typ []byte) [][]byte { return st.ext[string(typ)] }
66 func Count(k []byte) int32 { return int32(len(st.m[string(k)])) }
67 func Marks() int32 { return st.n }
68 69 // Load returns an error so this frame owns an arena, then builds everything it
70 // keeps from literals the store can hold without taking ownership.
71 func Load(rounds int32) (rerr error) {
72 st.Reset()
73 for i := int32(0); i < rounds; i++ {
74 st.Mark()
75 st.Grow([]byte("acc"), []byte("ab"))
76 st.AddExt([]byte("image/jpeg"), []byte(".jpg"))
77 st.AddExt([]byte("text/html"), []byte(".html"))
78 }
79 return nil
80 }
81