Skip to content

Commit

Permalink
Resolve map initialization order bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemk14ebr committed Aug 17, 2023
1 parent 897d990 commit 944c2b7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions debug/elf/file.go
Expand Up @@ -244,6 +244,7 @@ func (f *File) SectionByType(typ SectionType) *Section {
// NewFile creates a new File for accessing an ELF binary in an underlying reader.
// The ELF binary is expected to start at position 0 in the ReaderAt.
func NewFile(r io.ReaderAt) (*File, error) {

sr := io.NewSectionReader(r, 0, 1<<63-1)
// Read and decode ELF identifier
var ident [16]uint8
Expand All @@ -255,6 +256,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
}

f := new(File)
f.dataAfterSectionCache = make(map[uint64][]byte)
f.Class = Class(ident[EI_CLASS])
switch f.Class {
case ELFCLASS32:
Expand Down Expand Up @@ -565,8 +567,6 @@ func NewFile(r io.ReaderAt) (*File, error) {
return nil, &FormatError{shoff + int64(i*shentsize), "bad section name index", names[i]}
}
}

f.dataAfterSectionCache = make(map[uint64][]byte)
return f, nil
}

Expand Down
2 changes: 1 addition & 1 deletion debug/macho/file.go
Expand Up @@ -231,6 +231,7 @@ func (f *File) Close() error {
// The Mach-O binary is expected to start at position 0 in the ReaderAt.
func NewFile(r io.ReaderAt) (*File, error) {
f := new(File)
f.dataAfterSectionCache = make(map[uint64][]byte)
sr := io.NewSectionReader(r, 0, 1<<63-1)

// Read and decode Mach magic to determine byte order, size.
Expand Down Expand Up @@ -449,7 +450,6 @@ func NewFile(r io.ReaderAt) (*File, error) {
s.ReaderAt = s.sr
}
}
f.dataAfterSectionCache = make(map[uint64][]byte)
return f, nil
}

Expand Down
3 changes: 1 addition & 2 deletions debug/pe/file.go
Expand Up @@ -67,6 +67,7 @@ func (f *File) Close() error {
// NewFile creates a new File for accessing a PE binary in an underlying reader.
func NewFile(r io.ReaderAt) (*File, error) {
f := new(File)
f.dataAfterSectionCache = make(map[uint64][]byte)
sr := io.NewSectionReader(r, 0, 1<<63-1)

var dosheader [96]byte
Expand Down Expand Up @@ -172,8 +173,6 @@ func NewFile(r io.ReaderAt) (*File, error) {
return nil, err
}
}

f.dataAfterSectionCache = make(map[uint64][]byte)
return f, nil
}

Expand Down
14 changes: 14 additions & 0 deletions main_test.go
Expand Up @@ -188,6 +188,20 @@ func TestWeirdBins(t *testing.T) {
t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.")
}
})

// reading the buildid with notes section at the start and alignment of 0 previously caused underflow in offset calculations
t.Run("zero_elf_palignment", func(t *testing.T) {
filePath := fmt.Sprintf("%s/test/weirdbins/%s", workingDirectory, "zero_elf_palignment")
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
t.Errorf("Test file %s doesn't exist\n", filePath)
return
}

_, err := main_impl(filePath, true, true, true, 0, "")
if err == nil {
t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.")
}
})
}

func TestBig(t *testing.T) {
Expand Down
Binary file modified test/weirdbins.zip
Binary file not shown.

0 comments on commit 944c2b7

Please sign in to comment.