burrowers / garble

Obfuscate Go builds
BSD 3-Clause "New" or "Revised" License
3.93k stars 248 forks source link

Invalid syntax when using garble to build #812

Closed coremedic closed 9 months ago

coremedic commented 10 months ago

What version of Garble and Go are you using?

$ garble version
mvdan.cc/garble v0.10.1

Build settings:
      -buildmode exe
       -compiler gc
  DefaultGODEBUG panicnil=1
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

$ go version
go version go1.21.4 linux/amd64

What environment are you running Garble on?

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

What did you do?

I have some go assembly code that I am trying to obfuscate, getting an "invalid syntax" error when building. When I build with "go build" my build works fine. Here are some relevant parts of the debug output:

# badger/asm
[garble] transforming asm with args: -p badger/asm -trimpath asm=>badger/asm;/tmp/go-build3214285344/b050=> -I $WORK/b050/ -I /usr/local/go/pkg/include -D GOOS_windows -D GOARCH_amd64 -D GOAMD64_v1 -gensymabis -o $WORK/b050/symabis asm/parser_asm_x64_windows.s asm/syscall_asm_x64_windows.s asm/trampoline_asm_x64_windows.s asm/util_asm_x64_windows.s
[garble] shared cache loaded in 860µs from /tmp/garble-shared2640033168/main-cache.gob
[garble] import path "badger/asm" hashed with 26a2a52d7fc9accf418cde02e0135913eac9c1e8ee188f0514e27cf7af84ec56 to "gsPjuhBO"
invalid syntax

[...]

[garble] transformed args for compile in 4.36ms: -o $WORK/b002/_pkg_.a -trimpath /tmp/garble-shared2640033168=>;/home/ben/xShell/badger/cmd/whoami=>badger/cmd/whoami;/tmp/go-build3214285344/b002=> -p vHMvj_Nzn -lang=go1.21 -complete -buildid MmSW_M4DMY_TAYsT-AKF/MmSW_M4DMY_TAYsT-AKF -goversion go1.21.4 -c=4 -nolocalimports -importcfg /tmp/garble-shared2640033168/importcfg3258481374 -pack -dwarf=false /tmp/garble-shared2640033168/vHMvj_Nzn/whoami.go /tmp/garble-shared2640033168/vHMvj_Nzn/whoami_windows.go
exit status 1
coremedic commented 10 months ago

Resolved my own issue. I had a comment on the same line as my #include

lu4p commented 10 months ago

Still if it builds with go and not garble we should fix it. Can you send in your code so we can reproduce?

coremedic commented 9 months ago

Sorry for the late response, I cant provide the whole context but here is how you could recreate the issue:

Assembly in go program

#include "textflag.h" // THIS COMMENT BREAKS GARBLE

// func ReadByteAtOffset(start uintptr, offset uint32) uint8
TEXT ·readByteAtOffset(SB), NOSPLIT, $0-24
    MOVQ start+0(FP), AX       // Load start into AX
    MOVL offset+8(FP), CX      // Load offset into CX
    ADDQ CX, AX                // Add offset to start
    MOVB (AX), CL              // Read byte from the address in AX
    MOVB CL, ret+16(FP)        // Store the byte in return value
    RET

Comments on the same line as #include will cause an invalid syntax error when building with garble, but is fine with go build

mvdan commented 9 months ago

Thanks @coremedic - that is enough for me to reproduce.