mirror of
https://github.com/burrowers/garble.git
synced 2025-09-26 20:01:16 +08:00
all: run gopls's modernize -fix
Except on reflect_abi_code.go, as that needs to be compatible with older versions of Go given that we inject its code.
This commit is contained in:
@@ -85,7 +85,7 @@ func typeparams_computeTermSetInternal(t types.Type, seen map[types.Type]*typepa
|
||||
// The term set of an interface is the intersection of the term sets of its
|
||||
// embedded types.
|
||||
tset.terms = typeparams_allTermlist
|
||||
for i := 0; i < u.NumEmbeddeds(); i++ {
|
||||
for i := range u.NumEmbeddeds() {
|
||||
embedded := u.EmbeddedType(i)
|
||||
if _, ok := embedded.Underlying().(*types.TypeParam); ok {
|
||||
return nil, fmt.Errorf("invalid embedded type %T", embedded)
|
||||
@@ -99,7 +99,7 @@ func typeparams_computeTermSetInternal(t types.Type, seen map[types.Type]*typepa
|
||||
case *types.Union:
|
||||
// The term set of a union is the union of term sets of its terms.
|
||||
tset.terms = nil
|
||||
for i := 0; i < u.Len(); i++ {
|
||||
for i := range u.Len() {
|
||||
t := u.Term(i)
|
||||
var terms typeparams_termlist
|
||||
switch t.Type().Underlying().(type) {
|
||||
|
@@ -37,7 +37,7 @@ type typeutil_hasher struct{ inGenericSig bool }
|
||||
// hashString computes the Fowler–Noll–Vo hash of s.
|
||||
func typeutil_hashString(s string) uint32 {
|
||||
var h uint32
|
||||
for i := 0; i < len(s); i++ {
|
||||
for i := range len(s) {
|
||||
h ^= uint32(s[i])
|
||||
h *= 16777619
|
||||
}
|
||||
@@ -129,7 +129,7 @@ func (h typeutil_hasher) hash(t types.Type) uint32 {
|
||||
case *types.Named:
|
||||
hash := h.hashTypeName(t.Obj())
|
||||
targs := t.TypeArgs()
|
||||
for i := 0; i < targs.Len(); i++ {
|
||||
for i := range targs.Len() {
|
||||
targ := targs.At(i)
|
||||
hash += 2 * h.hash(targ)
|
||||
}
|
||||
|
@@ -200,7 +200,7 @@ func Obfuscate(fset *token.FileSet, ssaPkg *ssa.Package, files []*ast.File, obfR
|
||||
if trashBlockCount > 0 {
|
||||
addTrashBlockMarkers(ssaFunc, trashBlockCount, obfRand)
|
||||
}
|
||||
for i := 0; i < split; i++ {
|
||||
for range split {
|
||||
if !applySplitting(ssaFunc, obfRand) {
|
||||
break // no more candidates for splitting
|
||||
}
|
||||
@@ -209,7 +209,7 @@ func Obfuscate(fset *token.FileSet, ssaPkg *ssa.Package, files []*ast.File, obfR
|
||||
addJunkBlocks(ssaFunc, junkCount, obfRand)
|
||||
}
|
||||
var dispatchers []dispatcherInfo
|
||||
for i := 0; i < passes; i++ {
|
||||
for range passes {
|
||||
if info := applyFlattening(ssaFunc, obfRand); info != nil {
|
||||
dispatchers = append(dispatchers, info)
|
||||
}
|
||||
|
@@ -150,13 +150,10 @@ type delegateTableHardening struct{}
|
||||
|
||||
func (delegateTableHardening) Apply(dispatcher []cfgInfo, ssaRemap map[ssa.Value]ast.Expr, rnd *mathrand.Rand) (ast.Decl, ast.Stmt) {
|
||||
keySize := literals.MinSize + mathrand.Intn(literals.MinSize)
|
||||
delegateCount := keySize
|
||||
|
||||
// Reusing multiple times one decryption function is fine,
|
||||
// but it doesn't make sense to generate more functions than keys.
|
||||
if delegateCount > len(dispatcher) {
|
||||
delegateCount = len(dispatcher)
|
||||
}
|
||||
delegateCount := min(keySize, len(dispatcher))
|
||||
|
||||
delegateKeyIdxs := rnd.Perm(keySize)[:delegateCount]
|
||||
delegateLocalKeys := generateKeys(delegateCount, nil, rnd)
|
||||
@@ -184,7 +181,7 @@ func (delegateTableHardening) Apply(dispatcher []cfgInfo, ssaRemap map[ssa.Value
|
||||
}
|
||||
|
||||
delegatesAst := make([]ast.Expr, delegateCount)
|
||||
for i := 0; i < delegateCount; i++ {
|
||||
for i := range delegateCount {
|
||||
// Code for single decryption delegate:
|
||||
/*
|
||||
func(i int) int {
|
||||
|
@@ -152,7 +152,7 @@ func addJunkBlocks(ssaFunc *ssa.Function, count int, obfRand *mathrand.Rand) {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
for i := range count {
|
||||
targetBlock := candidates[obfRand.Intn(len(candidates))]
|
||||
succsIdx := obfRand.Intn(len(targetBlock.Succs))
|
||||
succs := targetBlock.Succs[succsIdx]
|
||||
@@ -254,7 +254,7 @@ func addTrashBlockMarkers(ssaFunc *ssa.Function, count int, obfRand *mathrand.Ra
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
for range count {
|
||||
targetBlock := candidates[obfRand.Intn(len(candidates))]
|
||||
succsIdx := obfRand.Intn(len(targetBlock.Succs))
|
||||
succs := targetBlock.Succs[succsIdx]
|
||||
|
@@ -178,7 +178,7 @@ func isSupportedSig(m *types.Func) bool {
|
||||
if isGenericType(sig) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < sig.Params().Len(); i++ {
|
||||
for i := range sig.Params().Len() {
|
||||
if !isSupportedType(sig.Params().At(i).Type()) {
|
||||
return false
|
||||
}
|
||||
@@ -363,7 +363,7 @@ func (t *trashGenerator) cacheMethods(vars map[string]*definedVar) {
|
||||
var methods []*types.Func
|
||||
switch typ := typ.(type) {
|
||||
case methodSet:
|
||||
for i := 0; i < typ.NumMethods(); i++ {
|
||||
for i := range typ.NumMethods() {
|
||||
if m := typ.Method(i); token.IsExported(m.Name()) && isSupportedSig(m) {
|
||||
methods = append(methods, m)
|
||||
if len(methods) > limitFunctionCount {
|
||||
@@ -432,7 +432,7 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {
|
||||
|
||||
targetSig := targetFunc.Signature()
|
||||
params := targetSig.Params()
|
||||
for i := 0; i < params.Len(); i++ {
|
||||
for i := range params.Len() {
|
||||
param := params.At(i)
|
||||
if !targetSig.Variadic() || i != params.Len()-1 {
|
||||
args = append(args, t.generateRandomValue(param.Type(), vars))
|
||||
@@ -440,7 +440,7 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {
|
||||
}
|
||||
|
||||
variadicCount := t.rand.Intn(maxVariadicParams)
|
||||
for i := 0; i < variadicCount; i++ {
|
||||
for range variadicCount {
|
||||
sliceTyp, ok := param.Type().(*types.Slice)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("unsupported variadic type: %v", param.Type()))
|
||||
@@ -472,7 +472,7 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {
|
||||
Rhs: []ast.Expr{callExpr},
|
||||
}
|
||||
|
||||
for i := 0; i < results.Len(); i++ {
|
||||
for i := range results.Len() {
|
||||
ident := ast.NewIdent(getRandomName(t.rand))
|
||||
vars[ident.Name] = &definedVar{
|
||||
Type: results.At(i).Type(),
|
||||
@@ -501,10 +501,7 @@ func (t *trashGenerator) generateAssign(vars map[string]*definedVar) ast.Stmt {
|
||||
varNames[i], varNames[j] = varNames[j], varNames[i]
|
||||
})
|
||||
|
||||
varCount := 1 + t.rand.Intn(maxAssignVars)
|
||||
if varCount > len(varNames) {
|
||||
varCount = len(varNames)
|
||||
}
|
||||
varCount := min(1+t.rand.Intn(maxAssignVars), len(varNames))
|
||||
|
||||
assignStmt := &ast.AssignStmt{
|
||||
Tok: token.ASSIGN,
|
||||
@@ -537,7 +534,7 @@ func (t *trashGenerator) Generate(statementCount int, externalVars map[string]ty
|
||||
}
|
||||
|
||||
var stmts []ast.Stmt
|
||||
for i := 0; i < statementCount; i++ {
|
||||
for range statementCount {
|
||||
var stmt ast.Stmt
|
||||
if len(vars) >= minVarsForAssign && t.rand.Float32() < assignVarProb {
|
||||
stmt = t.generateAssign(vars)
|
||||
|
@@ -38,7 +38,7 @@ var (
|
||||
|
||||
func genRandIntSlice(obfRand *mathrand.Rand, max, count int) []int {
|
||||
indexes := make([]int, count)
|
||||
for i := 0; i < count; i++ {
|
||||
for i := range count {
|
||||
indexes[i] = obfRand.Intn(max)
|
||||
}
|
||||
return indexes
|
||||
|
@@ -30,10 +30,7 @@ func splitIntoRandomChunks(obfRand *mathrand.Rand, data []byte) [][]byte {
|
||||
|
||||
var chunks [][]byte
|
||||
for len(data) > 0 {
|
||||
chunkSize := 1 + obfRand.Intn(maxChunkSize)
|
||||
if chunkSize > len(data) {
|
||||
chunkSize = len(data)
|
||||
}
|
||||
chunkSize := min(1+obfRand.Intn(maxChunkSize), len(data))
|
||||
|
||||
chunks = append(chunks, data[:chunkSize])
|
||||
data = data[chunkSize:]
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"maps"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -459,7 +460,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
|
||||
localTuple := true
|
||||
tmpVars := make(map[string]types.Type)
|
||||
|
||||
for i := 0; i < tuple.Len(); i++ {
|
||||
for i := range tuple.Len() {
|
||||
name, typ, hasRefs := fc.tupleVarNameAndType(r, i)
|
||||
tmpVars[name] = typ
|
||||
if hasRefs {
|
||||
@@ -469,9 +470,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
|
||||
}
|
||||
|
||||
if !localTuple {
|
||||
for n, t := range tmpVars {
|
||||
astFunc.Vars[n] = t
|
||||
}
|
||||
maps.Copy(astFunc.Vars, tmpVars)
|
||||
}
|
||||
|
||||
return assignStmt
|
||||
|
@@ -58,7 +58,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
case *types.Interface:
|
||||
methods := &ast.FieldList{}
|
||||
hasComparable := false
|
||||
for i := 0; i < typ.NumEmbeddeds(); i++ {
|
||||
for i := range typ.NumEmbeddeds() {
|
||||
embeddedType := typ.EmbeddedType(i)
|
||||
if namedType, ok := embeddedType.(*types.Named); ok && namedType.String() == "comparable" {
|
||||
hasComparable = true
|
||||
@@ -74,7 +74,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
if !hasComparable && typ.IsComparable() {
|
||||
methods.List = append(methods.List, &ast.Field{Type: ast.NewIdent("comparable")})
|
||||
}
|
||||
for i := 0; i < typ.NumExplicitMethods(); i++ {
|
||||
for i := range typ.NumExplicitMethods() {
|
||||
method := typ.ExplicitMethod(i)
|
||||
methodSig, err := tc.Convert(method.Type())
|
||||
if err != nil {
|
||||
@@ -112,7 +112,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
// reference to unexported named emulated through new interface with explicit declarated methods
|
||||
if !token.IsExported(obj.Name()) {
|
||||
var methods []*types.Func
|
||||
for i := 0; i < typ.NumMethods(); i++ {
|
||||
for i := range typ.NumMethods() {
|
||||
method := typ.Method(i)
|
||||
if token.IsExported(method.Name()) {
|
||||
methods = append(methods, method)
|
||||
@@ -139,7 +139,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
return &ast.IndexExpr{X: namedExpr, Index: typeParamExpr}, nil
|
||||
}
|
||||
genericExpr := &ast.IndexListExpr{X: namedExpr}
|
||||
for i := 0; i < typeParams.Len(); i++ {
|
||||
for i := range typeParams.Len() {
|
||||
typeArgs := typeParams.At(i)
|
||||
typeParamExpr, err := tc.Convert(typeArgs)
|
||||
if err != nil {
|
||||
@@ -157,7 +157,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
case *types.Signature:
|
||||
funcSigExpr := &ast.FuncType{Params: &ast.FieldList{}}
|
||||
if sigParams := typ.Params(); sigParams != nil {
|
||||
for i := 0; i < sigParams.Len(); i++ {
|
||||
for i := range sigParams.Len() {
|
||||
param := sigParams.At(i)
|
||||
|
||||
var paramType ast.Expr
|
||||
@@ -185,7 +185,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
}
|
||||
if sigResults := typ.Results(); sigResults != nil {
|
||||
funcSigExpr.Results = &ast.FieldList{}
|
||||
for i := 0; i < sigResults.Len(); i++ {
|
||||
for i := range sigResults.Len() {
|
||||
result := sigResults.At(i)
|
||||
resultExpr, err := tc.Convert(result.Type())
|
||||
if err != nil {
|
||||
@@ -201,7 +201,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
}
|
||||
if typeParams := typ.TypeParams(); typeParams != nil {
|
||||
funcSigExpr.TypeParams = &ast.FieldList{}
|
||||
for i := 0; i < typeParams.Len(); i++ {
|
||||
for i := range typeParams.Len() {
|
||||
typeParam := typeParams.At(i)
|
||||
resultExpr, err := tc.Convert(typeParam.Constraint().Underlying())
|
||||
if err != nil {
|
||||
@@ -220,7 +220,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
return &ast.ArrayType{Elt: eltExpr}, nil
|
||||
case *types.Struct:
|
||||
fieldList := &ast.FieldList{}
|
||||
for i := 0; i < typ.NumFields(); i++ {
|
||||
for i := range typ.NumFields() {
|
||||
f := typ.Field(i)
|
||||
fieldExpr, err := tc.Convert(f.Type())
|
||||
if err != nil {
|
||||
@@ -240,7 +240,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
|
||||
return ast.NewIdent(typ.Obj().Name()), nil
|
||||
case *types.Union:
|
||||
var unionExpr ast.Expr
|
||||
for i := 0; i < typ.Len(); i++ {
|
||||
for i := range typ.Len() {
|
||||
term := typ.Term(i)
|
||||
expr, err := tc.Convert(term.Type())
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user