2015-10-12 08:16:57 +00:00
|
|
|
// Copyright 2015 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-12-12 08:13:35 +00:00
|
|
|
"path/filepath"
|
2015-10-12 08:16:57 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-06-21 12:38:08 +00:00
|
|
|
"sync"
|
2017-11-16 11:42:30 +00:00
|
|
|
"time"
|
2015-10-13 12:58:50 +00:00
|
|
|
|
2017-06-17 10:40:18 +00:00
|
|
|
"github.com/google/syzkaller/pkg/cover"
|
2017-11-16 11:42:30 +00:00
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2020-05-27 12:26:00 +00:00
|
|
|
"github.com/google/syzkaller/sys/targets"
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
2016-09-06 17:35:48 +00:00
|
|
|
var (
|
2018-06-21 12:38:08 +00:00
|
|
|
initCoverOnce sync.Once
|
|
|
|
initCoverError error
|
|
|
|
initCoverVMOffset uint32
|
2018-12-09 14:32:10 +00:00
|
|
|
reportGenerator *cover.ReportGenerator
|
2016-09-06 17:35:48 +00:00
|
|
|
)
|
|
|
|
|
2020-05-27 12:26:00 +00:00
|
|
|
func initCover(target *targets.Target, kernelObj, kernelSrc, kernelBuildSrc string) error {
|
2019-05-26 11:55:37 +00:00
|
|
|
initCoverOnce.Do(func() {
|
|
|
|
if kernelObj == "" {
|
|
|
|
initCoverError = fmt.Errorf("kernel_obj is not specified")
|
|
|
|
return
|
|
|
|
}
|
2020-05-27 12:26:00 +00:00
|
|
|
vmlinux := filepath.Join(kernelObj, target.KernelObject)
|
|
|
|
reportGenerator, initCoverError = cover.MakeReportGenerator(target, vmlinux, kernelSrc, kernelBuildSrc)
|
2019-05-26 11:55:37 +00:00
|
|
|
if initCoverError != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-27 12:26:00 +00:00
|
|
|
initCoverVMOffset, initCoverError = getVMOffset(vmlinux, target.OS)
|
2019-05-26 11:55:37 +00:00
|
|
|
})
|
|
|
|
return initCoverError
|
2016-09-06 17:35:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 12:26:00 +00:00
|
|
|
func coverToPCs(target *targets.Target, cov []uint32) []uint64 {
|
2018-02-20 19:44:04 +00:00
|
|
|
pcs := make([]uint64, 0, len(cov))
|
2019-05-26 11:55:37 +00:00
|
|
|
for _, pc := range cov {
|
|
|
|
fullPC := cover.RestorePC(pc, initCoverVMOffset)
|
2020-05-27 12:26:00 +00:00
|
|
|
prevPC := cover.PreviousInstructionPC(target, fullPC)
|
2019-05-26 11:55:37 +00:00
|
|
|
pcs = append(pcs, prevPC)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2019-05-26 11:55:37 +00:00
|
|
|
return pcs
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 18:07:41 +00:00
|
|
|
func getVMOffset(vmlinux, OS string) (uint32, error) {
|
|
|
|
if OS == "freebsd" {
|
|
|
|
return 0xffffffff, nil
|
|
|
|
}
|
2017-11-23 08:21:00 +00:00
|
|
|
out, err := osutil.RunCmd(time.Hour, "", "readelf", "-SW", vmlinux)
|
2016-04-27 15:37:54 +00:00
|
|
|
if err != nil {
|
2017-11-16 11:42:30 +00:00
|
|
|
return 0, err
|
2016-04-27 15:37:54 +00:00
|
|
|
}
|
|
|
|
s := bufio.NewScanner(bytes.NewReader(out))
|
|
|
|
var addr uint32
|
|
|
|
for s.Scan() {
|
|
|
|
ln := s.Text()
|
|
|
|
pieces := strings.Fields(ln)
|
|
|
|
for i := 0; i < len(pieces); i++ {
|
|
|
|
if pieces[i] != "PROGBITS" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseUint("0x"+pieces[i+1], 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to parse addr in readelf output: %v", err)
|
|
|
|
}
|
|
|
|
if v == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v32 := (uint32)(v >> 32)
|
|
|
|
if addr == 0 {
|
|
|
|
addr = v32
|
|
|
|
}
|
|
|
|
if addr != v32 {
|
|
|
|
return 0, fmt.Errorf("different section offsets in a single binary")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|