Commit Graph

44 Commits

Author SHA1 Message Date
Benjamin Cabé a0287cac7f scripts: coccinelle: add errno as a reserved name
errno is defined by the ISO C standard to be a modifiable lvalue of type
int, and **must not be explicitly declared**

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2024-08-22 14:21:53 -04:00
Rubin Gerritsen 470a0fcaa5 cocinelle: Add a sematic patch file for ztest string assertions
ztest now provides functionality to compare strings.
These are simpler to use than the strcmp ways.

The semantic patch transforms many of the commonly used patterns.
It does not handle variable length macros.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2024-06-14 19:25:42 +02:00
Rubin Gerritsen fc377363e4 cocinelle: Parse ztest test functions
We want to able to use cocinelle on ztest functions as
well when transforming APIs.

Provide a simple macro so that test functions are recognized.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2024-06-14 19:25:42 +02:00
Sachin D Kulkarni 0399213cd5 scripts: coccinelle: Remove strtok_r symbol from checklist
strtok_r is part of GNU99, but default Zephyr is built with C99, so,
the GNU headers are not pulled in, even if we add "--std=gnu99" it
won't override the C99 standard.

As a workaround for build we redefine the prototype, but this causes a
coding guideline warning, so, for now disable this warning
for strtok_r.

Signed-off-by: Sachin D Kulkarni <sachin.kulkarni@nordicsemi.no>
2023-08-23 16:05:08 +01:00
Nazar Kazakov f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Anas Nashif 184b786fe2 coccinelle: check reserved symbols based on a file
Use a file with reserved symbols instead of checking against a fixed
list of strings.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-04-30 22:09:43 -04:00
Yasushi SHOJI 2b32e47a9a scripts: coccinelle: Fix parse error
The commit coccinelle/coccinelle@47bd4cae52 changed a behavior of
parsing right before coccinelle v1.1.0.  With the commit, the current
scripts under scripts/coccinelle/ errors out with:

   minus: parse error:
     File ".../zephyr/scripts/coccinelle/deref_null.cocci",
     line 25, column 42, charpos = 666
     around = '...',
     whole content =  (E != NULL && ...) ? <+...E->f@p1...+> : ...

I've already raised an issue upstream coccinelle/coccinelle#257.  But
Debian is already shipping v1.1.0 and we need a fix.

The proposed fix doesn't change the semantics, it just explicitly
states that the rule is an expression.

Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
2021-04-14 14:18:02 -04:00
Flavio Ceolin a025fb2747 coccinelle: Add script to check rule 14.4
Controlling expression shall have essentially boolean
type.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-08 07:56:28 -04:00
Flavio Ceolin a869da55c6 scripts: coccinelle: Add guideline number on a script output
Add information about the guideline number in the script checking a
identifier length.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-08 07:56:28 -04:00
Flavio Ceolin e8fdac3be0 scripts: coccinelle: Check rule 21.2
This coccinelle script can check some violations for rule 21.2.
Currently it is checking the follow reserved names:

"remove", "rewind", "malloc", "free", "exp", "signal"

It can easily be extended in the future though.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>

Rebnase
2021-04-08 07:56:28 -04:00
Flavio Ceolin 0058c02252 scripts: coccinelle: Check rule 5.7
This coccinelle script can check some violations for rule 5.7.
It can identify things like:

struct device *device

But it is not capable to identify:

struct test { ... }

...

int test;

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-08 07:56:28 -04:00
Tomasz Bursztyka 72d9e8c8ab scripts: coccinelle: Scripts for finding wrong device instance usage
2 scripts are provided.
- find_functions.cocci (name probably sucks...)
- find_dev_usage.cocci (ditto...)

find_functions.cocci can patch files where device instance are not
const.

Then it is used to generate the function database:

./scripts/coccicheck --mode=report --jobs=1 \
--cocci=scripts/coccinelle/find_functions.cocci \
--sp-flag="--include-headers" ./

Then, find_dev_usage.cocci will check if the const qualifier is, or
might be lost in a function call.

For instance:
./scripts/coccicheck --mode=report --jobs=1 \
--cocci=scripts/coccinelle/find_dev_usage.cocci \
--sp-flag="--include-headers" drivers/i2c

Which will output a WARNING on non-zephyr functions calls with a device:

./drivers/i2c/i2c_cc13xx_cc26xx.c:393:5-8: WARNING: in i2c_cc13xx_cc26xx_pm_control calling cb param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_mcux_lpi2c.c:205:40-43: WARNING: in mcux_lpi2c_init calling LPI2C_MasterTransferCreateHandle param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_nrfx_twi.c:258:5-8: WARNING: in twi_nrfx_pm_control calling cb param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_nrfx_twi.c:202:22-25: WARNING: in init_twi calling nrfx_twi_init param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_mcux.c:187:38-41: WARNING: in i2c_mcux_init calling I2C_MasterTransferCreateHandle param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_mcux_flexcomm.c:184:43-46: WARNING: in mcux_flexcomm_init calling I2C_MasterTransferCreateHandle param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_nrfx_twim.c:232:5-8: WARNING: in twim_nrfx_pm_control calling cb param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_nrfx_twim.c:174:8-11: WARNING: in init_twim calling nrfx_twim_init param with dev, check if const qualifier is not lost
./drivers/i2c/i2c_rv32m1_lpi2c.c:246:6-9: WARNING: in rv32m1_lpi2c_init calling LPI2C_MasterTransferCreateHandle param with dev, check if const qualifier is not lost

Or:
./scripts/coccicheck --mode=report --jobs=1 \
--cocci=scripts/coccinelle/find_dev_usage.cocci \
--sp-flag="--include-headers" drivers/ieee802154

Which will output an ERROR on using a zephyr function that looses the
const qualifier:

drivers/ieee802154/ieee802154_rf2xx.c:778:3-6: ERROR: in rf2xx_init calling k_thread_create param with dev, loosing const qualifier, please wrap
drivers/ieee802154/ieee802154_nrf5.c:477:19-22: ERROR: in nrf5_init calling k_thread_create param with dev, loosing const qualifier, please wrap
drivers/ieee802154/ieee802154_cc1200.c:819:3-6: ERROR: in cc1200_init calling k_thread_create param with dev, loosing const qualifier, please wrap
drivers/ieee802154/ieee802154_mcr20a.c:1443:3-6: ERROR: in mcr20a_init calling k_thread_create param with dev, loosing const qualifier, please wrap
drivers/ieee802154/ieee802154_cc2520.c:1116:3-6: ERROR: in cc2520_init calling k_thread_create param with dev, loosing const qualifier, please wrap
drivers/ieee802154/ieee802154_cc13xx_cc26xx.c:439:32-35: ERROR: in ieee802154_cc13xx_cc26xx_data_init calling k_thread_create param with dev, loosing const qualifier, please wrap

ISSUE:
- Is it possible to run a set of rules first on all the code, and then
  another set, both sets being in the same .cocci file?
  Would be nice to have all at once.

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Kumar Gala a1b77fd589 zephyr: replace zephyr integer types with C99 types
git grep -l 'u\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/u\(8\|16\|32\|64\)_t/uint\1_t/g"
	git grep -l 's\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/s\(8\|16\|32\|64\)_t/int\1_t/g"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-06-08 08:23:57 -05:00
Peter Bigot 49e8c7080f scripts/coccinelle: add script to preserve const qualifier on config_info
Drivers cast the device config_info pointer to a driver-specific
structure.  The referenced object is const-qualified; make sure the
cast doesn't inadvertently remove that qualifier.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-05-13 18:21:52 +02:00
Peter Bigot f77e03bb44 scripts/coccinelle: rename and extend ms to timeout conversion script
Originally this only dealt with constant timeouts.  Add the ability to
recognize integer parameters and convert them as well.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-05-07 19:46:03 +02:00
Peter Bigot 15ed58652e scripts/cocci: add Zephyr macro file
Zephyr uses some defines to provide additional information about the
item being declared.  When unrecognized these can confuse the
Coccinelle parser so that it does not apply semantic patches in
situations where they should be applied.

Add a macro file that extends the Coccinelle builtin macro file with
some identifiers that are specific to Zephyr.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-05-05 06:15:04 -04:00
Peter Bigot 1f228da658 scripts/coccinelle: add script to convert legacy timeout values
Some API that takes timeouts represented by integral milliseconds
recommended using the K_NO_WAIT and K_FOREVER constants, which are no
longer integers.  This script replaces the incorrect arguments with
the corresponding legacy integral value.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-30 18:26:26 +02:00
Peter Bigot c661caf531 scripts/coccinelle: fix int_literal_to_timeout for K_THREAD_DEFINE
The patch specification should not be conditional on failing to match
an identifier, as some replaced values are identifiers.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-15 08:28:57 -05:00
Peter Bigot d4bb09c083 scripts/coccinelle: improve int_literal_to_timeout
Replace timeout parameters that are multiples of MSEC_PER_SEC, or such
a value passed through K_MSEC, with the normalized timeout expression
using whole seconds.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-02 19:47:51 +03:00
Peter Bigot 9d29e1c48c scripts/coccinelle: update int_literal_to_timeout for thread defines
Recent timeout rework reverted the interpretation of the delay
parameter to K_THREAD_DEFINE from a timeout to a count in
milliseconds, although the corresponding parameter in the
k_thread_create() function remains a timeout.  Convert timeout
expressions to millisecond durations where necessary.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-02 19:47:51 +03:00
Peter Bigot 03c22b8c94 scripts/coccinelle: add k_thread create/define to timeout standardization
k_thread_create and K_THREAD_DEFINE both take a delay as the final
parameter.  Most uses of K_THREAD_DEFINE pass either `K_NO_WAIT` or
`K_FOREVER`.  Ensure that all uses of K_THREAD_DEFINE follow that
practice, and that the runtime k_thread_create calls do so as well.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-09 08:38:10 -04:00
Peter Bigot f0c0b3b5dc scripts/coccinelle: add sleep to int literal to timeout standardization
k_sleep uses the same underlying thread infrastructure as the other
functions that take timeouts, so the delay should be specified as a
timeout rather than milliseconds.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-09 08:38:10 -04:00
Peter Bigot 567d3c4a3e scripts/coccinelle: more cleanup of int literal to timeout
Sort the functions within the regular expression so they can be
checked more easily.

Remove k_thread_deadline_set as it takes an argument in cycles.  (The
one in-tree call to this function was not affected by this error.)

Add missed k_mbox_data_block_get.

Fix an overly ambitious multi-match disjunct that covered some
non-existent functions.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-09 08:38:10 -04:00
Peter Bigot 3a0a582119 scripts/coccinelle: updates and improvements for integer timeout values
Add support for the report and patch modes so this can be invoked by
coccicheck.

Use PCRE options to make the kernel timeout API identifier rule more
readable.  Extend the pattern to new API.

Use rule extends and depends clauses, and pattern disjunction, to
avoid replicating metavariable content.

Hint that using --include-headers may be helpful (some patterns can be
found in static inline functions).

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-04 16:23:57 -04:00
Peter Bigot 0e5329331b scripts/coccinelle: add conversion of integers to timeout values
Some legacy code still passes integer literals in milliseconds as the
value to functions that take a timeout.  This usage interferes with
plans to replace the millisecond representation with a more generic
k_timeout_t value.  Add a Coccinelle script to convert call sites to
use the proper constants and macros.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-03 11:55:44 -07:00
Patrik Flykt 83de530d5a scripts/coccinelle: Add script for counting identifier length
Add a simple Coccinelle script that counts identifier lengths and
prints out a warning if it is longer than 31 characters.

The script can be run with:
spatch -D report --very-quiet \
--include-headers --recursive-includes \
--cocci-file $ZEPHYR_BASE/scripts/coccinelle/identifier_length.cocci \
--dir $ZEPHYR_BASE \
kernel/

Where '--include-headers' and '--recursive-includes' can be omitted
if neede.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-04-18 12:29:07 -04:00
Patrik Flykt 2fb87b9dd2 scripts/coccinelle: Update unsigned suffix script
Update unsigned suffix script to properly match multiplication.
Fix provided by Himanshu Jha.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-28 17:15:58 -05:00
Patrik Flykt caebf204c6 scripts/coccinelle: Update Coccinelle script for unsigned values
Update the script to detect and update more instances of unsigned
variable assignments when using all four simple rules of arithmetics.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-28 17:15:58 -05:00
Flavio Ceolin 8f3459b3db coccinelle: Updating ignore_return to support memcpy
The return of memcpy was being ignored just like memset's return. Just
adding it to coccinelle script.

MISRA-C rule 17.7

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-12-19 14:37:25 +01:00
Patrik Flykt 1b48cb6dc1 scripts/coccinelle: Add Coccinelle script for unsigned values
Add a Coccinelle script that adds an 'U' to values assigned to
unsigned variables, according ot MISRA-C rule 7.2.

Add a 'report' mode to the script that can be used by developer/CI
and a 'patch' mode that should do the heavy lifting.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-12-04 22:51:56 -05:00
Himanshu Jha 40b5442699 coccinelle: Add script to remove unnecessary return variable
This script helps to remove bogus intermediate local variable
used in functions to store return value and instead return
directly while saving few bits of memory.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-28 11:54:01 -08:00
Himanshu Jha 817f4b374f coccinelle: Suppress reports/warnings for `ext/`
The following addition `depends on !(file in "ext")` allows
to exclude `ext/` warnings reported by coccinelle scripts.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-14 19:20:34 -05:00
Himanshu Jha 58781d5782 coccinelle: Add script to check legal usage of sizeof expression
sizeof when applied to a pointer typed expression gives the size of
the pointer and not the size of the object associated with the pointer
expression leading to errors.

This scripts checks for inconsistencies where sizeof is incorrectly
used, especially while calculating size of memory to be allocated in
memory allocating functions.

Eg:

-	memset(pStr, 0, sizeof(pStr));
+	memset(pStr, 0, sizeof(*pStr));

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:34:24 -05:00
Himanshu Jha 1e3e10cfa8 coccinelle: Add script to remove redundant semicolon
This script allowes to remove the redundant semicolon from
`if`, `switch`, `while`, `for` statements.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:34:06 -05:00
Himanshu Jha 6f2c7eed49 coccinelle: Add script to find cases of unsigned < 0
Unsigned expressions cannot be less than zero and presence
of such practices very likely indicates a bug.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:33:43 -05:00
Himanshu Jha 1df011c8e2 coccinelle: Add script to find cases of missing locks
This script finds cases of missing locks in the code.
There is some possibilty of false positives in cases
where a particular function is supposed to exit with
the lock held or there is any preceding function call
that releases the lock.

Suggested-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:32:59 -05:00
Himanshu Jha 6f98ae38a2 coccinelle: irq_lock.cocci: Update script
* Provide a single liner description at top using `///`
  comments. This one liner is displayed when using `--verbose`
  mode of coccicheck.

* Specify Condidence level property adhering to coccinelle.rst
  section "Proposing new semantic patches".

* Add virtual patch rule to handle the patch case when coccicheck
  is supplied with `--mode=patch` option.

* Add `depends on !(file in "ext")` to ignore reports from `ext/`
  directory.

* Simplify rule to use disjunctions and use "exists" to match any
  available control path.

Suggested-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:31:41 -05:00
Himanshu Jha ff5a8a85e1 coccinelle: ignore_return.cocci: Update script
* Provide a single liner description at top using `///`
  comments. This one liner is displayed when using `--verbose`
  mode of coccicheck.

* Specify Condidence level property adhering to coccinelle.rst
  section "Proposing new semantic patches".

* Add virtual patch rule to handle the patch case when coccicheck
  is supplied with `--mode=patch` option.

* Add `depends on !(file in "ext")` to ignore reports from `ext/`
  directory.

* Simplify patch rule to reduce effort in reconstruction since
  logically we are only *adding* void cast.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:30:11 -05:00
Himanshu Jha 6aaba6842b coccinelle: Add script to report cases of NULL dereference
This script finds cases of NULL dereferences where a variable
is dereferenced under a NULL test, even though it is known
to be NULL.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:28:58 -05:00
Himanshu Jha 060414704b coccinelle: unsigned_shift.cocci: Update script
* Use `expression` metavariable instead of `constant` to catch
  missing cases such as:

diff -u -p a/subsys/net/ip/rpl.c b/subsys/net/ip/rpl.c
--- a/subsys/net/ip/rpl.c
+++ b/subsys/net/ip/rpl.c
@@ -686,7 +686,7 @@ static void new_dio_interval(struct net_
 {
 	u32_t time;

-	time = 1 << instance->dio_interval_current;
+	time = BIT(instance->dio_interval_current);

* Provide a single liner description at top using `///`
  comments. This one liner is displayed when using `--verbose`
  mode of coccicheck.

* Specify Condidence level property adhering to coccinelle.rst
  section "Proposing new semantic patches".

* Add virtual patch rule to handle the patch case when coccicheck
  is supplied with `--mode=patch` option.

* Edit patch rule to remove redundant parentheses in the output.

* Add `depends on !(file in "ext")` to ignore reports from `ext/`
  directory.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-11-07 10:27:44 -05:00
Himanshu Jha ab35ebe17d coccinelle: Add script to enforce ARRAY_SIZE macro usage
This script detects cases where ARRAY_SIZE can be used such as
where there is a division of sizeof the array by the sizeof its first
element or by any indexed element or the element type. It replaces the
division of the two sizeofs by ARRAY_SIZE helper macro.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
2018-10-02 14:03:39 -07:00
Flavio Ceolin da49f2e440 coccicnelle: Ignore return of memset
The return of memset is never checked. This patch explicitly ignore
the return to avoid MISRA-C violations.

The only directory excluded directory was ext/* since it contains
only imported code.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-14 16:55:37 -04:00
Flavio Ceolin 8aec087268 kernel: Fix bitwise operators with unsigned operators
Bitwise operators should be used only with unsigned integer operands
because the result os bitwise operations on signed integers are
implementation-defined.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-08-16 19:47:41 -07:00
Flavio Ceolin 0866d18d03 irq: Fix irq_lock api usage
irq_lock returns an unsigned int, though, several places was using
signed int. This commit fix this behaviour.

In order to avoid this error happens again, a coccinelle script was
added and can be used to check violations.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-08-16 19:47:41 -07:00