|
|
|
skip # TODO: get plugins working properly. See issue #87
|
|
|
|
|
|
|
|
[windows] skip 'Go plugins are not supported on Windows'
|
|
|
|
|
deprecate using GOPRIVATE in favor of GOGARBLE (#427)
Piggybacking off of GOPRIVATE is great for a number of reasons:
* People tend to obfuscate private code, whose package paths will
generally be in GOPRIVATE already
* Its meaning and syntax are well understood
* It allows all the flexibility we need without adding our own env var
or config option
However, using GOPRIVATE directly has one main drawback.
It's fairly common to also want to obfuscate public dependencies,
to make the code in private packages even harder to follow.
However, using "GOPRIVATE=*" will result in two main downsides:
* GONOPROXY defaults to GOPRIVATE, so the proxy would be entirely disabled.
Downloading modules, such as when adding or updating dependencies,
or when the local cache is cold, can be less reliable.
* GONOSUMDB defaults to GOPRIVATE, so the sumdb would be entirely disabled.
Adding entries to go.sum, such as when adding or updating dependencies,
can be less secure.
We will continue to consume GOPRIVATE as a fallback,
but we now expect users to set GOGARBLE instead.
The new logic is documented in the README.
While here, rewrite some uses of "private" with "to obfuscate",
to make the code easier to follow and harder to misunderstand.
Fixes #276.
4 years ago
|
|
|
env GOGARBLE=test/main
|
|
|
|
|
|
|
|
garble build -buildmode=plugin ./plugin
|
|
|
|
binsubstr plugin.so 'PublicVar' 'PublicFunc'
|
|
|
|
! binsubstr plugin.so 'privateFunc'
|
|
|
|
|
|
|
|
# Note that we need -trimpath; see the caveat section in the README.
|
|
|
|
go build -trimpath
|
|
|
|
exec ./main
|
|
|
|
cmp stderr main.stderr
|
|
|
|
binsubstr main$exe 'PublicVar' 'PublicFunc'
|
|
|
|
|
|
|
|
[short] stop # no need to verify this with -short
|
|
|
|
|
|
|
|
# This used to fail, since in this case the package path for the ad-hoc plugin
|
|
|
|
# package isn't "main", but "plugin/unnamed-*".
|
|
|
|
garble build -buildmode=plugin plugin/main.go
|
|
|
|
|
|
|
|
go build -buildmode=plugin ./plugin
|
|
|
|
binsubstr plugin.so 'PublicVar' 'PublicFunc' 'privateFunc'
|
|
|
|
go build
|
|
|
|
exec ./main
|
|
|
|
cmp stderr main.stderr
|
|
|
|
|
|
|
|
-- go.mod --
|
|
|
|
module test/main
|
|
|
|
|
|
|
|
go 1.17
|
|
|
|
-- plugin/main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "test/main/plugin/lib"
|
|
|
|
|
|
|
|
var PublicVar int = lib.ImportedFunc()
|
|
|
|
|
|
|
|
func privateFunc(n int) { println("Hello, number", n) }
|
|
|
|
|
|
|
|
func PublicFunc() { privateFunc(PublicVar) }
|
|
|
|
-- plugin/lib/lib.go --
|
|
|
|
package lib
|
|
|
|
|
|
|
|
func ImportedFunc() int { return 4 }
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "plugin"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
p, err := plugin.Open("plugin.so")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
v, err := p.Lookup("PublicVar")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
f, err := p.Lookup("PublicFunc")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
*v.(*int) = 7
|
|
|
|
f.(func())()
|
|
|
|
}
|
|
|
|
-- main.stderr --
|
|
|
|
Hello, number 7
|