Optimize fake line number

Now fake line numbers are generated in the range from 1 to the number of methods
pull/111/head
pagran 4 years ago committed by GitHub
parent d3af58b558
commit b3f04e53d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -79,11 +79,14 @@ func findBuildTags(commentGroups []*ast.CommentGroup) (buildTags []string) {
return buildTags
}
func transformLineInfo(fileIndex int, file *ast.File) ([]string, *ast.File) {
func transformLineInfo(file *ast.File) ([]string, *ast.File) {
// Save build tags and add file name leak protection
extraComments := append(findBuildTags(file.Comments), "", "//line :1")
file.Comments = nil
newLines := mathrand.Perm(len(file.Decls))
funcCounter := 0
pre := func(cursor *astutil.Cursor) bool {
node := cursor.Node()
clearNodeComments(node)
@ -98,10 +101,9 @@ func transformLineInfo(fileIndex int, file *ast.File) ([]string, *ast.File) {
return true
}
// TODO: Optimize the generated values of line numbers to reduce space usage.
linePos := hashWithAsUint64(buildInfo.buildID, fmt.Sprintf("%d:%s", fileIndex, funcDecl.Name), PosMin, PosMax)
comment := &ast.Comment{Text: fmt.Sprintf("//line %c.go:%d", nameCharset[mathrand.Intn(len(nameCharset))], linePos)}
comment := &ast.Comment{Text: fmt.Sprintf("//line %c.go:%d", nameCharset[mathrand.Intn(len(nameCharset))], 1+newLines[funcCounter])}
funcDecl.Doc = prependComment(funcDecl.Doc, comment)
funcCounter++
return true
}

@ -292,7 +292,7 @@ func mainErr(args []string) error {
modpath, err := exec.Command("go", "list", "-m").Output()
if err == nil {
path := string(bytes.TrimSpace(modpath))
envGoPrivate = path+","+path+"_test"
envGoPrivate = path + "," + path + "_test"
}
}
// Explicitly set GOPRIVATE, since future garble processes won't
@ -526,7 +526,7 @@ func transformCompile(args []string) ([]string, error) {
// messy.
name = "_cgo_" + name
default:
extraComments, file = transformLineInfo(i, file)
extraComments, file = transformLineInfo(file)
file = transformGo(file, info, blacklist)
// Uncomment for some quick debugging. Do not delete.
@ -683,16 +683,6 @@ func hashWith(salt, value string) string {
return "z" + sum[:length]
}
func hashWithAsUint64(salt, value string, min, max uint64) uint64 {
d := sha256.New()
io.WriteString(d, salt)
d.Write(seed)
io.WriteString(d, value)
sum := d.Sum(nil)
val := binary.LittleEndian.Uint64(sum)
return min + (val % (max - min))
}
// buildBlacklist collects all the objects in a package which are known to be
// used with reflect.TypeOf or reflect.ValueOf. Since we obfuscate one package
// at a time, we only detect those if the type definition and the reflect usage

Loading…
Cancel
Save