aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/cover/report_test.go
diff options
context:
space:
mode:
authorAlexander Potapenko <glider@google.com>2024-01-10 16:17:08 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-01-17 09:28:44 +0000
commit3392690e404b6ba5022825d33259bc2e9e89eb53 (patch)
tree00e7889468610e958a0cb52762f88c4f7929983e /pkg/cover/report_test.go
parentf3c9957806e53e07de6c36da01736aa5bbbca2bf (diff)
pkg/cover: ensure that all PCs returned by kcov have matching callbacks
In the case some modules' addresses are off, certain kernel addresses returned by kcov may not have corresponding coverage callbacks in the .ko files. Keep an additional map in the backend to verify those addresses and report an error if that is the case. Also adjust text expectations in pkg/cover/report_test.go, as inexact coverage will result in an error now.
Diffstat (limited to 'pkg/cover/report_test.go')
-rw-r--r--pkg/cover/report_test.go32
1 files changed, 27 insertions, 5 deletions
diff --git a/pkg/cover/report_test.go b/pkg/cover/report_test.go
index 64baf3ee2..2c07cb107 100644
--- a/pkg/cover/report_test.go
+++ b/pkg/cover/report_test.go
@@ -11,6 +11,7 @@ package cover
import (
"bytes"
"encoding/csv"
+ "fmt"
"os"
"path/filepath"
"reflect"
@@ -36,8 +37,10 @@ type Test struct {
Progs []Prog
DebugInfo bool
AddCover bool
- Result string
- Supports func(target *targets.Target) bool
+ AddBadPc bool
+ // Inexact coverage generated by AddCover=true may override empty Result.
+ Result string
+ Supports func(target *targets.Target) bool
}
func TestReportGenerator(t *testing.T) {
@@ -74,6 +77,14 @@ func TestReportGenerator(t *testing.T) {
DebugInfo: true,
},
{
+ Name: "mismatch-pcs",
+ AddCover: true,
+ AddBadPc: true,
+ CFlags: []string{"-fsanitize-coverage=trace-pc"},
+ DebugInfo: true,
+ Result: `.* do not have matching coverage callbacks`,
+ },
+ {
Name: "good-pie",
AddCover: true,
CFlags: []string{"-fsanitize-coverage=trace-pc", "-fpie"},
@@ -132,7 +143,7 @@ func TestReportGenerator(t *testing.T) {
}
func testReportGenerator(t *testing.T, target *targets.Target, test Test) {
- rep, csv, err := generateReport(t, target, test)
+ rep, csv, err := generateReport(t, target, &test)
if err != nil {
if test.Result == "" {
t.Fatalf("expected no error, but got:\n%v", err)
@@ -177,7 +188,7 @@ void* aslr_base() { return NULL; }
void __sanitizer_cov_trace_pc() { printf("%llu", (long long)(__builtin_return_address(0) - aslr_base())); }
`
-func buildTestBinary(t *testing.T, target *targets.Target, test Test, dir string) string {
+func buildTestBinary(t *testing.T, target *targets.Target, test *Test, dir string) string {
kcovSrc := filepath.Join(dir, "kcov.c")
kcovObj := filepath.Join(dir, "kcov.o")
if err := osutil.WriteFile(kcovSrc, []byte(kcovCode)); err != nil {
@@ -257,7 +268,7 @@ func buildTestBinary(t *testing.T, target *targets.Target, test Test, dir string
return bin
}
-func generateReport(t *testing.T, target *targets.Target, test Test) ([]byte, []byte, error) {
+func generateReport(t *testing.T, target *targets.Target, test *Test) ([]byte, []byte, error) {
dir := t.TempDir()
bin := buildTestBinary(t, target, test, dir)
cfg := &mgrconfig.Config{
@@ -292,6 +303,7 @@ func generateReport(t *testing.T, target *targets.Target, test Test) ([]byte, []
}
if test.AddCover {
var pcs []uint64
+ Inexact := false
// Sanitizers crash when installing signal handlers with static libc.
const sanitizerOptions = "handle_segv=0:handle_sigbus=0:handle_sigfpe=0"
cmd := osutil.Command(bin)
@@ -322,6 +334,16 @@ func generateReport(t *testing.T, target *targets.Target, test Test) ([]byte, []
pcs = append(pcs, main.Addr+uint64(off))
}
t.Logf("using inexact coverage range 0x%x-0x%x", main.Addr, main.Addr+uint64(main.Size))
+ Inexact = true
+ }
+ // All PCs are required to have corresponding coverage callbacks. If the expected result is empty,
+ // override it with an error message.
+ if Inexact && test.Result == "" {
+ test.Result = fmt.Sprintf("%d out of %d PCs returned by kcov do not have matching coverage callbacks",
+ len(pcs)-1, len(pcs))
+ }
+ if test.AddBadPc {
+ pcs = append(pcs, 0xdeadbeef)
}
progs = append(progs, Prog{Data: "main", PCs: pcs})
}