Skip to content

Commit 91dc22a

Browse files
author
Ethan Graham
committed
various: Fix lint errors on CI
1 parent 09b1de9 commit 91dc22a

File tree

17 files changed

+172
-143
lines changed

17 files changed

+172
-143
lines changed

pkg/fuzzer/fuzzer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func NewFuzzer(ctx context.Context, cfg *Config, rnd *rand.Rand,
7474
return f
7575
}
7676

77-
func (f *Fuzzer) RecommendedCalls() int {
78-
if f.modeKFuzzTest {
77+
func (fuzzer *Fuzzer) RecommendedCalls() int {
78+
if fuzzer.modeKFuzzTest {
7979
return prog.RecommendedCallsKFuzzTest
8080
}
8181
return prog.RecommendedCalls

pkg/kcov/cdefs.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1+
// Copyright 2025 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
13
package kcov
24

5+
// This file defines values required for KCOV ioctl calls. More information on
6+
// the values and their semantics can be found in the kernel documentation under
7+
// /Documentation/dev-tools/kcov.rst, or at docs.kernel.org/dev-tools/kcov.html.
8+
39
import "unsafe"
410

511
const (
612
sizeofUintPtr = int(unsafe.Sizeof((*int)(nil)))
713

8-
_IOC_NRBITS = 8
9-
_IOC_TYPEBITS = 8
10-
_IOC_SIZEBITS = 14
11-
_IOC_DIRBITS = 2
12-
13-
_IOC_NRSHIFT = 0
14-
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
15-
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
16-
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
17-
18-
_IOC_NONE = 0
19-
_IOC_WRITE = 1
20-
_IOC_READ = 2
21-
)
22-
23-
// KCOV ioctl commands for Linux.
24-
const (
25-
// KCOV_INIT_TRACE initializes kcov tracing.
26-
// #define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
27-
// On amd64, unsigned long is 8 bytes.
28-
KCOV_INIT_TRACE uintptr = (_IOC_READ << _IOC_DIRSHIFT) |
29-
(uintptr(unsafe.Sizeof(uint64(0))) << _IOC_SIZESHIFT) | ('c' << _IOC_TYPESHIFT) | (1 << _IOC_NRSHIFT) // 0x80086301.
30-
31-
// KCOV_ENABLE enables kcov for the current thread.
32-
// #define KCOV_ENABLE _IO('c', 100)
33-
KCOV_ENABLE uintptr = (_IOC_NONE << _IOC_DIRSHIFT) |
34-
(0 << _IOC_SIZESHIFT) | ('c' << _IOC_TYPESHIFT) | (100 << _IOC_NRSHIFT) // 0x6364.
35-
36-
// KCOV_DISABLE disables kcov for the current thread.
37-
// #define KCOV_DISABLE _IO('c', 101)
38-
KCOV_DISABLE uintptr = (_IOC_NONE << _IOC_DIRSHIFT) |
39-
(0 << _IOC_SIZESHIFT) | ('c' << _IOC_TYPESHIFT) | (101 << _IOC_NRSHIFT) // 0x6365.
40-
41-
KCOV_TRACE_PC = 0
42-
KCOV_TRACE_CMP = 1
14+
iocNrBits = 8
15+
iocTypeBits = 8
16+
iocSizeBits = 14
17+
iocDirBits = 2
18+
19+
iocNrShift = 0
20+
iocTypeshift = iocNrShift + iocNrBits
21+
iocSizeShift = iocTypeshift + iocTypeBits
22+
iocDirShift = iocSizeShift + iocSizeBits
23+
24+
iocNone = 0
25+
iocWrite = 1
26+
iocRead = 2
27+
28+
// kcovInitTrace initializes KCOV tracing.
29+
// #define kcovInitTrace _IOR('c', 1, unsigned long)
30+
kcovInitTrace uintptr = (iocRead << iocDirShift) |
31+
(unsafe.Sizeof(uint64(0)) << iocSizeShift) | ('c' << iocTypeshift) | (1 << iocNrShift) // 0x80086301.
32+
33+
// kcovEnable enables kcov for the current thread.
34+
// #define kcovEnable _IO('c', 100)
35+
kcovEnable uintptr = (iocNone << iocDirShift) |
36+
(0 << iocSizeShift) | ('c' << iocTypeshift) | (100 << iocNrShift) // 0x6364.
37+
38+
// kcovDisable disables kcov for the current thread.
39+
// #define kcovDisable _IO('c', 101)
40+
kcovDisable uintptr = (iocNone << iocDirShift) |
41+
(0 << iocSizeShift) | ('c' << iocTypeshift) | (101 << iocNrShift) // 0x6365.
42+
43+
kcovTracePC = 0
44+
kcovTraceCMP = 1
4345
)

pkg/kcov/kcov.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// Package kcov provides Go native code for reading kernel coverage.
1+
// Copyright 2025 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3+
4+
// Package kcov provides Go native code for collecting kernel coverage (KCOV)
5+
// information.
26
package kcov
37

48
import (
@@ -46,50 +50,63 @@ func (st *KCOVState) Trace(f func() error) KCOVTraceResult {
4650

4751
// EnableTracingForCurrentGoroutine prepares the current goroutine for kcov tracing.
4852
// It must be paired with a call to DisableTracing.
49-
func EnableTracingForCurrentGoroutine() (*KCOVState, error) {
53+
func EnableTracingForCurrentGoroutine() (st *KCOVState, err error) {
54+
st = &KCOVState{}
55+
defer func() {
56+
if err != nil {
57+
// The original error is more important, so we ignore any potential
58+
// errors that result from cleaning up.
59+
_ = st.DisableTracing()
60+
}
61+
}()
62+
5063
// KCOV is per-thread, so lock goroutine to its current OS thread.
5164
runtime.LockOSThread()
5265

5366
file, err := os.OpenFile(kcovPath, os.O_RDWR, 0)
5467
if err != nil {
5568
return nil, err
5669
}
57-
st := KCOVState{
58-
file: file,
59-
}
60-
fd := file.Fd()
70+
st.file = file
6171

6272
// Setup trace mode and size.
63-
if err := unix.IoctlSetInt(int(fd), uint(KCOV_INIT_TRACE), kcovCoverSize); err != nil {
64-
st.DisableTracing()
73+
if err := unix.IoctlSetInt(int(st.file.Fd()), uint(kcovInitTrace), kcovCoverSize); err != nil {
6574
return nil, err
6675
}
6776

6877
// Mmap buffer shared between kernel- and user-space. For more information,
6978
// see the Linux KCOV documentation: https://docs.kernel.org/dev-tools/kcov.html.
7079
st.cover, err = unix.Mmap(
71-
int(fd),
80+
int(st.file.Fd()),
7281
0, // Offset.
7382
kcovCoverSize*sizeofUintPtr,
7483
unix.PROT_READ|unix.PROT_WRITE,
7584
unix.MAP_SHARED,
7685
)
7786
if err != nil {
78-
st.DisableTracing()
7987
return nil, err
8088
}
8189

8290
// Enable coverage collection on the current thread.
83-
if err := unix.IoctlSetInt(int(fd), uint(KCOV_ENABLE), KCOV_TRACE_PC); err != nil {
84-
st.DisableTracing()
91+
if err := unix.IoctlSetInt(int(st.file.Fd()), uint(kcovEnable), kcovTracePC); err != nil {
8592
return nil, err
8693
}
87-
88-
return &st, nil
94+
return st, nil
8995
}
9096

91-
func (st *KCOVState) DisableTracing() {
97+
// DisableTracing disables KCOV tracing for the current Go routine. On failure,
98+
// it returns the first error that occurred during cleanup.
99+
func (st *KCOVState) DisableTracing() error {
100+
var firstErr error
101+
if err := unix.IoctlSetInt(int(st.file.Fd()), uint(kcovDisable), kcovTracePC); err != nil {
102+
firstErr = err
103+
}
104+
if err := unix.Munmap(st.cover); err != nil && firstErr == nil {
105+
firstErr = err
106+
}
107+
if err := st.file.Close(); err != nil && firstErr == nil {
108+
firstErr = err
109+
}
92110
runtime.UnlockOSThread()
93-
st.file.Close()
94-
unix.Munmap(st.cover)
111+
return firstErr
95112
}

pkg/kfuzztest-manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2025 syzkaller project authors. All rights reserved.
22
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3-
package kfuzztest_manager
3+
package kfuzztestmanager
44

55
import (
66
"context"

pkg/kfuzztest/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (b *Builder) EmitSyzlangDescription() (string, error) {
7171
// Format the output syzlang descriptions for consistency.
7272
var astError error
7373
eh := func(pos ast.Pos, msg string) {
74-
astError = fmt.Errorf("Failure: %v: %v\n", pos, msg)
74+
astError = fmt.Errorf("ast failure: %v: %v", pos, msg)
7575
}
7676
descAst := ast.Parse([]byte(descBuilder.String()), "", eh)
7777
if astError != nil {

pkg/kfuzztest/extractor.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// Extractor's job is to extract all information relevant to KFuzzTest from a
1313
// VMlinux binary.
1414
type Extractor struct {
15-
// Path to the `vmlinux` being parsed
15+
// Path to the `vmlinux` being parsed.
1616
vmlinuxPath string
1717
elfFile *elf.File
1818
dwarfData *dwarf.Data
@@ -112,7 +112,7 @@ func (e *Extractor) elfSection(addr uint64) *elf.Section {
112112
func (e *Extractor) readElfString(offset uint64) (string, error) {
113113
strSection := e.elfSection(offset)
114114
if strSection == nil {
115-
return "", fmt.Errorf("unable to find section for offset 0x%X\n", offset)
115+
return "", fmt.Errorf("unable to find section for offset 0x%X", offset)
116116
}
117117

118118
// 128 bytes is longer than we expect to see in KFuzzTest metadata.
@@ -167,10 +167,10 @@ func (e *Extractor) getSymbol(symbolName string) (elf.Symbol, error) {
167167
}
168168

169169
func (e *Extractor) extractFuncs() ([]SyzFunc, error) {
170-
var rawFuncs []*kftfTestCase
170+
var rawFuncs []*kfuzztestTarget
171171
var err error
172172

173-
rawFuncs, err = parseKftfObjects[*kftfTestCase](e)
173+
rawFuncs, err = parseKftfObjects[*kfuzztestTarget](e)
174174
if err != nil {
175175
return nil, err
176176
}
@@ -195,10 +195,10 @@ func (e *Extractor) extractFuncs() ([]SyzFunc, error) {
195195
}
196196

197197
func (e *Extractor) extractDomainConstraints() ([]SyzConstraint, error) {
198-
var rawConstraints []*kftfConstraint
198+
var rawConstraints []*kfuzztestConstraint
199199
var err error
200200

201-
rawConstraints, err = parseKftfObjects[*kftfConstraint](e)
201+
rawConstraints, err = parseKftfObjects[*kfuzztestConstraint](e)
202202
if err != nil {
203203
return nil, err
204204
}
@@ -227,10 +227,10 @@ func (e *Extractor) extractDomainConstraints() ([]SyzConstraint, error) {
227227
}
228228

229229
func (e *Extractor) extractAnnotations() ([]SyzAnnotation, error) {
230-
var rawAnnotations []*kftfAnnotation
230+
var rawAnnotations []*kfuzztestAnnotation
231231
var err error
232232

233-
rawAnnotations, err = parseKftfObjects[*kftfAnnotation](e)
233+
rawAnnotations, err = parseKftfObjects[*kfuzztestAnnotation](e)
234234
if err != nil {
235235
return nil, err
236236
}
@@ -400,28 +400,28 @@ func parseKftfObjects[T interface {
400400
*P
401401
parsableFromBytes
402402
}, P any](e *Extractor) ([]T, error) {
403-
var typeinfo T = new(P)
403+
var typeinfo T
404404

405405
startSymbol, err := e.getSymbol(typeinfo.startSymbol())
406406
if err != nil {
407407
return nil, err
408408
} else if startSymbol.Value == 0 {
409-
return nil, fmt.Errorf("Failed to resolve start symbol")
409+
return nil, fmt.Errorf("failed to resolve %s", typeinfo.startSymbol())
410410
}
411411

412412
endSymbol, err := e.getSymbol(typeinfo.endSymbol())
413413
if err != nil {
414414
return nil, err
415415
} else if endSymbol.Value == 0 {
416-
return nil, fmt.Errorf("Failed to resolve end symbol")
416+
return nil, fmt.Errorf("failed to resolve %s", typeinfo.endSymbol())
417417
}
418418

419419
out := make([]T, 0)
420420
data := make([]byte, typeinfo.size())
421421
for addr := startSymbol.Value; addr < endSymbol.Value; addr += typeinfo.size() {
422422
section := e.elfSection(addr)
423423
if section == nil {
424-
return nil, fmt.Errorf("Failed to locate section for addr=0x%x", addr)
424+
return nil, fmt.Errorf("failed to locate section for addr=0x%x", addr)
425425
}
426426

427427
n, err := section.ReadAt(data, int64(addr-section.Addr))
@@ -431,7 +431,7 @@ func parseKftfObjects[T interface {
431431
return nil, err
432432
}
433433

434-
var obj T = new(P)
434+
var obj T
435435
err = obj.fromBytes(e.elfFile, data)
436436
if err != nil {
437437
return nil, err

pkg/kfuzztest/kfuzztest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func ExtractData(vmlinuxPath string) (KFuzzTestData, error) {
129129

130130
var astError error
131131
eh := func(pos ast.Pos, msg string) {
132-
astError = fmt.Errorf("AST error: %v: %v\n", pos, msg)
132+
astError = fmt.Errorf("ast error: %v: %v", pos, msg)
133133
}
134134
descAst := ast.Parse([]byte(desc), "kfuzztest-autogen", eh)
135135
if astError != nil {
@@ -144,7 +144,7 @@ func ExtractData(vmlinuxPath string) (KFuzzTestData, error) {
144144
target := targets.Get(targets.Linux, targets.AMD64)
145145
program := compiler.Compile(descAst, make(map[string]uint64), target, eh)
146146
if astError != nil {
147-
extractState.err = fmt.Errorf("failed to compile extracted KFuzzTest target: %v", astError)
147+
extractState.err = fmt.Errorf("failed to compile extracted KFuzzTest target: %w", astError)
148148
}
149149

150150
kFuzzTestCalls := []*prog.Syscall{}

0 commit comments

Comments
 (0)