| 1 | package elfdeps |
| 2 | |
| 3 | import ( |
| 4 | "debug/elf" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Test helpers against a known binary in the system: find `true` via LookPath |
| 13 | func TestParseAndResolveTrue(t *testing.T) { |
| 14 | bin, err := exec.LookPath("true") |
| 15 | if err != nil { |
| 16 | t.Fatalf("failed to find 'true' binary: %v", err) |
| 17 | } |
| 18 | |
| 19 | f, err := elf.Open(bin) |
| 20 | if err != nil { |
| 21 | t.Fatalf("failed to open %s: %v", bin, err) |
| 22 | } |
| 23 | defer f.Close() |
| 24 | |
| 25 | interp := parseInterp(f) |
| 26 | if interp == "" { |
| 27 | t.Fatalf("expected interpreter for %s, got empty", bin) |
| 28 | } |
| 29 | |
| 30 | needed, rpaths := parseDynamic(f) |
| 31 | if needed == nil { |
| 32 | needed = []string{} |
| 33 | } |
| 34 | |
| 35 | origin := filepath.Dir(bin) |
| 36 | rpaths = normalizeRpaths(rpaths, origin) |
| 37 | paths := resolveSonames(needed, rpaths, f.Class, f.Machine) |
| 38 | if paths == nil { |
| 39 | paths = []string{} |
| 40 | } |
| 41 | |
| 42 | // Ensure interpreter path exists on filesystem |
| 43 | if _, err := os.Stat(interp); err != nil { |
| 44 | t.Fatalf("interp path %s does not exist: %v", interp, err) |
| 45 | } |
| 46 | |
| 47 | // If there are resolved library paths, they must exist and match the |
| 48 | // binary's architecture (not e.g. an x32 libc for an x86-64 binary). |
| 49 | for _, p := range paths { |
| 50 | if _, err := os.Stat(p); err != nil { |
| 51 | t.Fatalf("resolved library path %s does not exist: %v", p, err) |
| 52 | } |
| 53 | lib, err := elf.Open(p) |
| 54 | if err != nil { |
| 55 | continue |
| 56 | } |
| 57 | if lib.Class != f.Class || lib.Machine != f.Machine { |
| 58 | lib.Close() |
| 59 | t.Fatalf("resolved %s has class/machine %v/%v, want %v/%v", |
| 60 | p, lib.Class, lib.Machine, f.Class, f.Machine) |
| 61 | } |
| 62 | lib.Close() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestRecursiveDependencies(t *testing.T) { |
| 67 | if _, err := exec.LookPath("gcc"); err != nil { |
| 68 | t.Skip("gcc not found, skipping test") |
| 69 | } |
| 70 | // Create a temporary directory for compiled artifacts |
| 71 | tempDir := t.TempDir() |
| 72 | |
| 73 | // Compile liba.so |
| 74 | libaSrc := "testdata/liba.c" |
| 75 | libaSo := filepath.Join(tempDir, "liba.so") |
| 76 | cmd := exec.Command("gcc", "-fPIC", "-shared", "-o", libaSo, libaSrc) |
| 77 | if out, err := cmd.CombinedOutput(); err != nil { |
| 78 | t.Fatalf("failed to compile liba.so: %v\n%s", err, string(out)) |
| 79 | } |
| 80 | |
| 81 | // Compile libb.so |
| 82 | libbSrc := "testdata/libb.c" |
| 83 | libbSo := filepath.Join(tempDir, "libb.so") |
| 84 | cmd = exec.Command("gcc", "-fPIC", "-shared", "-o", libbSo, libbSrc, "-L"+tempDir, "-la", "-Wl,-rpath,$ORIGIN") |
| 85 | if out, err := cmd.CombinedOutput(); err != nil { |
| 86 | t.Fatalf("failed to compile libb.so: %v\n%s", err, string(out)) |
| 87 | } |
| 88 | |
| 89 | // Compile test_binary |
| 90 | mainSrc := "testdata/main.c" |
| 91 | testBin := filepath.Join(tempDir, "test_binary") |
| 92 | cmd = exec.Command("gcc", "-o", testBin, mainSrc, "-L"+tempDir, "-lb", "-Wl,-rpath,$ORIGIN") |
| 93 | if out, err := cmd.CombinedOutput(); err != nil { |
| 94 | t.Fatalf("failed to compile test_binary: %v\n%s", err, string(out)) |
| 95 | } |
| 96 | |
| 97 | // Run the actual test logic |
| 98 | deps, err := GetLibraryDependencies(testBin) |
| 99 | if err != nil { |
| 100 | t.Fatalf("GetLibraryDependencies failed: %v", err) |
| 101 | } |
| 102 | |
| 103 | foundA := false |
| 104 | foundB := false |
| 105 | for _, dep := range deps { |
| 106 | if dep == libaSo { |
| 107 | foundA = true |
| 108 | } |
| 109 | if dep == libbSo { |
| 110 | foundB = true |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if !foundA { |
| 115 | t.Errorf("expected to find %s in dependency list, but didn't. Found: %v", libaSo, deps) |
| 116 | } |
| 117 | if !foundB { |
| 118 | t.Errorf("expected to find %s in dependency list, but didn't. Found: %v", libbSo, deps) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestGetLibraryDependencies(t *testing.T) { |
| 123 | bin, err := exec.LookPath("true") |
| 124 | if err != nil { |
| 125 | t.Fatalf("failed to find 'true' binary: %v", err) |
| 126 | } |
| 127 | f, err := elf.Open(bin) |
| 128 | if err != nil { |
| 129 | t.Fatalf("failed to open %s: %v", bin, err) |
| 130 | } |
| 131 | class, machine := f.Class, f.Machine |
| 132 | f.Close() |
| 133 | |
| 134 | paths, err := GetLibraryDependencies(bin) |
| 135 | if err != nil { |
| 136 | t.Fatalf("GetLibraryDependencies failed: %v", err) |
| 137 | } |
| 138 | if len(paths) == 0 { |
| 139 | t.Fatalf("expected non-empty dependency list for %s", bin) |
| 140 | } |
| 141 | // ensure returned paths are absolute, exist, and match the binary arch |
| 142 | for _, p := range paths { |
| 143 | if !filepath.IsAbs(p) { |
| 144 | t.Fatalf("expected absolute path, got %s", p) |
| 145 | } |
| 146 | if _, err := os.Stat(p); err != nil { |
| 147 | t.Fatalf("path %s does not exist: %v", p, err) |
| 148 | } |
| 149 | if strings.Contains(p, "libx32") && class == elf.ELFCLASS64 { |
| 150 | t.Fatalf("x86-64 binary resolved to x32 path %s", p) |
| 151 | } |
| 152 | lib, err := elf.Open(p) |
| 153 | if err != nil { |
| 154 | continue // e.g. ld.so.cache |
| 155 | } |
| 156 | mismatched := lib.Class != class || lib.Machine != machine |
| 157 | lib.Close() |
| 158 | if mismatched { |
| 159 | t.Fatalf("dependency %s does not match binary arch", p) |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestGetLdmapWithFakeOutput(t *testing.T) { |
| 165 | // fake ldconfig output with a single mapping |
| 166 | original := ldconfigRunner |
| 167 | defer func() { ldconfigRunner = original }() |
| 168 | |
| 169 | // create a fake file on disk to satisfy os.Stat checks in getLdmap |
| 170 | tmpDir := t.TempDir() |
| 171 | tmp := filepath.Join(tmpDir, "libfake.so") |
| 172 | f, err := os.Create(tmp) |
| 173 | if err != nil { |
| 174 | t.Fatalf("failed to create tmp file: %v", err) |
| 175 | } |
| 176 | f.Close() |
| 177 | |
| 178 | // Because getLdmap checks the path exists, return tmp in the fake output |
| 179 | ldconfigRunner = func() ([]byte, error) { |
| 180 | return []byte("libfake.so (libc6,x86-64) => " + tmp + "\n"), nil |
| 181 | } |
| 182 | |
| 183 | m := getLdmap("x86-64") |
| 184 | if got, ok := m["libfake.so"]; !ok { |
| 185 | t.Fatalf("expected libfake.so in map") |
| 186 | } else if got != tmp { |
| 187 | t.Fatalf("expected path %s, got %s", tmp, got) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestGetLdmapPrefersMatchingArch(t *testing.T) { |
| 192 | original := ldconfigRunner |
| 193 | defer func() { ldconfigRunner = original }() |
| 194 | |
| 195 | tmpDir := t.TempDir() |
| 196 | x32Path := filepath.Join(tmpDir, "libc-x32.so.6") |
| 197 | x64Path := filepath.Join(tmpDir, "libc-x64.so.6") |
| 198 | for _, p := range []string{x32Path, x64Path} { |
| 199 | f, err := os.Create(p) |
| 200 | if err != nil { |
| 201 | t.Fatalf("failed to create %s: %v", p, err) |
| 202 | } |
| 203 | f.Close() |
| 204 | } |
| 205 | |
| 206 | // x32 listed first — the old bug picked this for every arch. |
| 207 | ldconfigRunner = func() ([]byte, error) { |
| 208 | return []byte( |
| 209 | "libc.so.6 (libc6,x32) => " + x32Path + "\n" + |
| 210 | "libc.so.6 (libc6,x86-64) => " + x64Path + "\n", |
| 211 | ), nil |
| 212 | } |
| 213 | |
| 214 | m := getLdmap("x86-64") |
| 215 | if got := m["libc.so.6"]; got != x64Path { |
| 216 | t.Fatalf("expected x86-64 libc %s, got %s", x64Path, got) |
| 217 | } |
| 218 | |
| 219 | m = getLdmap("x32") |
| 220 | if got := m["libc.so.6"]; got != x32Path { |
| 221 | t.Fatalf("expected x32 libc %s, got %s", x32Path, got) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func TestResolveSonamesUsesLdmapFallback(t *testing.T) { |
| 226 | original := ldconfigRunner |
| 227 | defer func() { ldconfigRunner = original }() |
| 228 | |
| 229 | tmpDir := t.TempDir() |
| 230 | tmp := filepath.Join(tmpDir, "libfake2.so") |
| 231 | f, err := os.Create(tmp) |
| 232 | if err != nil { |
| 233 | t.Fatalf("failed to create tmp file: %v", err) |
| 234 | } |
| 235 | f.Close() |
| 236 | |
| 237 | ldconfigRunner = func() ([]byte, error) { |
| 238 | return []byte("libfake2.so (libc6,x86-64) => " + tmp + "\n"), nil |
| 239 | } |
| 240 | |
| 241 | // needed contains a soname that won't be found in rpaths or std dirs |
| 242 | rpaths := normalizeRpaths([]string{}, tmpDir) |
| 243 | out := resolveSonames([]string{"libfake2.so"}, rpaths, elf.ELFCLASS64, elf.EM_X86_64) |
| 244 | if len(out) != 1 { |
| 245 | t.Fatalf("expected 1 resolved path, got %d", len(out)) |
| 246 | } |
| 247 | if out[0] != tmp { |
| 248 | t.Fatalf("expected %s, got %s", tmp, out[0]) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func TestResolveSonamesOriginExpansion(t *testing.T) { |
| 253 | // Create a temp dir and a lib subdir to simulate $ORIGIN/lib |
| 254 | tmpDir := t.TempDir() |
| 255 | libDir := filepath.Join(tmpDir, "lib") |
| 256 | if err := os.Mkdir(libDir, 0755); err != nil { |
| 257 | t.Fatalf("failed create lib dir: %v", err) |
| 258 | } |
| 259 | |
| 260 | libName := "liborigin.so" |
| 261 | libPath := filepath.Join(libDir, libName) |
| 262 | f, err := os.Create(libPath) |
| 263 | if err != nil { |
| 264 | t.Fatalf("failed to create lib file: %v", err) |
| 265 | } |
| 266 | f.Close() |
| 267 | |
| 268 | // rpath using $ORIGIN should resolve to tmpDir/lib |
| 269 | rpaths1 := normalizeRpaths([]string{"$ORIGIN/lib"}, tmpDir) |
| 270 | out := resolveSonames([]string{libName}, rpaths1, elf.ELFCLASS64, elf.EM_X86_64) |
| 271 | if len(out) != 1 { |
| 272 | t.Fatalf("expected 1 resolved path for $ORIGIN, got %d", len(out)) |
| 273 | } |
| 274 | if out[0] != libPath { |
| 275 | t.Fatalf("expected %s, got %s", libPath, out[0]) |
| 276 | } |
| 277 | |
| 278 | // relative rpath should also resolve against origin |
| 279 | rpaths2 := normalizeRpaths([]string{"lib"}, tmpDir) |
| 280 | out2 := resolveSonames([]string{libName}, rpaths2, elf.ELFCLASS64, elf.EM_X86_64) |
| 281 | if len(out2) != 1 { |
| 282 | t.Fatalf("expected 1 resolved path for relative rpath, got %d", len(out2)) |
| 283 | } |
| 284 | if out2[0] != libPath { |
| 285 | t.Fatalf("expected %s, got %s", libPath, out2[0]) |
| 286 | } |
| 287 | } |