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.
pull/925/head
Daniel Martí 2 months ago
parent fa2e718bd1
commit cb83c50b13

@ -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 // The term set of an interface is the intersection of the term sets of its
// embedded types. // embedded types.
tset.terms = typeparams_allTermlist tset.terms = typeparams_allTermlist
for i := 0; i < u.NumEmbeddeds(); i++ { for i := range u.NumEmbeddeds() {
embedded := u.EmbeddedType(i) embedded := u.EmbeddedType(i)
if _, ok := embedded.Underlying().(*types.TypeParam); ok { if _, ok := embedded.Underlying().(*types.TypeParam); ok {
return nil, fmt.Errorf("invalid embedded type %T", embedded) 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: case *types.Union:
// The term set of a union is the union of term sets of its terms. // The term set of a union is the union of term sets of its terms.
tset.terms = nil tset.terms = nil
for i := 0; i < u.Len(); i++ { for i := range u.Len() {
t := u.Term(i) t := u.Term(i)
var terms typeparams_termlist var terms typeparams_termlist
switch t.Type().Underlying().(type) { switch t.Type().Underlying().(type) {

@ -37,7 +37,7 @@ type typeutil_hasher struct{ inGenericSig bool }
// hashString computes the FowlerNollVo hash of s. // hashString computes the FowlerNollVo hash of s.
func typeutil_hashString(s string) uint32 { func typeutil_hashString(s string) uint32 {
var h uint32 var h uint32
for i := 0; i < len(s); i++ { for i := range len(s) {
h ^= uint32(s[i]) h ^= uint32(s[i])
h *= 16777619 h *= 16777619
} }
@ -129,7 +129,7 @@ func (h typeutil_hasher) hash(t types.Type) uint32 {
case *types.Named: case *types.Named:
hash := h.hashTypeName(t.Obj()) hash := h.hashTypeName(t.Obj())
targs := t.TypeArgs() targs := t.TypeArgs()
for i := 0; i < targs.Len(); i++ { for i := range targs.Len() {
targ := targs.At(i) targ := targs.At(i)
hash += 2 * h.hash(targ) hash += 2 * h.hash(targ)
} }

@ -200,7 +200,7 @@ func Obfuscate(fset *token.FileSet, ssaPkg *ssa.Package, files []*ast.File, obfR
if trashBlockCount > 0 { if trashBlockCount > 0 {
addTrashBlockMarkers(ssaFunc, trashBlockCount, obfRand) addTrashBlockMarkers(ssaFunc, trashBlockCount, obfRand)
} }
for i := 0; i < split; i++ { for range split {
if !applySplitting(ssaFunc, obfRand) { if !applySplitting(ssaFunc, obfRand) {
break // no more candidates for splitting 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) addJunkBlocks(ssaFunc, junkCount, obfRand)
} }
var dispatchers []dispatcherInfo var dispatchers []dispatcherInfo
for i := 0; i < passes; i++ { for range passes {
if info := applyFlattening(ssaFunc, obfRand); info != nil { if info := applyFlattening(ssaFunc, obfRand); info != nil {
dispatchers = append(dispatchers, info) 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) { 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) keySize := literals.MinSize + mathrand.Intn(literals.MinSize)
delegateCount := keySize
// Reusing multiple times one decryption function is fine, // Reusing multiple times one decryption function is fine,
// but it doesn't make sense to generate more functions than keys. // but it doesn't make sense to generate more functions than keys.
if delegateCount > len(dispatcher) { delegateCount := min(keySize, len(dispatcher))
delegateCount = len(dispatcher)
}
delegateKeyIdxs := rnd.Perm(keySize)[:delegateCount] delegateKeyIdxs := rnd.Perm(keySize)[:delegateCount]
delegateLocalKeys := generateKeys(delegateCount, nil, rnd) delegateLocalKeys := generateKeys(delegateCount, nil, rnd)
@ -184,7 +181,7 @@ func (delegateTableHardening) Apply(dispatcher []cfgInfo, ssaRemap map[ssa.Value
} }
delegatesAst := make([]ast.Expr, delegateCount) delegatesAst := make([]ast.Expr, delegateCount)
for i := 0; i < delegateCount; i++ { for i := range delegateCount {
// Code for single decryption delegate: // Code for single decryption delegate:
/* /*
func(i int) int { func(i int) int {

@ -152,7 +152,7 @@ func addJunkBlocks(ssaFunc *ssa.Function, count int, obfRand *mathrand.Rand) {
return return
} }
for i := 0; i < count; i++ { for i := range count {
targetBlock := candidates[obfRand.Intn(len(candidates))] targetBlock := candidates[obfRand.Intn(len(candidates))]
succsIdx := obfRand.Intn(len(targetBlock.Succs)) succsIdx := obfRand.Intn(len(targetBlock.Succs))
succs := targetBlock.Succs[succsIdx] succs := targetBlock.Succs[succsIdx]
@ -254,7 +254,7 @@ func addTrashBlockMarkers(ssaFunc *ssa.Function, count int, obfRand *mathrand.Ra
return return
} }
for i := 0; i < count; i++ { for range count {
targetBlock := candidates[obfRand.Intn(len(candidates))] targetBlock := candidates[obfRand.Intn(len(candidates))]
succsIdx := obfRand.Intn(len(targetBlock.Succs)) succsIdx := obfRand.Intn(len(targetBlock.Succs))
succs := targetBlock.Succs[succsIdx] succs := targetBlock.Succs[succsIdx]

@ -178,7 +178,7 @@ func isSupportedSig(m *types.Func) bool {
if isGenericType(sig) { if isGenericType(sig) {
return false return false
} }
for i := 0; i < sig.Params().Len(); i++ { for i := range sig.Params().Len() {
if !isSupportedType(sig.Params().At(i).Type()) { if !isSupportedType(sig.Params().At(i).Type()) {
return false return false
} }
@ -363,7 +363,7 @@ func (t *trashGenerator) cacheMethods(vars map[string]*definedVar) {
var methods []*types.Func var methods []*types.Func
switch typ := typ.(type) { switch typ := typ.(type) {
case methodSet: 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) { if m := typ.Method(i); token.IsExported(m.Name()) && isSupportedSig(m) {
methods = append(methods, m) methods = append(methods, m)
if len(methods) > limitFunctionCount { if len(methods) > limitFunctionCount {
@ -432,7 +432,7 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {
targetSig := targetFunc.Signature() targetSig := targetFunc.Signature()
params := targetSig.Params() params := targetSig.Params()
for i := 0; i < params.Len(); i++ { for i := range params.Len() {
param := params.At(i) param := params.At(i)
if !targetSig.Variadic() || i != params.Len()-1 { if !targetSig.Variadic() || i != params.Len()-1 {
args = append(args, t.generateRandomValue(param.Type(), vars)) 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) variadicCount := t.rand.Intn(maxVariadicParams)
for i := 0; i < variadicCount; i++ { for range variadicCount {
sliceTyp, ok := param.Type().(*types.Slice) sliceTyp, ok := param.Type().(*types.Slice)
if !ok { if !ok {
panic(fmt.Errorf("unsupported variadic type: %v", param.Type())) 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}, Rhs: []ast.Expr{callExpr},
} }
for i := 0; i < results.Len(); i++ { for i := range results.Len() {
ident := ast.NewIdent(getRandomName(t.rand)) ident := ast.NewIdent(getRandomName(t.rand))
vars[ident.Name] = &definedVar{ vars[ident.Name] = &definedVar{
Type: results.At(i).Type(), 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] varNames[i], varNames[j] = varNames[j], varNames[i]
}) })
varCount := 1 + t.rand.Intn(maxAssignVars) varCount := min(1+t.rand.Intn(maxAssignVars), len(varNames))
if varCount > len(varNames) {
varCount = len(varNames)
}
assignStmt := &ast.AssignStmt{ assignStmt := &ast.AssignStmt{
Tok: token.ASSIGN, Tok: token.ASSIGN,
@ -537,7 +534,7 @@ func (t *trashGenerator) Generate(statementCount int, externalVars map[string]ty
} }
var stmts []ast.Stmt var stmts []ast.Stmt
for i := 0; i < statementCount; i++ { for range statementCount {
var stmt ast.Stmt var stmt ast.Stmt
if len(vars) >= minVarsForAssign && t.rand.Float32() < assignVarProb { if len(vars) >= minVarsForAssign && t.rand.Float32() < assignVarProb {
stmt = t.generateAssign(vars) stmt = t.generateAssign(vars)

@ -38,7 +38,7 @@ var (
func genRandIntSlice(obfRand *mathrand.Rand, max, count int) []int { func genRandIntSlice(obfRand *mathrand.Rand, max, count int) []int {
indexes := make([]int, count) indexes := make([]int, count)
for i := 0; i < count; i++ { for i := range count {
indexes[i] = obfRand.Intn(max) indexes[i] = obfRand.Intn(max)
} }
return indexes return indexes

@ -30,10 +30,7 @@ func splitIntoRandomChunks(obfRand *mathrand.Rand, data []byte) [][]byte {
var chunks [][]byte var chunks [][]byte
for len(data) > 0 { for len(data) > 0 {
chunkSize := 1 + obfRand.Intn(maxChunkSize) chunkSize := min(1+obfRand.Intn(maxChunkSize), len(data))
if chunkSize > len(data) {
chunkSize = len(data)
}
chunks = append(chunks, data[:chunkSize]) chunks = append(chunks, data[:chunkSize])
data = data[chunkSize:] data = data[chunkSize:]

@ -6,6 +6,7 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"go/types" "go/types"
"maps"
"slices" "slices"
"sort" "sort"
"strconv" "strconv"
@ -459,7 +460,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
localTuple := true localTuple := true
tmpVars := make(map[string]types.Type) 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) name, typ, hasRefs := fc.tupleVarNameAndType(r, i)
tmpVars[name] = typ tmpVars[name] = typ
if hasRefs { if hasRefs {
@ -469,9 +470,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
} }
if !localTuple { if !localTuple {
for n, t := range tmpVars { maps.Copy(astFunc.Vars, tmpVars)
astFunc.Vars[n] = t
}
} }
return assignStmt return assignStmt

@ -58,7 +58,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
case *types.Interface: case *types.Interface:
methods := &ast.FieldList{} methods := &ast.FieldList{}
hasComparable := false hasComparable := false
for i := 0; i < typ.NumEmbeddeds(); i++ { for i := range typ.NumEmbeddeds() {
embeddedType := typ.EmbeddedType(i) embeddedType := typ.EmbeddedType(i)
if namedType, ok := embeddedType.(*types.Named); ok && namedType.String() == "comparable" { if namedType, ok := embeddedType.(*types.Named); ok && namedType.String() == "comparable" {
hasComparable = true hasComparable = true
@ -74,7 +74,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
if !hasComparable && typ.IsComparable() { if !hasComparable && typ.IsComparable() {
methods.List = append(methods.List, &ast.Field{Type: ast.NewIdent("comparable")}) 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) method := typ.ExplicitMethod(i)
methodSig, err := tc.Convert(method.Type()) methodSig, err := tc.Convert(method.Type())
if err != nil { 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 // reference to unexported named emulated through new interface with explicit declarated methods
if !token.IsExported(obj.Name()) { if !token.IsExported(obj.Name()) {
var methods []*types.Func var methods []*types.Func
for i := 0; i < typ.NumMethods(); i++ { for i := range typ.NumMethods() {
method := typ.Method(i) method := typ.Method(i)
if token.IsExported(method.Name()) { if token.IsExported(method.Name()) {
methods = append(methods, method) 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 return &ast.IndexExpr{X: namedExpr, Index: typeParamExpr}, nil
} }
genericExpr := &ast.IndexListExpr{X: namedExpr} genericExpr := &ast.IndexListExpr{X: namedExpr}
for i := 0; i < typeParams.Len(); i++ { for i := range typeParams.Len() {
typeArgs := typeParams.At(i) typeArgs := typeParams.At(i)
typeParamExpr, err := tc.Convert(typeArgs) typeParamExpr, err := tc.Convert(typeArgs)
if err != nil { if err != nil {
@ -157,7 +157,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
case *types.Signature: case *types.Signature:
funcSigExpr := &ast.FuncType{Params: &ast.FieldList{}} funcSigExpr := &ast.FuncType{Params: &ast.FieldList{}}
if sigParams := typ.Params(); sigParams != nil { if sigParams := typ.Params(); sigParams != nil {
for i := 0; i < sigParams.Len(); i++ { for i := range sigParams.Len() {
param := sigParams.At(i) param := sigParams.At(i)
var paramType ast.Expr var paramType ast.Expr
@ -185,7 +185,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
} }
if sigResults := typ.Results(); sigResults != nil { if sigResults := typ.Results(); sigResults != nil {
funcSigExpr.Results = &ast.FieldList{} funcSigExpr.Results = &ast.FieldList{}
for i := 0; i < sigResults.Len(); i++ { for i := range sigResults.Len() {
result := sigResults.At(i) result := sigResults.At(i)
resultExpr, err := tc.Convert(result.Type()) resultExpr, err := tc.Convert(result.Type())
if err != nil { if err != nil {
@ -201,7 +201,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
} }
if typeParams := typ.TypeParams(); typeParams != nil { if typeParams := typ.TypeParams(); typeParams != nil {
funcSigExpr.TypeParams = &ast.FieldList{} funcSigExpr.TypeParams = &ast.FieldList{}
for i := 0; i < typeParams.Len(); i++ { for i := range typeParams.Len() {
typeParam := typeParams.At(i) typeParam := typeParams.At(i)
resultExpr, err := tc.Convert(typeParam.Constraint().Underlying()) resultExpr, err := tc.Convert(typeParam.Constraint().Underlying())
if err != nil { if err != nil {
@ -220,7 +220,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
return &ast.ArrayType{Elt: eltExpr}, nil return &ast.ArrayType{Elt: eltExpr}, nil
case *types.Struct: case *types.Struct:
fieldList := &ast.FieldList{} fieldList := &ast.FieldList{}
for i := 0; i < typ.NumFields(); i++ { for i := range typ.NumFields() {
f := typ.Field(i) f := typ.Field(i)
fieldExpr, err := tc.Convert(f.Type()) fieldExpr, err := tc.Convert(f.Type())
if err != nil { if err != nil {
@ -240,7 +240,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
return ast.NewIdent(typ.Obj().Name()), nil return ast.NewIdent(typ.Obj().Name()), nil
case *types.Union: case *types.Union:
var unionExpr ast.Expr var unionExpr ast.Expr
for i := 0; i < typ.Len(); i++ { for i := range typ.Len() {
term := typ.Term(i) term := typ.Term(i)
expr, err := tc.Convert(term.Type()) expr, err := tc.Convert(term.Type())
if err != nil { if err != nil {

Loading…
Cancel
Save