You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
garble/testdata/script/ldflags.txtar

74 lines
2.1 KiB
Plaintext

# Note the proper domain, since the dot adds an edge case.
#
# Also note that there are three forms of -X allowed:
#
# -X=name=value
# -X name=value
# -X "name=value" (or with single quotes, allows spaces in value)
slight simplifications and alloc reductions Reuse a buffer and a map across loop iterations, because we can. Make recordTypeDone only track named types, as that is enough to detect type cycles. Without named types, there can be no cycles. These two reduce allocs by a fraction of a percent: name old time/op new time/op delta Build-16 10.4s ± 2% 10.4s ± 1% ~ (p=0.739 n=10+10) name old bin-B new bin-B delta Build-16 5.51M ± 0% 5.51M ± 0% ~ (all equal) name old cached-time/op new cached-time/op delta Build-16 391ms ± 9% 407ms ± 7% ~ (p=0.095 n=10+9) name old mallocs/op new mallocs/op delta Build-16 34.5M ± 0% 34.4M ± 0% -0.12% (p=0.000 n=10+10) name old sys-time/op new sys-time/op delta Build-16 5.87s ± 5% 5.82s ± 5% ~ (p=0.182 n=10+9) It doesn't seem like much, but remember that these stats are for the entire set of processes, where garble only accounts for about 10% of the total wall time when compared to the compiler or linker. So a ~0.1% decrease globally is still significant. linkerVariableStrings is also indexed by *types.Var rather than types.Object, since -ldflags=-X only supports setting the string value of variables. This shouldn't make a significant difference in terms of allocs, but at least the map is less prone to confusion with other object types. To ensure the new code doesn't trip up on non-variables, we add test cases. Finally, for the sake of clarity, index into the types.Info maps like Defs and Uses rather than calling ObjectOf if we know whether the identifier we have is a definition of a name or the use of a defined name. This isn't better in terms of performance, as ObjectOf is a tiny method, but just like with linkerVariableStrings before, the new code is clearer.
2 years ago
env LDFLAGS='-X=main.unexportedVersion=v1.22.33 -X=main.replacedWithEmpty= -X "main.replacedWithSpaces= foo bar " -X=domain.test/main/imported.ExportedUnset=garble_replaced -X=domain.test/missing/path.missingVar=value -X=main.someType=notAVariable'
exec garble build -ldflags=${LDFLAGS}
exec ./main
cmp stdout main.stdout
! binsubstr main$exe 'domain.test/main' 'unexportedVersion' 'ExportedUnset'
[short] stop # no need to verify this with -short
exec garble -tiny -literals -seed=0002deadbeef build -ldflags=${LDFLAGS}
exec ./main
cmp stdout main.stdout
! binsubstr main$exe 'unexportedVersion' 'ExportedUnset'
binsubstr main$exe 'v1.22.33' 'garble_replaced' # TODO: obfuscate injected strings too
binsubstr main$exe 'kept_before' 'kept_after' # TODO: obfuscate strings near ldflags vars
go build -ldflags=${LDFLAGS}
exec ./main
cmp stdout main.stdout
binsubstr main$exe 'unexportedVersion' 'ExportedUnset' 'v1.22.33' 'garble_replaced'
-- go.mod --
module domain.test/main
go 1.20
-- main.go --
package main
import (
"fmt"
"domain.test/main/imported"
)
var unexportedVersion = "unknown"
var notReplacedBefore, replacedWithEmpty, notReplacedAfter = "kept_before", "original", "kept_after"
var replacedWithSpaces = "original"
slight simplifications and alloc reductions Reuse a buffer and a map across loop iterations, because we can. Make recordTypeDone only track named types, as that is enough to detect type cycles. Without named types, there can be no cycles. These two reduce allocs by a fraction of a percent: name old time/op new time/op delta Build-16 10.4s ± 2% 10.4s ± 1% ~ (p=0.739 n=10+10) name old bin-B new bin-B delta Build-16 5.51M ± 0% 5.51M ± 0% ~ (all equal) name old cached-time/op new cached-time/op delta Build-16 391ms ± 9% 407ms ± 7% ~ (p=0.095 n=10+9) name old mallocs/op new mallocs/op delta Build-16 34.5M ± 0% 34.4M ± 0% -0.12% (p=0.000 n=10+10) name old sys-time/op new sys-time/op delta Build-16 5.87s ± 5% 5.82s ± 5% ~ (p=0.182 n=10+9) It doesn't seem like much, but remember that these stats are for the entire set of processes, where garble only accounts for about 10% of the total wall time when compared to the compiler or linker. So a ~0.1% decrease globally is still significant. linkerVariableStrings is also indexed by *types.Var rather than types.Object, since -ldflags=-X only supports setting the string value of variables. This shouldn't make a significant difference in terms of allocs, but at least the map is less prone to confusion with other object types. To ensure the new code doesn't trip up on non-variables, we add test cases. Finally, for the sake of clarity, index into the types.Info maps like Defs and Uses rather than calling ObjectOf if we know whether the identifier we have is a definition of a name or the use of a defined name. This isn't better in terms of performance, as ObjectOf is a tiny method, but just like with linkerVariableStrings before, the new code is clearer.
2 years ago
type someType int
const someConst = "untouchable"
func someFunc() string { return "untouchable" }
func main() {
fmt.Printf("version: %q\n", unexportedVersion)
fmt.Printf("becomes empty: %q\n", replacedWithEmpty)
fmt.Printf("becomes string with spaces: %q\n", replacedWithSpaces)
fmt.Printf("should be kept: %q, %q\n", notReplacedBefore, notReplacedAfter)
fmt.Printf("no longer unset: %q\n", imported.ExportedUnset)
}
-- imported/imported.go --
package imported
var (
ExportedUnset, AnotherUnset string
otherVar int
)
-- main.stdout --
version: "v1.22.33"
becomes empty: ""
becomes string with spaces: " foo bar "
should be kept: "kept_before", "kept_after"
no longer unset: "garble_replaced"