Skip to content

Commit

Permalink
tests: update checkpatch.pl from Linux kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Sep 14, 2023
1 parent edd5719 commit 88e3ea6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
72 changes: 69 additions & 3 deletions tests/ci/checkpatch/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
my $tabsize = 8;
my ${CONFIG_} = "CONFIG_";

my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h

sub help {
my ($exitcode) = @_;

Expand Down Expand Up @@ -3270,7 +3272,7 @@ sub process {
# A Fixes:, link or signature tag line
$commit_log_possible_stack_dump)) {
WARN("COMMIT_LOG_LONG_LINE",
"Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
"Prefer a maximum 75 chars per line (possible unwrapped commit description?)\n" . $herecurr);
$commit_log_long_line = 1;
}

Expand Down Expand Up @@ -5046,7 +5048,7 @@ sub process {
if|for|while|switch|return|case|
volatile|__volatile__|
__attribute__|format|__extension__|
asm|__asm__)$/x)
asm|__asm__|scoped_guard)$/x)
{
# cpp #define statements have non-optional spaces, ie
# if there is a space between the name and the open
Expand Down Expand Up @@ -6051,6 +6053,9 @@ sub process {

# check for line continuations outside of #defines, preprocessor #, and asm

} elsif ($realfile =~ m@/vmlinux.lds.h$@) {
$line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
#print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
} else {
if ($prevline !~ /^..*\\$/ &&
$line !~ /^\+\s*\#.*\\$/ && # preprocessor
Expand Down Expand Up @@ -6997,10 +7002,22 @@ sub process {
# }
# }

# strcpy uses that should likely be strscpy
if ($line =~ /\bstrcpy\s*\(/) {
WARN("STRCPY",
"Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88\n" . $herecurr);
}

# strlcpy uses that should likely be strscpy
if ($line =~ /\bstrlcpy\s*\(/) {
WARN("STRLCPY",
"Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
"Prefer strscpy over strlcpy - see: https://github.com/KSPP/linux/issues/89\n" . $herecurr);
}

# strncpy uses that should likely be strscpy or strscpy_pad
if ($line =~ /\bstrncpy\s*\(/) {
WARN("STRNCPY",
"Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90\n" . $herecurr);
}

# typecasts on min/max could be min_t/max_t
Expand Down Expand Up @@ -7107,6 +7124,21 @@ sub process {
"arguments for function declarations should follow identifier\n" . $herecurr);
}

} elsif ($realfile =~ /\.c$/ && defined $stat &&
$stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)
{
my ($st_type, $st_name) = ($1, $2);

for my $s (keys %maybe_linker_symbol) {
#print "Linker symbol? $st_name : $s\n";
goto LIKELY_LINKER_SYMBOL
if $st_name =~ /$s/;
}
WARN("AVOID_EXTERNS",
"found a file-scoped extern type:$st_type name:$st_name in .c file\n"
. "is this a linker symbol ?\n" . $herecurr);
LIKELY_LINKER_SYMBOL:

} elsif ($realfile =~ /\.c$/ && defined $stat &&
$stat =~ /^.\s*extern\s+/)
{
Expand Down Expand Up @@ -7418,6 +7450,16 @@ sub process {
}
}

# check for array definition/declarations that should use flexible arrays instead
if ($sline =~ /^[\+ ]\s*\}(?:\s*__packed)?\s*;\s*$/ &&
$prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
if (ERROR("FLEXIBLE_ARRAY",
"Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) &&
$1 == '0' && $fix) {
$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;
}
}

# nested likely/unlikely calls
if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
WARN("LIKELY_MISUSE",
Expand All @@ -7435,6 +7477,30 @@ sub process {
}
}

# Complain about RCU Tasks Trace used outside of BPF (and of course, RCU).
our $rcu_trace_funcs = qr{(?x:
rcu_read_lock_trace |
rcu_read_lock_trace_held |
rcu_read_unlock_trace |
call_rcu_tasks_trace |
synchronize_rcu_tasks_trace |
rcu_barrier_tasks_trace |
rcu_request_urgent_qs_task
)};
our $rcu_trace_paths = qr{(?x:
kernel/bpf/ |
include/linux/bpf |
net/bpf/ |
kernel/rcu/ |
include/linux/rcu
)};
if ($line =~ /\b($rcu_trace_funcs)\s*\(/) {
if ($realfile !~ m{^$rcu_trace_paths}) {
WARN("RCU_TASKS_TRACE",
"use of RCU tasks trace is incorrect outside BPF or core RCU code\n" . $herecurr);
}
}

# check for lockdep_set_novalidate_class
if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
$line =~ /__lockdep_no_validate__\s*\)/ ) {
Expand Down
23 changes: 22 additions & 1 deletion tests/ci/checkpatch/spelling.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ aquired||acquired
aquisition||acquisition
arbitary||arbitrary
architechture||architecture
archtecture||architecture
arguement||argument
arguements||arguments
arithmatic||arithmetic
Expand Down Expand Up @@ -279,6 +280,7 @@ cant'||can't
canot||cannot
cann't||can't
cannnot||cannot
capabiity||capability
capabilites||capabilities
capabilties||capabilities
capabilty||capability
Expand Down Expand Up @@ -426,6 +428,7 @@ cotrol||control
cound||could
couter||counter
coutner||counter
creationg||creating
cryptocraphic||cryptographic
cummulative||cumulative
cunter||counter
Expand Down Expand Up @@ -492,6 +495,7 @@ destorys||destroys
destroied||destroyed
detabase||database
deteced||detected
detecion||detection
detectt||detect
detroyed||destroyed
develope||develop
Expand All @@ -513,6 +517,7 @@ diferent||different
differrence||difference
diffrent||different
differenciate||differentiate
diffreential||differential
diffrentiate||differentiate
difinition||definition
digial||digital
Expand Down Expand Up @@ -617,6 +622,7 @@ evalute||evaluate
evalutes||evaluates
evalution||evaluation
excecutable||executable
excceed||exceed
exceded||exceeded
exceds||exceeds
exceeed||exceed
Expand All @@ -632,6 +638,7 @@ existant||existent
exixt||exist
exsits||exists
exlcude||exclude
exlcuding||excluding
exlcusive||exclusive
exlusive||exclusive
exmaple||example
Expand Down Expand Up @@ -726,6 +733,8 @@ generiously||generously
genereate||generate
genereted||generated
genric||generic
gerenal||general
geting||getting
globel||global
grabing||grabbing
grahical||graphical
Expand Down Expand Up @@ -899,6 +908,7 @@ iteraions||iterations
iternations||iterations
itertation||iteration
itslef||itself
ivalid||invalid
jave||java
jeffies||jiffies
jumpimng||jumping
Expand Down Expand Up @@ -977,6 +987,7 @@ microprocesspr||microprocessor
migrateable||migratable
millenium||millennium
milliseonds||milliseconds
minimim||minimum
minium||minimum
minimam||minimum
minimun||minimum
Expand Down Expand Up @@ -1042,6 +1053,7 @@ notifed||notified
notity||notify
nubmer||number
numebr||number
numer||number
numner||number
nunber||number
obtaion||obtain
Expand All @@ -1061,6 +1073,7 @@ offet||offset
offlaod||offload
offloded||offloaded
offseting||offsetting
oflload||offload
omited||omitted
omiting||omitting
omitt||omit
Expand Down Expand Up @@ -1105,6 +1118,7 @@ pakage||package
paket||packet
pallette||palette
paln||plan
palne||plane
paramameters||parameters
paramaters||parameters
paramater||parameter
Expand Down Expand Up @@ -1181,12 +1195,14 @@ previsously||previously
primative||primitive
princliple||principle
priorty||priority
priting||printing
privilaged||privileged
privilage||privilege
priviledge||privilege
priviledges||privileges
privleges||privileges
probaly||probably
probabalistic||probabilistic
procceed||proceed
proccesors||processors
procesed||processed
Expand Down Expand Up @@ -1460,6 +1476,7 @@ submited||submitted
submition||submission
succeded||succeeded
suceed||succeed
succesfuly||successfully
succesfully||successfully
succesful||successful
successed||succeeded
Expand Down Expand Up @@ -1503,6 +1520,7 @@ symetric||symmetric
synax||syntax
synchonized||synchronized
sychronization||synchronization
sychronously||synchronously
synchronuously||synchronously
syncronize||synchronize
syncronized||synchronized
Expand All @@ -1523,7 +1541,6 @@ temeprature||temperature
temorary||temporary
temproarily||temporarily
temperture||temperature
thead||thread
theads||threads
therfore||therefore
thier||their
Expand All @@ -1532,6 +1549,7 @@ threee||three
threshhold||threshold
thresold||threshold
throught||through
tansition||transition
trackling||tracking
troughput||throughput
trys||tries
Expand Down Expand Up @@ -1611,6 +1629,7 @@ unneccessary||unnecessary
unnecesary||unnecessary
unneedingly||unnecessarily
unnsupported||unsupported
unuspported||unsupported
unmached||unmatched
unprecise||imprecise
unpriviledged||unprivileged
Expand Down Expand Up @@ -1657,6 +1676,7 @@ verfication||verification
veriosn||version
verisons||versions
verison||version
veritical||vertical
verson||version
vicefersa||vice-versa
virtal||virtual
Expand All @@ -1677,6 +1697,7 @@ whenver||whenever
wheter||whether
whe||when
wierd||weird
wihout||without
wiil||will
wirte||write
withing||within
Expand Down
2 changes: 1 addition & 1 deletion tests/lint/checkpatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ for file in "$@"; do
tmp=$(mktemp)

"$checkpatch_path" --no-tree --terse \
--ignore LEADING_SPACE,SPDX_LICENSE_TAG,CODE_INDENT,NAKED_SSCANF,VOLATILE,NEW_TYPEDEFS,LONG_LINE,LONG_LINE_STRING,QUOTED_WHITESPACE_BEFORE_NEWLINE,STRLCPY \
--ignore LEADING_SPACE,SPDX_LICENSE_TAG,CODE_INDENT,NAKED_SSCANF,VOLATILE,NEW_TYPEDEFS,LONG_LINE,LONG_LINE_STRING,QUOTED_WHITESPACE_BEFORE_NEWLINE,STRLCPY,STRNCPY \
-f "$file" | tee "$tmp"

if [ -s "$tmp" ]; then
Expand Down

0 comments on commit 88e3ea6

Please sign in to comment.