Colin Cross | 36f55aa | 2022-03-21 18:46:41 -0700 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
Colin Cross | 6285c65 | 2022-04-05 18:06:15 -0700 | [diff] [blame^] | 19 | "debug/elf" |
Colin Cross | 36f55aa | 2022-03-21 18:46:41 -0700 | [diff] [blame] | 20 | "encoding/binary" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
Colin Cross | 6285c65 | 2022-04-05 18:06:15 -0700 | [diff] [blame^] | 25 | func Test_elfIdentifierFromReaderAt_BadElfFile(t *testing.T) { |
| 26 | tests := []struct { |
| 27 | name string |
| 28 | contents string |
| 29 | }{ |
| 30 | { |
| 31 | name: "empty", |
| 32 | contents: "", |
| 33 | }, |
| 34 | { |
| 35 | name: "text", |
| 36 | contents: "#!/bin/bash\necho foobar", |
| 37 | }, |
| 38 | { |
| 39 | name: "empty elf", |
| 40 | contents: emptyElfFile(), |
| 41 | }, |
| 42 | } |
| 43 | |
| 44 | for _, tt := range tests { |
| 45 | t.Run(tt.name, func(t *testing.T) { |
| 46 | buf := bytes.NewReader([]byte(tt.contents)) |
| 47 | _, err := elfIdentifierFromReaderAt(buf, "<>", false) |
| 48 | if err == nil { |
| 49 | t.Errorf("expected error reading bad elf file without allowMissing") |
| 50 | } |
| 51 | _, err = elfIdentifierFromReaderAt(buf, "<>", true) |
| 52 | if err != nil { |
| 53 | t.Errorf("expected no error reading bad elf file with allowMissing, got %q", err.Error()) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
Colin Cross | 36f55aa | 2022-03-21 18:46:41 -0700 | [diff] [blame] | 59 | func Test_readNote(t *testing.T) { |
| 60 | note := []byte{ |
| 61 | 0x04, 0x00, 0x00, 0x00, |
| 62 | 0x10, 0x00, 0x00, 0x00, |
| 63 | 0x03, 0x00, 0x00, 0x00, |
| 64 | 0x47, 0x4e, 0x55, 0x00, |
| 65 | 0xca, 0xaf, 0x44, 0xd2, 0x82, 0x78, 0x68, 0xfe, 0xc0, 0x90, 0xa3, 0x43, 0x85, 0x36, 0x6c, 0xc7, |
| 66 | } |
| 67 | |
| 68 | descs, err := readNote(bytes.NewBuffer(note), binary.LittleEndian) |
| 69 | if err != nil { |
| 70 | t.Fatalf("unexpected error in readNote: %s", err) |
| 71 | } |
| 72 | |
| 73 | expectedDescs := map[string][]byte{ |
| 74 | "GNU\x00": []byte{0xca, 0xaf, 0x44, 0xd2, 0x82, 0x78, 0x68, 0xfe, 0xc0, 0x90, 0xa3, 0x43, 0x85, 0x36, 0x6c, 0xc7}, |
| 75 | } |
| 76 | |
| 77 | if !reflect.DeepEqual(descs, expectedDescs) { |
| 78 | t.Errorf("incorrect return, want %#v got %#v", expectedDescs, descs) |
| 79 | } |
| 80 | } |
Colin Cross | 6285c65 | 2022-04-05 18:06:15 -0700 | [diff] [blame^] | 81 | |
| 82 | // emptyElfFile returns an elf file header with no program headers or sections. |
| 83 | func emptyElfFile() string { |
| 84 | ident := [elf.EI_NIDENT]byte{} |
| 85 | identBuf := bytes.NewBuffer(ident[0:0:elf.EI_NIDENT]) |
| 86 | binary.Write(identBuf, binary.LittleEndian, []byte("\x7fELF")) |
| 87 | binary.Write(identBuf, binary.LittleEndian, elf.ELFCLASS64) |
| 88 | binary.Write(identBuf, binary.LittleEndian, elf.ELFDATA2LSB) |
| 89 | binary.Write(identBuf, binary.LittleEndian, elf.EV_CURRENT) |
| 90 | binary.Write(identBuf, binary.LittleEndian, elf.ELFOSABI_LINUX) |
| 91 | binary.Write(identBuf, binary.LittleEndian, make([]byte, 8)) |
| 92 | |
| 93 | header := elf.Header64{ |
| 94 | Ident: ident, |
| 95 | Type: uint16(elf.ET_EXEC), |
| 96 | Machine: uint16(elf.EM_X86_64), |
| 97 | Version: uint32(elf.EV_CURRENT), |
| 98 | Entry: 0, |
| 99 | Phoff: uint64(binary.Size(elf.Header64{})), |
| 100 | Shoff: uint64(binary.Size(elf.Header64{})), |
| 101 | Flags: 0, |
| 102 | Ehsize: uint16(binary.Size(elf.Header64{})), |
| 103 | Phentsize: 0x38, |
| 104 | Phnum: 0, |
| 105 | Shentsize: 0x40, |
| 106 | Shnum: 0, |
| 107 | Shstrndx: 0, |
| 108 | } |
| 109 | |
| 110 | buf := &bytes.Buffer{} |
| 111 | binary.Write(buf, binary.LittleEndian, header) |
| 112 | return buf.String() |
| 113 | } |