请输入您要查询的百科知识:

 

词条 Sparse
释义

  1. Annotations

     Linux kernel definitions  Examples 

  2. See also

  3. References

  4. Further reading

  5. External links

{{cleanup HTML|date=February 2019}}{{about|a software static analysis tool}}{{Infobox software
| name = Sparse
| author = Linus Torvalds
| developer = Josh Triplett, Christopher Li, Luc Van Oostenryck
| released = 2003
| latest release version = 0.6.0
| latest release date = {{Start date and age|2018|12|26}}[1]
| programming language = C
| operating system = Linux, BSD, OS X, MinGW, Cygwin
| genre = Static code analysis
| license = MIT License
| website = {{URL|sparse.wiki.kernel.org}}
}}

Sparse is a computer software tool designed to find possible coding faults in the Linux kernel.[2] Unlike other such tools, this static analysis tool was initially designed to only flag constructs that were likely to be of interest to kernel developers, such as the mixing of pointers to user and kernel address spaces.

Sparse checks for known problems and allows the developer to include annotations in the code that convey information about data types, such as the address space that pointers point to and the locks that a function acquires or releases.

Linus Torvalds started writing Sparse in 2003. Josh Triplett was its maintainer from 2006, a role taken over by Christopher Li in 2009[3]

and by Luc Van Oostenryck in November 2018[4].

Sparse is released under the MIT License.

Annotations

Some of the checks performed by Sparse require annotating the source code using the __attribute__ GCC extension, or the Sparse-specific __context__ specifier.[5] Sparse defines the following list of attributes:

  • address_space(num)
  • bitwise
  • force
  • context(expression,in_context,out_context)

When an API is defined with a macro, the specifier __attribute__((context(...))) can be replaced by __context__(...).

Linux kernel definitions

The Linux kernel defines the following short forms as pre-processor macros in files linux/compiler.h and linux/types.h (when building without the __CHECKER__ flag, all these annotations are removed from the code):

  1. ifdef __CHECKER__
  2. define __user __attribute__((noderef, address_space(1)))
  3. define __kernel __attribute__((address_space(0)))
  4. define __safe __attribute__((safe))
  5. define __force __attribute__((force))
  6. define __nocast __attribute__((nocast))
  7. define __iomem __attribute__((noderef, address_space(2)))
  8. define __must_hold(x) __attribute__((context(x,1,1)))
  9. define __acquires(x) __attribute__((context(x,0,1)))
  10. define __releases(x) __attribute__((context(x,1,0)))
  11. define __acquire(x) __context__(x,1)
  12. define __release(x) __context__(x,-1)
  13. define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
  14. define __percpu __attribute__((noderef, address_space(3)))
  15. ifdef CONFIG_SPARSE_RCU_POINTER
  16. define __rcu __attribute__((noderef, address_space(4)))
  17. else
  18. define __rcu
  19. endif

extern void __chk_user_ptr(const volatile void __user *);

extern void __chk_io_ptr(const volatile void __iomem *);

  1. else
  2. define __user
  3. define __kernel
  4. define __safe
  5. define __force
  6. define __nocast
  7. define __iomem
  8. define __chk_user_ptr(x) (void)0
  9. define __chk_io_ptr(x) (void)0
  10. define __builtin_warning(x, y...) (1)
  11. define __must_hold(x)
  12. define __acquires(x)
  13. define __releases(x)
  14. define __acquire(x) (void)0
  15. define __release(x) (void)0
  16. define __cond_lock(x,c) (c)
  17. define __percpu
  18. define __rcu
  19. endif
  1. ifdef __CHECKER__
  2. define __bitwise __attribute__((bitwise))
  3. else
  4. define __bitwise
  5. endif

Examples

The types __le32 and __be32 represent 32-bit integer types with different endianness. However, the C language does not allow to specify that variables of these types should not be mixed. The bitwise attribute is used to mark these types as restricted, so Sparse will give a warning if variables of these types or other integer variables are mixed:

typedef __u32 __bitwise __le32;

typedef __u32 __bitwise __be32;

To mark valid conversions between restricted types, a casting with the force attribute is used to avoid Sparse giving a warning.

See also

{{Portal|Free and open-source software}}
  • List of tools for static code analysis

References

1. ^{{cite mailing list | title = Sparse 0.6.0 | author = Luc Van Oostenryck | date = 2018-12-26 | mailinglist = linux-sparse@vger.kernel.org | url = https://marc.info/?l=linux-sparse&m=154581090608347 | accessdate = 2018-12-26}}
2. ^{{cite conference | title = Semantic patches for documenting and automating collateral evolutions in Linux device drivers |author1=Yoann Padioleau |author2=René Rydhof Hansen |author3=Julia L. Lawall |author4=Gilles Muller | year = 2006 | conference = Proceedings of the 3rd workshop on Programming languages and operating systems: linguistic support for modern operating systems | isbn = 1-59593-577-0 | doi = 10.1145/1215995.1216005 | url = http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.122.7080&rep=rep1&type=pdf | quote = The Linux community has recently begun using various tools to better analyze C code. Sparse is a library that, like a compiler front end, provides convenient access to the abstract syntax tree and typing information of a C program. | accessdate = 2010-11-06}}
3. ^{{cite mailing list | title = Sparse 0.4.2 released | author = Christopher Li | date = 2009-10-16 | mailinglist = linux-sparse | url = http://marc.info/?l=linux-sparse&m=125570805231414&w=2 | accessdate = 2010-11-06}}
4. ^{{Citation| url = https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/MAINTAINERS?id=4e962ff6e34f44c400c548da0c1e2393053a691e| title = change Sparse's maintainer| accessdate = December 10, 2018| ref = none}}
5. ^{{cite web | url = https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html | title = Attribute Syntax — Using the GNU Compiler Collection (GCC) | publisher = Free Software Foundation | accessdate = 2010-11-13}}

Further reading

  • {{cite web

| title = Finding kernel problems automatically
| author = Jonathan Corbet
| date = 2004-06-01
| work = LWN.net
| url = https://lwn.net/Articles/87538/
| accessdate = 2010-11-06
}}
  • {{cite web

| title = Linus & the Lunatics, Part I
| author = Doc Searls
| date = 2003-11-24
| work = Linux Journal
| url = http://www.linuxjournal.com/article/7272
| accessdate = 2010-11-06
}}
  • {{cite conference

| title = Putting LTP to test—Validating both the Linux kernel and Test-cases
|author1=Subrata Modak |author2=Balbir Singh |author3=Yamato Masatake | year = 2009
| conference = Ottawa Linux Symposium 2009
| conferenceurl = https://www.kernel.org/doc/ols/2009/
| pages = 209–220
| url = https://www.kernel.org/doc/ols/2009/ols2009-pages-209-220.pdf
| accessdate = 2010-11-07
}}
  • {{cite thesis

| degree = M.Sc.
| title = Detection of Static Flaws in Changesets
| author = Daniel De Graaf
| year = 2010
| publisher = Iowa State University
| location = Ames, Iowa
| accessdate = 2010-11-07
| url = http://archives.ece.iastate.edu/archive/00000566/01/thesis.pdf
| oclc = 665146513
}}

External links

  • {{Official website|sparse.wiki.kernel.org}}
  • [https://www.kernel.org/doc/html/latest/dev-tools/sparse.html Using sparse for typechecking], Linux Kernel Documentation
  • {{man|1|sparse|Linux|Semantic Parser for C}}
  • {{man|1|cgcc|Linux|Compiler wrapper to run Sparse after compiling}}

3 : Static program analysis tools|Free software testing tools|Software using the MIT license

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 1:30:48