| 1 |
// Package sparse provides types and functions for implementing sparse |
| 2 |
// data-flow analyses. |
| 3 |
package sparse |
| 4 |
|
| 5 |
import ( |
| 6 |
"cmp" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"math/bits" |
| 10 |
"slices" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"honnef.co/go/tools/analysis/dfa" |
| 14 |
"honnef.co/go/tools/go/ir" |
| 15 |
|
| 16 |
"golang.org/x/exp/constraints" |
| 17 |
) |
| 18 |
|
| 19 |
const debugging = false |
| 20 |
|
| 21 |
func debugf(f string, args ...any) { |
| 22 |
if debugging { |
| 23 |
log.Printf(f, args...) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// Mapping maps a single [ir.Value] to an abstract state. |
| 28 |
type Mapping[Elem any] struct { |
| 29 |
Value ir.Value |
| 30 |
State Elem |
| 31 |
Decision Decision |
| 32 |
} |
| 33 |
|
| 34 |
// Decision describes how a mapping from an [ir.Value] to an abstract state |
| 35 |
// came to be. Decisions are provided by transfer functions when they create |
| 36 |
// mappings. |
| 37 |
type Decision struct { |
| 38 |
// The relevant values that the transfer function used to make the |
| 39 |
// decision. |
| 40 |
Inputs []ir.Value |
| 41 |
// A human-readable description of the decision. |
| 42 |
Description string |
| 43 |
// Whether this is the source of an abstract state. For example, in a taint |
| 44 |
// analysis, the call to a function that produces a tainted value would be |
| 45 |
// the source of the taint state, and any instructions that operate on and |
| 46 |
// propagate tainted values would not be sources. |
| 47 |
Source bool |
| 48 |
} |
| 49 |
|
| 50 |
func (m Mapping[Elem]) String() string { |
| 51 |
return fmt.Sprintf("%s = %v", m.Value.Name(), m.State) |
| 52 |
} |
| 53 |
|
| 54 |
// M is a helper for constructing instances of [Mapping]. |
| 55 |
func M[Elem any](v ir.Value, s Elem, d Decision) Mapping[Elem] { |
| 56 |
return Mapping[Elem]{Value: v, State: s, Decision: d} |
| 57 |
} |
| 58 |
|
| 59 |
// Ms is a helper for constructing slices of mappings. |
| 60 |
// |
| 61 |
// Example: |
| 62 |
// |
| 63 |
// Ms(M(v1, d1, ...), M(v2, d2, ...)) |
| 64 |
func Ms[Elem comparable](ms ...Mapping[Elem]) []Mapping[Elem] { |
| 65 |
return ms |
| 66 |
} |
| 67 |
|
| 68 |
func Forward[L dfa.Semilattice[Elem], Elem any]( |
| 69 |
fn *ir.Function, |
| 70 |
transfer func(*Instance[L, Elem], ir.Instruction) []Mapping[Elem], |
| 71 |
) *Instance[L, Elem] { |
| 72 |
ins := &Instance[L, Elem]{ |
| 73 |
Transfer: transfer, |
| 74 |
Mapping: map[ir.Value]Mapping[Elem]{}, |
| 75 |
} |
| 76 |
ins.Forward(fn) |
| 77 |
return ins |
| 78 |
} |
| 79 |
|
| 80 |
// Instance is an instance of a data-flow analysis. It is created by |
| 81 |
// [Framework.Forward]. |
| 82 |
type Instance[L dfa.Semilattice[Elem], Elem any] struct { |
| 83 |
l L |
| 84 |
|
| 85 |
Transfer func(*Instance[L, Elem], ir.Instruction) []Mapping[Elem] |
| 86 |
// Mapping is the result of the analysis. Consider using Instance.Value |
| 87 |
// instead of accessing Mapping directly, as it correctly returns ⊥ for |
| 88 |
// missing values. |
| 89 |
Mapping map[ir.Value]Mapping[Elem] |
| 90 |
} |
| 91 |
|
| 92 |
// Set maps v to the abstract value d. It does not apply any checks. This |
| 93 |
// should only be used before calling [Instance.Forward], to set initial states |
| 94 |
// of values. |
| 95 |
func (ins *Instance[L, Elem]) Set(v ir.Value, d Elem) { |
| 96 |
ins.Mapping[v] = Mapping[Elem]{Value: v, State: d} |
| 97 |
} |
| 98 |
|
| 99 |
// Value returns the abstract value for v. If none was set, it returns ⊥. |
| 100 |
func (ins *Instance[L, Elem]) Value(v ir.Value) Elem { |
| 101 |
m, ok := ins.Mapping[v] |
| 102 |
if ok { |
| 103 |
return m.State |
| 104 |
} else { |
| 105 |
return ins.l.Ident() |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Decision returns the decision of the mapping for v, if any. |
| 110 |
func (ins *Instance[L, Elem]) Decision(v ir.Value) Decision { |
| 111 |
return ins.Mapping[v].Decision |
| 112 |
} |
| 113 |
|
| 114 |
var dfsDebugMu sync.Mutex |
| 115 |
|
| 116 |
// Forward runs a forward data-flow analysis on fn. |
| 117 |
func (ins *Instance[L, Elem]) Forward(fn *ir.Function) { |
| 118 |
if debugging { |
| 119 |
dfsDebugMu.Lock() |
| 120 |
defer dfsDebugMu.Unlock() |
| 121 |
} |
| 122 |
|
| 123 |
debugf("Analyzing %s\n", fn) |
| 124 |
if ins.Mapping == nil { |
| 125 |
ins.Mapping = map[ir.Value]Mapping[Elem]{} |
| 126 |
} |
| 127 |
|
| 128 |
worklist := map[ir.Instruction]struct{}{} |
| 129 |
for _, b := range fn.Blocks { |
| 130 |
for _, instr := range b.Instrs { |
| 131 |
worklist[instr] = struct{}{} |
| 132 |
} |
| 133 |
} |
| 134 |
for len(worklist) > 0 { |
| 135 |
var instr ir.Instruction |
| 136 |
for instr = range worklist { |
| 137 |
break |
| 138 |
} |
| 139 |
delete(worklist, instr) |
| 140 |
|
| 141 |
var ds []Mapping[Elem] |
| 142 |
if phi, ok := instr.(*ir.Phi); ok { |
| 143 |
d := ins.l.Ident() |
| 144 |
for _, edge := range phi.Edges { |
| 145 |
a, b := d, ins.Value(edge) |
| 146 |
d = ins.l.Merge(a, b) |
| 147 |
debugf("join(%v, %v) = %v", a, b, d) |
| 148 |
} |
| 149 |
ds = []Mapping[Elem]{ |
| 150 |
{ |
| 151 |
Value: phi, |
| 152 |
State: d, |
| 153 |
Decision: Decision{ |
| 154 |
Inputs: phi.Edges, |
| 155 |
Description: "this variable merges the results of multiple branches", |
| 156 |
}, |
| 157 |
}, |
| 158 |
} |
| 159 |
} else { |
| 160 |
ds = ins.Transfer(ins, instr) |
| 161 |
} |
| 162 |
if len(ds) > 0 { |
| 163 |
if v, ok := instr.(ir.Value); ok { |
| 164 |
debugf("transfer(%s = %s) = %v", v.Name(), instr, ds) |
| 165 |
} else { |
| 166 |
debugf("transfer(%s) = %v", instr, ds) |
| 167 |
} |
| 168 |
} |
| 169 |
for _, d := range ds { |
| 170 |
old := ins.Value(d.Value) |
| 171 |
dd := d.State |
| 172 |
if !ins.l.Equals(dd, old) { |
| 173 |
ins.Mapping[d.Value] = Mapping[Elem]{ |
| 174 |
Value: d.Value, |
| 175 |
State: dd, |
| 176 |
Decision: d.Decision, |
| 177 |
} |
| 178 |
|
| 179 |
for _, ref := range *instr.Referrers() { |
| 180 |
worklist[ref] = struct{}{} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
printMapping(fn, ins.Mapping) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// Propagate is a helper for creating a [Mapping] that propagates the abstract |
| 189 |
// state of src to dst. The desc parameter is used as the value of |
| 190 |
// Decision.Description. |
| 191 |
func (ins *Instance[L, Elem]) Propagate(dst, src ir.Value, desc string) Mapping[Elem] { |
| 192 |
return M(dst, ins.Value(src), Decision{Inputs: []ir.Value{src}, Description: desc}) |
| 193 |
} |
| 194 |
|
| 195 |
func (ins *Instance[L, Elem]) Transform(dst ir.Value, s Elem, src ir.Value, desc string) Mapping[Elem] { |
| 196 |
return M(dst, s, Decision{Inputs: []ir.Value{src}, Description: desc}) |
| 197 |
} |
| 198 |
|
| 199 |
func printMapping[Elem any](fn *ir.Function, m map[ir.Value]Elem) { |
| 200 |
if !debugging { |
| 201 |
return |
| 202 |
} |
| 203 |
|
| 204 |
debugf("Mapping for %s:\n", fn) |
| 205 |
var keys []ir.Value |
| 206 |
for k := range m { |
| 207 |
keys = append(keys, k) |
| 208 |
} |
| 209 |
slices.SortFunc(keys, func(a, b ir.Value) int { |
| 210 |
return cmp.Compare(fmt.Sprintf("%v", a), fmt.Sprintf("%v", b)) |
| 211 |
}) |
| 212 |
for _, k := range keys { |
| 213 |
v := m[k] |
| 214 |
debugf("\t%v\n", v) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// BinaryTable returns a binary operator based on the provided mapping. For |
| 219 |
// missing pairs of values, the default value will be returned. |
| 220 |
func BinaryTable[Elem comparable](default_ Elem, m map[[2]Elem]Elem) func(Elem, Elem) Elem { |
| 221 |
return func(a, b Elem) Elem { |
| 222 |
if d, ok := m[[2]Elem{a, b}]; ok { |
| 223 |
return d |
| 224 |
} else if d, ok := m[[2]Elem{b, a}]; ok { |
| 225 |
return d |
| 226 |
} else { |
| 227 |
return default_ |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func PowerSet[Elem constraints.Integer](all Elem) []Elem { |
| 233 |
out := make([]Elem, all+1) |
| 234 |
for i := range out { |
| 235 |
out[i] = Elem(i) |
| 236 |
} |
| 237 |
return out |
| 238 |
} |
| 239 |
|
| 240 |
func MapSet[Elem constraints.Integer](set Elem, fn func(Elem) Elem) Elem { |
| 241 |
bits := 64 - bits.LeadingZeros64(uint64(set)) |
| 242 |
var out Elem |
| 243 |
for i := range bits { |
| 244 |
if b := (set & (1 << i)); b != 0 { |
| 245 |
out |= fn(b) |
| 246 |
} |
| 247 |
} |
| 248 |
return out |
| 249 |
} |
| 250 |
|
| 251 |
func MapCartesianProduct[Elem constraints.Integer](x, y Elem, fn func(Elem, Elem) Elem) Elem { |
| 252 |
bitsX := 64 - bits.LeadingZeros64(uint64(x)) |
| 253 |
bitsY := 64 - bits.LeadingZeros64(uint64(y)) |
| 254 |
|
| 255 |
var out Elem |
| 256 |
for i := range bitsX { |
| 257 |
for j := range bitsY { |
| 258 |
bx := x & (1 << i) |
| 259 |
by := y & (1 << j) |
| 260 |
|
| 261 |
if bx != 0 && by != 0 { |
| 262 |
out |= fn(bx, by) |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return out |
| 268 |
} |