Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: a goose on PATH is told apart from a Go program without parsing its symbol table

yetone7e340036a71b
diff --git a/internal/agent/agent.go b/internal/agent/agent.go
index 66ca4edfd..384b064d3 100644
--- a/internal/agent/agent.go
+++ b/internal/agent/agent.go
@@ -3,8 +3,10 @@
package agent
import (
+ "bytes"
"debug/buildinfo"
"fmt"
+ "io"
"os"
"path/filepath"
"runtime"
@@ -441,12 +443,48 @@ func goProgram(bin string) bool {
if v, ok := goPrograms.m[key]; ok {
return v
}
- _, err = buildinfo.ReadFile(bin)
+ is := hasGoMagic(bin)
+ if is {
+ _, err = buildinfo.ReadFile(bin)
+ is = err == nil
+ }
if goPrograms.m == nil {
goPrograms.m = map[goProgramKey]bool{}
}
- goPrograms.m[key] = err == nil
- return err == nil
+ goPrograms.m[key] = is
+ return is
+}
+
+// goBuildInfoMagic begins a Go program's build info, the bytes
+// debug/buildinfo looks for.
+var goBuildInfoMagic = []byte("\xff Go buildinf:")
+
+// hasGoMagic reports whether bin holds Go's build-info magic anywhere,
+// read through a small buffer. buildinfo.ReadFile parses the binary's
+// whole symbol and string tables first: for Block's goose, a 260 MB Rust
+// program, that was 200 MB allocated on the first state the window asked
+// for, which the heap kept as room long after. A file without the magic
+// is no Go program; one with it is still asked of buildinfo.
+func hasGoMagic(bin string) bool {
+ f, err := os.Open(bin)
+ if err != nil {
+ return false
+ }
+ defer f.Close()
+ n := len(goBuildInfoMagic)
+ buf := make([]byte, 256<<10)
+ kept := 0 // the last n-1 bytes of the read before, for a magic across two reads
+ for {
+ m, err := io.ReadFull(f, buf[kept:])
+ if bytes.Contains(buf[:kept+m], goBuildInfoMagic) {
+ return true
+ }
+ if err != nil {
+ return false
+ }
+ kept = n - 1
+ copy(buf, buf[len(buf)-kept:])
+ }
}
type goProgramKey struct {
diff --git a/internal/agent/goprogram_test.go b/internal/agent/goprogram_test.go
new file mode 100644
index 000000000..459323424
--- /dev/null
+++ b/internal/agent/goprogram_test.go
@@ -0,0 +1,69 @@
+package agent
+
+import (
+ "encoding/binary"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+// Telling a goose on PATH apart from a Go program reads it through a small
+// buffer: buildinfo.ReadFile parsed a binary's whole symbol and string
+// tables first, 200 MB allocated for Block's 260 MB goose on the first
+// state the window asked for (procmem profile, #2515).
+func TestGoProgramReadsNoSymbolTable(t *testing.T) {
+ const strsize, nsyms = 32 << 20, 512 << 10
+ bin := filepath.Join(t.TempDir(), "goose")
+ // a Mach-O whose LC_SYMTAB names a 32 MB string table and 8 MB of
+ // symbols, all zeros: a large program that isn't Go
+ var hdr []byte
+ le := binary.LittleEndian
+ for _, v := range []uint32{0xfeedfacf, 0x0100000c, 0, 2, 1, 24, 0, 0, 0x2, 24, 4096, nsyms, 4096 + nsyms*16, strsize} {
+ hdr = le.AppendUint32(hdr, v)
+ }
+ f, err := os.Create(bin)
+ if err != nil {
+ t.Fatal(err)
+ }
+ f.Write(hdr)
+ f.Truncate(4096 + nsyms*16 + strsize)
+ f.Close()
+
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ is := goProgram(bin)
+ runtime.ReadMemStats(&after)
+ if is {
+ t.Fatal("a Mach-O without Go's build info taken for a Go program")
+ }
+ if got := after.TotalAlloc - before.TotalAlloc; got > 4<<20 {
+ t.Fatalf("goProgram allocated %d MB for a 40 MB binary that isn't Go", got>>20)
+ }
+}
+
+// The magic is found across two reads of the buffer, and a Go program is
+// still one (TestGooseDetectSkipsGoGoose builds a real one).
+func TestHasGoMagicAcrossReads(t *testing.T) {
+ dir := t.TempDir()
+ for _, at := range []int{0, 256<<10 - 5, 256<<10 - len(goBuildInfoMagic), 256 << 10, 600 << 10} {
+ b := make([]byte, 700<<10)
+ copy(b[at:], goBuildInfoMagic)
+ p := filepath.Join(dir, "bin")
+ if err := os.WriteFile(p, b, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if !hasGoMagic(p) {
+ t.Errorf("magic at %d not found", at)
+ }
+ }
+ p := filepath.Join(dir, "none")
+ os.WriteFile(p, make([]byte, 700<<10), 0o755)
+ if hasGoMagic(p) {
+ t.Error("magic found in zeros")
+ }
+ self, err := os.Executable()
+ if err == nil && !goProgram(self) {
+ t.Error("this test binary, a Go program, not taken for one")
+ }
+}