//go:build linux package procmon import ( "crypto/md5" "crypto/sha1" "encoding/hex" "hash" "io" "os" "path/filepath" "strconv" "strings" "sync" "syscall" "testing" "time" ) // These tests exercise the real production checksum method. Distinct Process // objects refer to our own live image; they do not launch or inspect other apps. // rchar measures bytes returned by reads (including page-cache reads), not disk IO. func checksumReadChars(tb testing.TB) uint64 { tb.Helper() raw, err := os.ReadFile("/proc/self/io") if err != nil { tb.Fatal(err) } for _, line := range strings.Split(string(raw), "\n") { if strings.HasPrefix(line, "rchar:") { n, err := strconv.ParseUint(strings.TrimSpace(strings.TrimPrefix(line, "rchar:")), 10, 64) if err != nil { tb.Fatal(err) } return n } } tb.Fatal("missing rchar in /proc/self/io") return 0 } func checksumLiveImage(tb testing.TB) (string, int64) { tb.Helper() path, err := os.Executable() if err != nil { tb.Fatal(err) } info, err := os.Stat(ProcSelfExe) if err != nil { tb.Fatal(err) } // A conservative cache may refuse recently changed files. Settle our newly // linked test image with one bounded wait; do not forge filesystem timestamps. st, ok := info.Sys().(*syscall.Stat_t) if !ok { tb.Fatal("missing Linux stat") } wait := time.Until(time.Unix(st.Ctim.Sec, st.Ctim.Nsec).Add(2100 * time.Millisecond)) if wait > 0 { if wait > 3*time.Second { tb.Fatal("unexpected future executable ctime") } timer := time.NewTimer(wait) defer timer.Stop() <-timer.C } return path, info.Size() } func checksumFixtureProcess(id int, path, exe string) *Process { p := NewProcessEmpty(100000000+id, "checksum-test") p.Path = path p.RealPath = path p.pathExe = exe p.pathProc = "/proc/self" // Keep memory-dump fallbacks bound to the synthetic, nonexistent PID. return p } func checksumExpected(tb testing.TB, path, algo string) string { tb.Helper() var h hash.Hash if algo == HashMD5 { h = md5.New() } else { h = sha1.New() } f, err := os.Open(path) if err != nil { tb.Fatal(err) } defer f.Close() if _, err = io.Copy(h, f); err != nil { tb.Fatal(err) } return hex.EncodeToString(h.Sum(nil)) } func TestChecksumReuseAcrossProcesses(t *testing.T) { path, size := checksumLiveImage(t) want := checksumExpected(t, ProcSelfExe, HashMD5) before := checksumReadChars(t) for i := 0; i < 12; i++ { p := checksumFixtureProcess(i, path, ProcSelfExe) p.ComputeChecksum(HashMD5) if p.Checksums[HashMD5] != want { t.Fatalf("incorrect MD5 for process %d", i) } } reads := checksumReadChars(t) - before t.Logf("distinct_processes=12 image_bytes=%d read_chars=%d", size, reads) if reads > uint64(2*size+128*1024) { t.Fatalf("redundant executable hashing: %d read chars for twelve processes, image=%d", reads, size) } } func TestChecksumConcurrentImageReads(t *testing.T) { path, size := checksumLiveImage(t) want := checksumExpected(t, ProcSelfExe, HashSHA1) const n = 12 procs := make([]*Process, n) for i := range procs { procs[i] = checksumFixtureProcess(100+i, path, ProcSelfExe) } start := make(chan struct{}) var ready, done sync.WaitGroup ready.Add(n) done.Add(n) for _, p := range procs { go func(p *Process) { defer done.Done(); ready.Done(); <-start; p.ComputeChecksum(HashSHA1) }(p) } ready.Wait() before := checksumReadChars(t) close(start) done.Wait() reads := checksumReadChars(t) - before for _, p := range procs { if p.Checksums[HashSHA1] != want { t.Fatal("concurrent SHA1 mismatch") } } t.Logf("concurrent_processes=%d image_bytes=%d read_chars=%d", n, size, reads) if reads > uint64(2*size+128*1024) { t.Fatalf("redundant concurrent executable hashing: %d read chars, image=%d", reads, size) } } func TestChecksumFileIdentityAndAlgorithms(t *testing.T) { dir := t.TempDir() a := filepath.Join(dir, "image-a") b := filepath.Join(dir, "image-b") if err := os.WriteFile(a, []byte("image one"), 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(b, []byte("image two"), 0600); err != nil { t.Fatal(err) } for i, f := range []string{a, b} { // Same reported name, distinct opened images: namespace/path aliases must // never acquire each other's digests. p := checksumFixtureProcess(200+i, "/same/reported/path", f) for _, algo := range []string{HashMD5, HashSHA1} { p.ComputeChecksum(algo) if p.Checksums[algo] != checksumExpected(t, f, algo) { t.Fatal("opened image/algorithm conflation") } } } replacement := filepath.Join(dir, "replacement") if err := os.WriteFile(replacement, []byte("new image"), 0600); err != nil { t.Fatal(err) } if err := os.Rename(replacement, a); err != nil { t.Fatal(err) } p := checksumFixtureProcess(203, a, a) p.ComputeChecksum(HashMD5) if p.Checksums[HashMD5] != checksumExpected(t, a, HashMD5) { t.Fatal("replaced pathname reused old digest") } if err := os.WriteFile(a, []byte("new bytes"), 0600); err != nil { t.Fatal(err) } p = checksumFixtureProcess(204, a, a) p.ComputeChecksum(HashMD5) if p.Checksums[HashMD5] != checksumExpected(t, a, HashMD5) { t.Fatal("mutated image reused old digest") } } func TestChecksumReadFailureIsNotSuccess(t *testing.T) { dir := t.TempDir() p := checksumFixtureProcess(300, dir, dir) p.ComputeChecksum(HashMD5) if p.Checksums[HashMD5] != "" { t.Fatal("directory read error reported as checksum success") } if err := os.Remove(dir); err != nil { t.Fatal(err) } if err := os.WriteFile(dir, []byte("recover after read failure"), 0600); err != nil { t.Fatal(err) } p = checksumFixtureProcess(301, dir, dir) p.ComputeChecksum(HashMD5) if p.Checksums[HashMD5] != checksumExpected(t, dir, HashMD5) { t.Fatal("read failure poisoned subsequent image") } } func BenchmarkChecksumNewProcessImage(b *testing.B) { path, size := checksumLiveImage(b) before := checksumReadChars(b) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { p := checksumFixtureProcess(i, path, ProcSelfExe) p.ComputeChecksum(HashMD5) if len(p.Checksums[HashMD5]) != 32 { b.Fatal("missing checksum") } } b.StopTimer() reads := checksumReadChars(b) - before b.ReportMetric(float64(reads)/float64(b.N), "read-B/op") b.ReportMetric(float64(size), "image-B") } // A missing primary exe must retain the existing full-hash fallback. This is // the cold/nonshareable path, with no mutation of any production cache in tests. func BenchmarkChecksumColdFallback(b *testing.B) { path, size := checksumLiveImage(b) before := checksumReadChars(b) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { p := checksumFixtureProcess(i, path, ProcSelfExe+"/unavailable") p.RealPath = ProcSelfExe p.ComputeChecksum(HashMD5) if len(p.Checksums[HashMD5]) != 32 { b.Fatal("missing fallback checksum") } } b.StopTimer() reads := checksumReadChars(b) - before b.ReportMetric(float64(reads)/float64(b.N), "read-B/op") b.ReportMetric(float64(size), "image-B") } func BenchmarkChecksumExistingProcess(b *testing.B) { path, size := checksumLiveImage(b) p := checksumFixtureProcess(0, path, ProcSelfExe) p.ComputeChecksum(HashMD5) before := checksumReadChars(b) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { p.ComputeChecksum(HashMD5) } b.StopTimer() reads := checksumReadChars(b) - before b.ReportMetric(float64(reads)/float64(b.N), "read-B/op") b.ReportMetric(float64(size), "image-B") } func BenchmarkChecksumConcurrentNewProcesses(b *testing.B) { path, size := checksumLiveImage(b) before := checksumReadChars(b) b.SetParallelism(8) b.ReportAllocs() b.ResetTimer() var idMu sync.Mutex id := 0 b.RunParallel(func(pb *testing.PB) { for pb.Next() { idMu.Lock() id++ n := id idMu.Unlock() p := checksumFixtureProcess(n, path, ProcSelfExe) p.ComputeChecksum(HashMD5) } }) b.StopTimer() reads := checksumReadChars(b) - before b.ReportMetric(float64(reads)/float64(b.N), "read-B/op") b.ReportMetric(float64(size), "image-B") }