| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"go/build" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"path" |
| 11 |
"path/filepath" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/go-quicktest/qt" |
| 15 |
|
| 16 |
"github.com/cilium/ebpf" |
| 17 |
"github.com/cilium/ebpf/internal/testutils" |
| 18 |
"github.com/cilium/ebpf/internal/tracefs" |
| 19 |
"github.com/cilium/ebpf/internal/unix" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
bashEx, _ = OpenExecutable("/bin/bash") |
| 24 |
bashSyms = []string{"main", "_start", "check_dev_tty"} |
| 25 |
bashSym = bashSyms[0] |
| 26 |
) |
| 27 |
|
| 28 |
func TestExecutable(t *testing.T) { |
| 29 |
_, err := OpenExecutable("") |
| 30 |
qt.Assert(t, qt.IsNotNil(err)) |
| 31 |
|
| 32 |
_, err = OpenExecutable("/non/existent/path") |
| 33 |
qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist)) |
| 34 |
|
| 35 |
var pe *os.PathError |
| 36 |
qt.Assert(t, qt.ErrorAs(err, &pe)) |
| 37 |
|
| 38 |
|
| 39 |
dir := t.TempDir() |
| 40 |
path := filepath.Join(dir, "file.txt") |
| 41 |
qt.Assert(t, qt.IsNil(os.WriteFile(path, []byte("hello"), 0600))) |
| 42 |
|
| 43 |
_, err = OpenExecutable(path) |
| 44 |
qt.Assert(t, qt.IsNil(err)) |
| 45 |
|
| 46 |
|
| 47 |
err = os.Chmod(path, 0700) |
| 48 |
qt.Assert(t, qt.IsNil(err)) |
| 49 |
|
| 50 |
_, err = OpenExecutable(path) |
| 51 |
qt.Assert(t, qt.IsNil(err)) |
| 52 |
|
| 53 |
qt.Assert(t, qt.Equals(bashEx.path, "/bin/bash")) |
| 54 |
|
| 55 |
_, err = bashEx.address(bashSym, 0, 0) |
| 56 |
qt.Assert(t, qt.IsNil(err)) |
| 57 |
|
| 58 |
_, err = bashEx.address("bogus", 0, 0) |
| 59 |
qt.Assert(t, qt.IsNotNil(err)) |
| 60 |
} |
| 61 |
|
| 62 |
func TestExecutableOffset(t *testing.T) { |
| 63 |
symbolOffset, err := bashEx.address(bashSym, 0, 0) |
| 64 |
qt.Assert(t, qt.IsNil(err)) |
| 65 |
|
| 66 |
offset, err := bashEx.address(bashSym, 0x1, 0) |
| 67 |
qt.Assert(t, qt.IsNil(err)) |
| 68 |
qt.Assert(t, qt.Equals(offset, 0x1)) |
| 69 |
|
| 70 |
offset, err = bashEx.address(bashSym, 0, 0x2) |
| 71 |
qt.Assert(t, qt.IsNil(err)) |
| 72 |
qt.Assert(t, qt.Equals(offset, symbolOffset+0x2)) |
| 73 |
|
| 74 |
offset, err = bashEx.address(bashSym, 0x1, 0x2) |
| 75 |
qt.Assert(t, qt.IsNil(err)) |
| 76 |
qt.Assert(t, qt.Equals(offset, 0x1+0x2)) |
| 77 |
} |
| 78 |
|
| 79 |
func TestExecutableLazyLoadSymbols(t *testing.T) { |
| 80 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 81 |
|
| 82 |
ex, err := OpenExecutable("/bin/bash") |
| 83 |
qt.Assert(t, qt.IsNil(err)) |
| 84 |
|
| 85 |
qt.Assert(t, qt.HasLen(ex.cachedSymbols, 0)) |
| 86 |
|
| 87 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 88 |
|
| 89 |
|
| 90 |
up, err := ex.Uprobe(bashSym, prog, &UprobeOptions{Address: 124}) |
| 91 |
qt.Assert(t, qt.IsNil(err)) |
| 92 |
up.Close() |
| 93 |
|
| 94 |
|
| 95 |
qt.Assert(t, qt.HasLen(ex.cachedSymbols, 0)) |
| 96 |
|
| 97 |
up, err = ex.Uprobe(bashSym, prog, nil) |
| 98 |
qt.Assert(t, qt.IsNil(err)) |
| 99 |
up.Close() |
| 100 |
|
| 101 |
|
| 102 |
qt.Assert(t, qt.Not(qt.HasLen(ex.cachedSymbols, 0))) |
| 103 |
} |
| 104 |
|
| 105 |
func TestUprobe(t *testing.T) { |
| 106 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 107 |
|
| 108 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 109 |
|
| 110 |
up, err := bashEx.Uprobe(bashSym, prog, nil) |
| 111 |
qt.Assert(t, qt.IsNil(err)) |
| 112 |
defer up.Close() |
| 113 |
|
| 114 |
testLink(t, up, prog) |
| 115 |
} |
| 116 |
|
| 117 |
func TestUprobeInfo(t *testing.T) { |
| 118 |
testutils.SkipOnOldKernel(t, "6.6", "bpf_link_info_perf_event") |
| 119 |
|
| 120 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 121 |
|
| 122 |
up, err := bashEx.Uprobe(bashSym, prog, nil) |
| 123 |
qt.Assert(t, qt.IsNil(err)) |
| 124 |
defer up.Close() |
| 125 |
|
| 126 |
info, err := up.Info() |
| 127 |
qt.Assert(t, qt.IsNil(err)) |
| 128 |
qt.Assert(t, qt.Equals(info.Type, PerfEventType)) |
| 129 |
eventInfo := info.PerfEvent() |
| 130 |
qt.Assert(t, qt.Equals(eventInfo.Type, PerfEventUprobe)) |
| 131 |
uprobeInfo := eventInfo.Uprobe() |
| 132 |
qt.Assert(t, qt.StringContains(uprobeInfo.File, bashEx.path)) |
| 133 |
executable, err := OpenExecutable(uprobeInfo.File) |
| 134 |
qt.Assert(t, qt.IsNil(err)) |
| 135 |
sym, err := executable.Symbol(uint64(uprobeInfo.Offset)) |
| 136 |
qt.Assert(t, qt.IsNil(err)) |
| 137 |
qt.Assert(t, qt.Equals(sym.Symbol, bashSym)) |
| 138 |
qt.Assert(t, qt.Equals(sym.Offset, 0)) |
| 139 |
} |
| 140 |
|
| 141 |
func TestUprobeExtNotFound(t *testing.T) { |
| 142 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 143 |
|
| 144 |
|
| 145 |
_, err := bashEx.Uprobe("open", prog, nil) |
| 146 |
qt.Assert(t, qt.IsNotNil(err)) |
| 147 |
} |
| 148 |
|
| 149 |
func TestUprobeExtWithOpts(t *testing.T) { |
| 150 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 151 |
|
| 152 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
up, err := bashEx.Uprobe("open", prog, &UprobeOptions{ |
| 157 |
|
| 158 |
Address: 0x4, |
| 159 |
}) |
| 160 |
qt.Assert(t, qt.IsNil(err)) |
| 161 |
defer up.Close() |
| 162 |
} |
| 163 |
|
| 164 |
func TestUprobeWithPID(t *testing.T) { |
| 165 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 166 |
|
| 167 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 168 |
|
| 169 |
up, err := bashEx.Uprobe(bashSym, prog, &UprobeOptions{PID: os.Getpid()}) |
| 170 |
qt.Assert(t, qt.IsNil(err)) |
| 171 |
defer up.Close() |
| 172 |
} |
| 173 |
|
| 174 |
func TestUprobeWithNonExistentPID(t *testing.T) { |
| 175 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 176 |
|
| 177 |
|
| 178 |
_, err := bashEx.Uprobe(bashSym, prog, &UprobeOptions{PID: -2}) |
| 179 |
qt.Assert(t, qt.ErrorIs(err, unix.ESRCH)) |
| 180 |
} |
| 181 |
|
| 182 |
func TestUretprobe(t *testing.T) { |
| 183 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 184 |
|
| 185 |
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "") |
| 186 |
|
| 187 |
up, err := bashEx.Uretprobe(bashSym, prog, nil) |
| 188 |
qt.Assert(t, qt.IsNil(err)) |
| 189 |
defer up.Close() |
| 190 |
|
| 191 |
testLink(t, up, prog) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func TestUprobeCreatePMU(t *testing.T) { |
| 196 |
|
| 197 |
testutils.SkipOnOldKernel(t, "4.17", "perf_kprobe PMU") |
| 198 |
|
| 199 |
|
| 200 |
off, err := bashEx.address(bashSym, 0, 0) |
| 201 |
qt.Assert(t, qt.IsNil(err)) |
| 202 |
|
| 203 |
|
| 204 |
args := tracefs.ProbeArgs{ |
| 205 |
Type: tracefs.Uprobe, |
| 206 |
Symbol: bashSym, |
| 207 |
Path: bashEx.path, |
| 208 |
Offset: off, |
| 209 |
Pid: perfAllThreads, |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
pu, err := pmuProbe(args) |
| 214 |
qt.Assert(t, qt.IsNil(err)) |
| 215 |
defer pu.Close() |
| 216 |
|
| 217 |
|
| 218 |
args.Ret = true |
| 219 |
pr, err := pmuProbe(args) |
| 220 |
qt.Assert(t, qt.IsNil(err)) |
| 221 |
defer pr.Close() |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func TestUprobePMUUnavailable(t *testing.T) { |
| 226 |
|
| 227 |
off, err := bashEx.address(bashSym, 0, 0) |
| 228 |
qt.Assert(t, qt.IsNil(err)) |
| 229 |
|
| 230 |
|
| 231 |
args := tracefs.ProbeArgs{ |
| 232 |
Type: tracefs.Uprobe, |
| 233 |
Symbol: bashSym, |
| 234 |
Path: bashEx.path, |
| 235 |
Offset: off, |
| 236 |
Pid: perfAllThreads, |
| 237 |
} |
| 238 |
|
| 239 |
pk, err := pmuProbe(args) |
| 240 |
if err == nil { |
| 241 |
pk.Close() |
| 242 |
t.Skipf("Kernel supports perf_uprobe PMU, not asserting error.") |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
qt.Assert(t, qt.ErrorIs(err, ErrNotSupported), qt.Commentf("got error: %s", err)) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
func TestUprobeTraceFS(t *testing.T) { |
| 251 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 252 |
|
| 253 |
|
| 254 |
off, err := bashEx.address(bashSym, 0, 0) |
| 255 |
qt.Assert(t, qt.IsNil(err)) |
| 256 |
|
| 257 |
|
| 258 |
args := tracefs.ProbeArgs{ |
| 259 |
Type: tracefs.Uprobe, |
| 260 |
Symbol: bashSym, |
| 261 |
Path: bashEx.path, |
| 262 |
Offset: off, |
| 263 |
Pid: perfAllThreads, |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
up, err := tracefsProbe(args) |
| 268 |
qt.Assert(t, qt.IsNil(err)) |
| 269 |
qt.Assert(t, qt.IsNil(up.Close())) |
| 270 |
|
| 271 |
args.Ret = true |
| 272 |
up, err = tracefsProbe(args) |
| 273 |
qt.Assert(t, qt.IsNil(err)) |
| 274 |
qt.Assert(t, qt.IsNil(up.Close())) |
| 275 |
|
| 276 |
|
| 277 |
args.Ret = false |
| 278 |
u1, err := tracefsProbe(args) |
| 279 |
qt.Assert(t, qt.IsNil(err)) |
| 280 |
defer u1.Close() |
| 281 |
qt.Assert(t, qt.IsNotNil(u1.tracefsEvent)) |
| 282 |
|
| 283 |
u2, err := tracefsProbe(args) |
| 284 |
qt.Assert(t, qt.IsNil(err)) |
| 285 |
defer u2.Close() |
| 286 |
qt.Assert(t, qt.IsNotNil(u2.tracefsEvent)) |
| 287 |
|
| 288 |
|
| 289 |
qt.Assert(t, qt.Not(qt.Equals(u1.tracefsEvent.ID(), u2.tracefsEvent.ID()))) |
| 290 |
|
| 291 |
|
| 292 |
args.Group = "/" |
| 293 |
_, err = tracefsProbe(args) |
| 294 |
qt.Assert(t, qt.Not(qt.IsNil(err))) |
| 295 |
|
| 296 |
args.Group = "customgroup" |
| 297 |
u3, err := tracefsProbe(args) |
| 298 |
qt.Assert(t, qt.IsNil(err)) |
| 299 |
defer u3.Close() |
| 300 |
qt.Assert(t, qt.Matches(u3.tracefsEvent.Group(), `customgroup_[a-f0-9]{16}`)) |
| 301 |
} |
| 302 |
|
| 303 |
func TestUprobeProgramCall(t *testing.T) { |
| 304 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 305 |
|
| 306 |
tests := []struct { |
| 307 |
name string |
| 308 |
elf string |
| 309 |
args []string |
| 310 |
sym string |
| 311 |
}{ |
| 312 |
{ |
| 313 |
"bash", |
| 314 |
"/bin/bash", |
| 315 |
[]string{"--help"}, |
| 316 |
"main", |
| 317 |
}, |
| 318 |
{ |
| 319 |
"go-binary", |
| 320 |
path.Join(build.Default.GOROOT, "bin/go"), |
| 321 |
[]string{"version"}, |
| 322 |
"main.main", |
| 323 |
}, |
| 324 |
} |
| 325 |
|
| 326 |
for _, tt := range tests { |
| 327 |
t.Run(tt.name, func(t *testing.T) { |
| 328 |
if tt.name == "go-binary" { |
| 329 |
|
| 330 |
testutils.SkipOnOldKernel(t, "4.14", "uprobes on Go binaries silently fail on kernel < 4.14") |
| 331 |
} |
| 332 |
|
| 333 |
m, p := newUpdaterMapProg(t, ebpf.Kprobe, 0) |
| 334 |
|
| 335 |
|
| 336 |
ex, err := OpenExecutable(tt.elf) |
| 337 |
qt.Assert(t, qt.IsNil(err)) |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
u, err := ex.Uprobe(tt.sym, p, nil) |
| 342 |
if errors.Is(err, ErrNoSymbol) { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
t.Skipf("executable %s appear to be stripped, skipping", tt.elf) |
| 347 |
} |
| 348 |
qt.Assert(t, qt.IsNil(err)) |
| 349 |
|
| 350 |
|
| 351 |
trigger := func(t *testing.T) { |
| 352 |
qt.Assert(t, qt.IsNil(exec.Command(tt.elf, tt.args...).Run())) |
| 353 |
} |
| 354 |
trigger(t) |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
assertMapValueGE(t, m, 0, 1) |
| 359 |
|
| 360 |
|
| 361 |
qt.Assert(t, qt.IsNil(u.Close())) |
| 362 |
|
| 363 |
|
| 364 |
qt.Assert(t, qt.IsNil(m.Update(uint32(0), uint32(0), ebpf.UpdateExist))) |
| 365 |
|
| 366 |
|
| 367 |
trigger(t) |
| 368 |
|
| 369 |
|
| 370 |
assertMapValue(t, m, 0, 0) |
| 371 |
}) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestUprobeProgramWrongPID(t *testing.T) { |
| 376 |
testutils.SkipOnOldKernel(t, "4.14", "uprobe on v4.9 returns EIO on vimto") |
| 377 |
|
| 378 |
m, p := newUpdaterMapProg(t, ebpf.Kprobe, 0) |
| 379 |
|
| 380 |
|
| 381 |
ex, err := OpenExecutable("/bin/bash") |
| 382 |
qt.Assert(t, qt.IsNil(err)) |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
u, err := ex.Uprobe("main", p, &UprobeOptions{PID: os.Getpid()}) |
| 390 |
qt.Assert(t, qt.IsNil(err)) |
| 391 |
defer u.Close() |
| 392 |
|
| 393 |
|
| 394 |
qt.Assert(t, qt.IsNil(exec.Command("/bin/bash", "--help").Run())) |
| 395 |
|
| 396 |
|
| 397 |
assertMapValue(t, m, 0, 0) |
| 398 |
} |
| 399 |
|
| 400 |
func TestHaveRefCtrOffsetPMU(t *testing.T) { |
| 401 |
testutils.CheckFeatureTest(t, haveRefCtrOffsetPMU) |
| 402 |
} |