Skip to content

Commit

Permalink
[release-branch.go1.15] cmd/internal/goobj2: fix buglet in object fil…
Browse files Browse the repository at this point in the history
…e reader

The code in the Go object file reader was casting a pointer to mmaped
memory into a large array prior to performing a read of the
relocations section:

	return (*[1<<20]Reloc)(unsafe.Pointer(&r.b[off]))[:n:n]

For very large object files, this artificial array isn't large enough
(that is, there are more than 1048576 relocs to read), so update the
code to use a larger artifical array size.

Fixes #43214.
Updates #41621.

Change-Id: Ic047c8aef4f8a3839f2e7e3594bce652ebd6bd5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/278492
Run-TryBot: Than McIntosh <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Cherry Zhang <[email protected]>
Reviewed-by: Jeremy Faller <[email protected]>
Trust: Than McIntosh <[email protected]>
(cherry picked from commit f4e7a6b)
Reviewed-on: https://go-review.googlesource.com/c/go/+/278673
  • Loading branch information
thanm authored and cagedmantis committed Feb 2, 2021
1 parent aa9b48c commit 58dc445
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/cmd/internal/goobj2/objfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func (a *Aux) Write(w *Writer) { w.Bytes(a[:]) }
// for testing
func (a *Aux) fromBytes(b []byte) { copy(a[:], b) }

// Used to construct an artifically large array type when reading an
// item from the object file relocs section or aux sym section (needs
// to work on 32-bit as well as 64-bit). See issue 41621.
const huge = (1<<31 - 1) / RelocSize

// Referenced symbol name.
//
// Serialized format:
Expand Down Expand Up @@ -652,7 +657,7 @@ func (r *Reader) Reloc(i int, j int) *Reloc {
func (r *Reader) Relocs(i int) []Reloc {
off := r.RelocOff(i, 0)
n := r.NReloc(i)
return (*[1 << 20]Reloc)(unsafe.Pointer(&r.b[off]))[:n:n]
return (*[huge]Reloc)(unsafe.Pointer(&r.b[off]))[:n:n]
}

// NAux returns the number of aux symbols of the i-th symbol.
Expand All @@ -678,7 +683,7 @@ func (r *Reader) Aux(i int, j int) *Aux {
func (r *Reader) Auxs(i int) []Aux {
off := r.AuxOff(i, 0)
n := r.NAux(i)
return (*[1 << 20]Aux)(unsafe.Pointer(&r.b[off]))[:n:n]
return (*[huge]Aux)(unsafe.Pointer(&r.b[off]))[:n:n]
}

// DataOff returns the offset of the i-th symbol's data.
Expand Down

0 comments on commit 58dc445

Please sign in to comment.