obfuscate alias names like any other objects

Before this change, we would try to never obfuscate alias names. That
was far from ideal, as they can end up in field names via anonymous
fields.

Even then, we would sometimes still fail to build, because we would
inconsistently obfuscate alias names. For example, in the added test
case:

	--- FAIL: TestScripts/syntax (0.23s)
	    testscript.go:397:
	        > env GOPRIVATE='test/main,private.source'
	        > garble build
	        [stderr]
	        # test/main/sub
	        Lv_a8gRD.go:15: undefined: KCvSpxmQ

To fix this problem, we set obj to be the TypeName corresponding to the
alias when it is used as an embedded field. We can then make the right
choice when obfuscating the name.

Right now, all aliases will be obfuscated. A TODO exists about not
obfuscating alias names when they're used as embedded fields in a struct
type in the same package, and that package is used for reflection -
since then, the alias name ends up as the field name.

With these changes, the protobuf module now builds.
pull/340/head
Daniel Martí 3 years ago committed by lu4p
parent 68f07389b2
commit 65ff07875b

@ -1167,13 +1167,26 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
}
pkg := obj.Pkg()
if vr, ok := obj.(*types.Var); ok && vr.Embedded() {
// ObjectOf returns the field for embedded struct
// fields, not the type it uses. Use the type.
named := namedType(obj.Type())
if named == nil {
return true // unnamed type (probably a basic type, e.g. int)
// The docs for ObjectOf say:
//
// If id is an embedded struct field, ObjectOf returns the
// field (*Var) it defines, not the type (*TypeName) it uses.
//
// If this embedded field is a type alias, we want to
// handle that instead of treating it as the type the
// alias points to.
//
// Alternatively, if we don't have an alias, we want to
// use the embedded type, not the field.
if tname, ok := tf.info.Uses[node].(*types.TypeName); ok && tname.IsAlias() {
obj = tname
} else {
named := namedType(obj.Type())
if named == nil {
return true // unnamed type (probably a basic type, e.g. int)
}
obj = named.Obj()
}
obj = named.Obj()
pkg = obj.Pkg()
}
if pkg == nil {
@ -1268,14 +1281,6 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
return true
}
// TODO: we need to obfuscate aliases too.
// When used as anonymous struct fields,
// their names end up in the binary as a field name.
// For now, skip them, as they are hard to obfuscate.
if obj.IsAlias() {
return true
}
// If the type was not obfuscated in the package were it was defined,
// do not obfuscate it here.
if path != curPkg.ImportPath {

@ -4,9 +4,7 @@ garble build
exec ./main
cmp stdout main.stdout
# TODO: also check for these once we obfuscate aliases:
# 'AliasIndirectNamedWithReflect' 'AliasIndirectNamedWithoutReflect'
! binsubstr main$exe 'main.go' 'test/main' 'importedpkg.' 'DownstreamObfuscated' 'SiblingObfuscated' 'IndirectObfuscated' 'IndirectNamedWithoutReflect'
! binsubstr main$exe 'main.go' 'test/main' 'importedpkg.' 'DownstreamObfuscated' 'SiblingObfuscated' 'IndirectObfuscated' 'IndirectNamedWithoutReflect' 'AliasIndirectNamedWithReflect' 'AliasIndirectNamedWithoutReflect'
binsubstr main$exe 'ReflectInDefined' 'ExportedField2' 'unexportedField2' 'IndirectUnobfuscated' 'IndirectNamedWithReflect'
[short] stop # no need to verify this with -short
@ -83,12 +81,15 @@ func main() {
SiblingObfuscated: "sibling",
})
// Using type aliases as both regular fields, and embedded fields.
var emb EmbeddingIndirect
emb.With.IndirectUnobfuscated = "indirect-with"
emb.Without.IndirectObfuscated = "indirect-without"
fmt.Printf("%v\n", emb)
printfWithoutPackage("%#v\n", emb.With)
// TODO: don't obfuscate the embedded field name here
// printfWithoutPackage("%#v\n", importedpkg.ReflectEmbeddingAlias{})
}
type EmbeddingIndirect struct {
@ -184,6 +185,16 @@ type (
AliasIndirectNamedWithoutReflect = indirect.IndirectNamedWithoutReflect
)
var _ = reflect.TypeOf(ReflectEmbeddingAlias{})
type ReflectEmbeddingAlias struct {
ReflectEmbeddedAlias
}
type ReflectEmbeddedAlias = ReflectEmbeddingNamed
type ReflectEmbeddingNamed struct{}
-- importedpkg2/imported2.go --
package importedpkg2

@ -155,6 +155,17 @@ func TestFoo(s string) {}
func TestBar(*struct{}) {}
// If we obfuscate the alias name, we must obfuscate its use here too.
type EmbeddingAlias struct {
EmbeddedAlias
}
type EmbeddedAlias = EmbeddedStruct
type EmbeddedStruct struct {
Foo int
}
-- main.stderr --
nil case
1 1 1

Loading…
Cancel
Save