Skip to content
Permalink
Browse files
cmd/compile: make all positions x.go:1:1
This shrinks binaries by about 6%, if you're using -ldflags=-w.

Backtraces should still work, with function names and PCs and arguments,
but all line numbers will display as x.go:1.

This breaks any tests that depend on correct line numbers.
For example, net/http:

--- FAIL: TestWriteHeaderNoCodeCheck_h1hijack (0.00s)
    x.go:1: stderr output = "http: response.WriteHeader on hijacked connection from net/http_test.testWriteHeaderAfterWrite.func1 (x.go:1)"; want "http: response.WriteHeader on hijacked connection from net/http_test.testWriteHeaderAfterWrite.func1 (clientserver_test.go:"
--- FAIL: TestWriteHeaderNoCodeCheck_h1 (0.01s)
    x.go:1: stderr output = "http: superfluous response.WriteHeader call from net/http_test.testWriteHeaderAfterWrite.func1 (x.go:1)"; want "http: superfluous response.WriteHeader call from net/http_test.testWriteHeaderAfterWrite.func1 (clientserver_test.go:"
FAIL
FAIL	net/http	13.940s

Remove an invariant check in dwinl.go that's broken by this.
See golang#38698.
That may break debugging. But if you care enough about binary
size to use this, you're already stripping binaries with -ldflags=-w.

Remove a security check around cgo pragma placements.
Unfortunate, but if you're using a hacked toolchain,
you should always make sure your code still builds and passes tests
with a regular toolchain.

Change-Id: I56c7540ea856a3bdccf8d44b5c3dbd4803545015
  • Loading branch information
josharian committed Apr 30, 2020
1 parent 9d812cf commit 1a3e66ceed9bbdb944fa30d8a486cea1e345d4cd
Showing 3 changed files with 2 additions and 10 deletions.
@@ -221,9 +221,6 @@ func makePreinlineDclMap(fnsym *obj.LSym) map[varPos]int {
DeclLine: pos.RelLine(),
DeclCol: pos.Col(),
}
if _, found := m[vp]; found {
Fatalf("child dcl collision on symbol %s within %v\n", n.Sym.Name, fnsym.Name)
}
m[vp] = i
}
return m
@@ -1584,12 +1584,6 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
}
fallthrough
case strings.HasPrefix(text, "go:cgo_"):
// For security, we disallow //go:cgo_* directives other
// than cgo_import_dynamic outside cgo-generated files.
// Exception: they are allowed in the standard library, for runtime and syscall.
if !isCgoGeneratedFile(pos) && !compiling_std {
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
}
p.pragcgo(pos, text)
fallthrough // because of //go:cgo_unsafe_args
default:
@@ -21,7 +21,7 @@ type Pos struct {
}

// MakePos returns a new Pos for the given PosBase, line and column.
func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, sat32(line), sat32(col)} }
func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, 1, 1} }

// TODO(gri) IsKnown makes an assumption about linebase < 1.
// Maybe we should check for Base() != nil instead.
@@ -99,6 +99,7 @@ type PosBase struct {
// A file PosBase's position is relative to itself, with the
// position being filename:1:1.
func NewFileBase(filename string) *PosBase {
filename = "x.go"
base := &PosBase{MakePos(nil, linebase, colbase), filename, linebase, colbase}
base.pos.base = base
return base

0 comments on commit 1a3e66c

Please sign in to comment.