Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-literals can still cause unused import errors #658

Closed
ghost-ng opened this issue Jan 27, 2023 · 6 comments · Fixed by #663
Closed

-literals can still cause unused import errors #658

ghost-ng opened this issue Jan 27, 2023 · 6 comments · Fixed by #663

Comments

@ghost-ng
Copy link

ghost-ng commented Jan 27, 2023

What version of Garble and Go are you using?

:5: imported and not used: "gCMQFPPui" as time
exit status 2
exit status 2

go version
go version go1.19.5 linux/amd64

garble version
mvdan.cc/garble v0.9.1

Build settings:
-compiler gc
CGO_ENABLED 1
GOARCH amd64
GOOS linux
GOAMD64 v1

Even stranger, when i run garble on windows, it works without error but much older garble version

garble version
mvdan.cc/garble v0.7.2

Build settings:
-compiler gc
CGO_ENABLED 1
GOARCH amd64
GOOS windows
GOAMD64 v1

What environment are you running Garble on?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/name/.cache/go-build"
GOENV="/home/name/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/name/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/name/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="/usr/bin/gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK="/opt/name/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3167790354=/tmp/go-build -gno-record-gcc-switches"

What did you do?

env GOOS=windows GOARCH=amd64 garble -tiny -literals -seed=random build -buildmode=pie -trimpath -ldflags="-w -s"

What did you expect to see?

No error

What did you see instead?

:5: imported and not used: "gCMQFPPui" as time
exit status 2
exit status 2

Notably, the compile works without the -literals flag. I wish I could give you better info, I just dont know what else to give. Code base is custom but i can share more if you need it.

@mvdan
Copy link
Member

mvdan commented Jan 27, 2023

Can you provide a way for us to reproduce the bug? If you can't, see #595 as well.

@ghost-ng
Copy link
Author

trying to find you a good excert/way to replicate, in the meantime, it looks like the latest update is what caused the issue.

Everything works with 0.9.0

go install mvdan.cc/garble@v0.9.0

garble version
mvdan.cc/garble v0.9.0

Build settings:
-compiler gc
CGO_ENABLED 1
GOARCH amd64
GOOS linux
GOAMD64 v1

@mvdan
Copy link
Member

mvdan commented Jan 28, 2023

@pagran does this ring a bell? you did make a change to -literals with imports, so maybe we missed something.

@pagran
Copy link
Member

pagran commented Jan 28, 2023

In process, I have one hypothesis

@pagran
Copy link
Member

pagran commented Jan 28, 2023

1:

package main

import (
	"time"
	. "time"
)

const Test = ANSIC

func main() {
	println(time.ANSIC)
	println(Test)
}

2:

package main

import (
	"time"
	t2 "time"
)

const Test = t2.ANSIC

func main() {
	println(time.ANSIC)
	println(Test)
}

@ghost-ng
Copy link
Author

I can confirm, both tests successfully reproduced the error

@mvdan mvdan changed the title -literals issues with string replacements -literals can still cause unused import errors Jan 31, 2023
mvdan pushed a commit that referenced this issue Jan 31, 2023
garble's -literals flag and its patching of the runtime may leave unused imports.
We used to try to detect those and remove the imports,
but that was still buggy with edge cases like dot imports or renamed imports.

Moreover, it was potentially incorrect.
Completely removing an import from a package means we don't run its init funcs,
which could have side effects changing the behavior of a program.
As an example, database/sql drivers are registered at init time.

Instead, for each import in an obfuscated Go file,
add an unnamed declaration which references the imported package.
This may not be necessary for all imported packages,
as only a minority become unused due to garble,
but it's also relatively harmless to do so.

Fixes #658.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants