-
Notifications
You must be signed in to change notification settings - Fork 8
/
bitwise.go
52 lines (44 loc) · 1.02 KB
/
bitwise.go
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
package bitwise
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type Bitwise struct {
mutations map[token.Token]token.Token
}
// New creates a new Bitwise virus.
//
// It replaces `&` with `|`, `|` with `&`, `^` with `&`, `&^` with `&`, `<<`
// with `>>` and `>>` with `<<`.
func New() *Bitwise {
return &Bitwise{
mutations: map[token.Token]token.Token{
token.AND: token.OR,
token.OR: token.AND,
token.XOR: token.AND,
token.AND_NOT: token.AND,
token.SHL: token.SHR,
token.SHR: token.SHL,
},
}
}
func (v *Bitwise) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
expression, matches := node.(*ast.BinaryExpr)
if !matches {
return nil
}
originalOperation := expression.Op
mutatedOperation, matches := v.mutations[expression.Op]
if !matches {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Bitwise",
func() { expression.Op = mutatedOperation },
func() { expression.Op = originalOperation },
),
}
}