Add lint and go generate steps to CI (#254)

* Add lint and go generate stages to CI

Add CI step to verify `go generate` was run on repo.
Add linter stage to CI along with linter config file,
`.golangci.yml`.
Will likely prefer revive over static-check.

Updated README Contributing section on linting requirements.

Added sequence ordering to make sure lint and go generate stages run
before tests and build.
This way, build and tests are not run on code that could potentially:

    1. not build due to `gofmt` issues;
    2. contain bugs;
    3. have to be re-submitted after issues are fixed; or
    4. contain outdated Win32 syscall or other auto-generated files.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* Fixed linter issues

Code changes to satisfy linters:

 - Ran `gofmt -s -w` on repo.
 - Broke up long lines.
 - When possible, changed names with incorrect initialism formatting
   - Added exceptions for exported variables.
 - Added exceptions for ALL_CAPS_WITH_UNDERSCORES code.
   - Switched to using `windows` or `syscall` definitions if possible;
     especially if some constants were unused.
 - Added `_ =` to satisfy error linter, and acknowledge that errors are
   being ignored.
 - Switched to using `errors.Is` and `As` in places, elsewhere added
   exceptions if error value was known to be `syscall.Errno`.
 - Removed bare returns.
 - Prevented variables from being overshadowed in certain places
   (ignoring cases of overshadowing `err`).
 - Renamed variables and functions (eg, `len`, `eventMetadata.bytes`) to
   prevent shadowing pre-built functions and imported pacakges.
 - Removed unused method receivers.
 - Added exceptions to certain unused (unexported) constants and
   functions.
   - Deleted unused `once` from `pkg/etw.providerMap`.
 - Renamed `noop.go` files to `main_other.go` or `doc.go`, to better fit
   style recommendations.
 - Added exceptions for non-secure use of SHA1 and weak crypto
   libraries.
 - Replaced `ioutil` with `io` and `os` (and `t.TempDir` in tests).
 - Added fully exhaustive checks for `switch` statements in `pkg/etw`.
 - Defined constant strings for `tools/mkwinsyscall`.
 - Removed unnecessary conversions.
 - Made sure `context.Cancel` was called.

Additionally, added `//go:build windows" constraints on files with
unexported code, since linter will complain about unused code on
non-Windows platforms.

Added a stub `main() {}` for `mkwinsyscall` for non-Windows builds, just in
case `//go:generate` directives are added to OS-agnostic files.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: spelling, constants, fuzzing

Moved HVSocket fuzzing tests to separate file with go 1.18 build
constraint.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
This commit is contained in:
Hamza El-Saawy
2022-08-23 15:05:05 -04:00
committed by GitHub
parent 79ae8cea02
commit e268c11e27
67 changed files with 1192 additions and 674 deletions
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package main
@@ -1,3 +1,4 @@
//go:build windows
// +build windows
package main
+19 -14
View File
@@ -18,13 +18,16 @@ like func declarations if //sys is replaced by func, but:
- If go func name needs to be different from its winapi dll name,
the winapi name could be specified at the end, after "=" sign, like
//sys LoadLibrary(libname string) (handle uint32, err error) = LoadLibraryA
- Each function that returns err needs to supply a condition, that
return value of winapi will be tested against to detect failure.
This would set err to windows "last-error", otherwise it will be nil.
The value can be provided at end of //sys declaration, like
//sys LoadLibrary(libname string) (handle uint32, err error) [failretval==-1] = LoadLibraryA
and is [failretval==0] by default.
- If the function name ends in a "?", then the function not existing is non-
@@ -32,21 +35,23 @@ like func declarations if //sys is replaced by func, but:
Usage:
mkwinsyscall [flags] [path ...]
mkwinsyscall [flags] [path ...]
The flags are:
-output string
Specify output file name (standard output if omitted).
-sort
Sort DLL and Function declarations (default true). Setting to false is intended to maintain
compatibility with older versions of mkwinsyscall so that diffs are easier to read and understand.
-systemdll
Specify that all DLLs should be loaded from the Windows system directory (default true).
-trace
Generate print statement after every syscall.
winio
Import this package ("github.com/Microsoft/go-winio").
Flags
-output string
Output file name (standard output if omitted).
-sort
Sort DLL and function declarations (default true).
Intended to help transition from older versions of mkwinsyscall by making diffs
easier to read and understand.
-systemdll
Whether all DLLs should be loaded from the Windows system directory (default true).
-trace
Generate print statement after every syscall.
-utf16
Encode string arguments as UTF-16 for syscalls not ending in 'A' or 'W' (default true).
-winio
Import this package ("github.com/Microsoft/go-winio").
*/
package main
+47 -28
View File
@@ -16,7 +16,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -29,6 +28,24 @@ import (
"golang.org/x/sys/windows"
)
const (
pkgSyscall = "syscall"
pkgWindows = "windows"
// common types.
tBool = "bool"
tBoolPtr = "*bool"
tError = "error"
tString = "string"
// error variable names.
varErr = "err"
varErrNTStatus = "ntStatus"
varErrHR = "hr"
)
var (
filename = flag.String("output", "", "output file name (standard output if omitted)")
printTraceFlag = flag.Bool("trace", false, "generate print statement after every syscall")
@@ -53,20 +70,20 @@ func packagename() string {
}
func windowsdot() string {
if packageName == "windows" {
if packageName == pkgWindows {
return ""
}
return "windows."
return pkgWindows + "."
}
func syscalldot() string {
if packageName == "syscall" {
if packageName == pkgSyscall {
return ""
}
return "syscall."
return pkgSyscall + "."
}
// Param is function parameter
// Param is function parameter.
type Param struct {
Name string
Type string
@@ -134,9 +151,9 @@ func (p *Param) StringTmpVarCode() string {
// TmpVarCode returns source code for temp variable.
func (p *Param) TmpVarCode() string {
switch {
case p.Type == "bool":
case p.Type == tBool:
return p.BoolTmpVarCode()
case p.Type == "*bool":
case p.Type == tBoolPtr:
return p.BoolPointerTmpVarCode()
case strings.HasPrefix(p.Type, "[]"):
return p.SliceTmpVarCode()
@@ -148,7 +165,7 @@ func (p *Param) TmpVarCode() string {
// TmpVarReadbackCode returns source code for reading back the temp variable into the original variable.
func (p *Param) TmpVarReadbackCode() string {
switch {
case p.Type == "*bool":
case p.Type == tBoolPtr:
return fmt.Sprintf("*%s = %s != 0", p.Name, p.tmpVar())
default:
return ""
@@ -170,11 +187,11 @@ func (p *Param) SyscallArgList() []string {
t := p.HelperType()
var s string
switch {
case t == "*bool":
case t == tBoolPtr:
s = fmt.Sprintf("unsafe.Pointer(&%s)", p.tmpVar())
case t[0] == '*':
s = fmt.Sprintf("unsafe.Pointer(%s)", p.Name)
case t == "bool":
case t == tBool:
s = p.tmpVar()
case strings.HasPrefix(t, "[]"):
return []string{
@@ -189,12 +206,12 @@ func (p *Param) SyscallArgList() []string {
// IsError determines if p parameter is used to return error.
func (p *Param) IsError() bool {
return p.Name == "err" && p.Type == "error"
return p.Name == varErr && p.Type == tError
}
// HelperType returns type of parameter p used in helper function.
func (p *Param) HelperType() string {
if p.Type == "string" {
if p.Type == tString {
return p.fn.StrconvType()
}
return p.Type
@@ -226,9 +243,9 @@ type Rets struct {
// ErrorVarName returns error variable name for r.
func (r *Rets) ErrorVarName() string {
if r.ReturnsError {
return "err"
return varErr
}
if r.Type == "error" {
if r.Type == tError {
return r.Name
}
return ""
@@ -241,7 +258,7 @@ func (r *Rets) ToParams() []*Param {
ps = append(ps, &Param{Name: r.Name, Type: r.Type})
}
if r.ReturnsError {
ps = append(ps, &Param{Name: "err", Type: "error"})
ps = append(ps, &Param{Name: varErr, Type: tError})
}
return ps
}
@@ -295,8 +312,8 @@ func (r *Rets) SetErrorCode() string {
const code = `if r0 != 0 {
%s = %sErrno(r0)
}`
const ntstatus = `if r0 != 0 {
ntstatus = %sNTStatus(r0)
const ntStatus = `if r0 != 0 {
%s = %sNTStatus(r0)
}`
const hrCode = `if int32(r0) < 0 {
if r0&0x1fff0000 == 0x00070000 {
@@ -311,22 +328,22 @@ func (r *Rets) SetErrorCode() string {
if r.Name == "" {
return r.useLongHandleErrorCode("r1")
}
if r.Type == "error" {
if r.Type == tError {
switch r.Name {
case "ntstatus":
return fmt.Sprintf(ntstatus, windowsdot())
case "hr":
case varErrNTStatus, strings.ToLower(varErrNTStatus): // allow ntstatus to work
return fmt.Sprintf(ntStatus, r.Name, windowsdot())
case varErrHR:
return fmt.Sprintf(hrCode, r.Name, syscalldot())
default:
return fmt.Sprintf(code, r.Name, syscalldot())
}
}
s := ""
var s string
switch {
case r.Type[0] == '*':
s = fmt.Sprintf("%s = (%s)(unsafe.Pointer(r0))", r.Name, r.Type)
case r.Type == "bool":
case r.Type == tBool:
s = fmt.Sprintf("%s = r0 != 0", r.Name)
default:
s = fmt.Sprintf("%s = %s(r0)", r.Name, r.Type)
@@ -564,7 +581,7 @@ func (f *Fn) HelperCallParamList() string {
a := make([]string, 0, len(f.Params))
for _, p := range f.Params {
s := p.Name
if p.Type == "string" {
if p.Type == tString {
s = p.tmpVar()
}
a = append(a, s)
@@ -583,7 +600,7 @@ func (f *Fn) MaybeAbsent() string {
}`
errorVar := f.Rets.ErrorVarName()
if errorVar == "" {
errorVar = "err"
errorVar = varErr
}
return fmt.Sprintf(code, errorVar, f.DLLFuncName())
}
@@ -616,7 +633,7 @@ func (f *Fn) StrconvType() string {
// Otherwise it is false.
func (f *Fn) HasStringParam() bool {
for _, p := range f.Params {
if p.Type == "string" {
if p.Type == tString {
return true
}
}
@@ -892,7 +909,8 @@ func main() {
if *filename == "" {
_, err = os.Stdout.Write(data)
} else {
err = ioutil.WriteFile(*filename, data, 0644)
//nolint:gosec // G306: code file, no need for wants 0600
err = os.WriteFile(*filename, data, 0644)
}
if err != nil {
log.Fatal(err)
@@ -900,6 +918,7 @@ func main() {
}
// TODO: use println instead to print in the following template
const srcTemplate = `
{{define "main"}} //go:build windows