How to use Clang Static Analyzer,
IntroductionClang
In a macro sense, Clang is a project name. Like GCC, Clang is a lightweight compiler for C, C ++, and Objective C. It is part of the Clang project.
Compared with GCC, Clang compilation is faster and takes less memory. Clang's error message and warning information are more accurate and clearer than GCC. In addition, Clang is a library-based modular design that facilitates IDE integration and complies with the llvm bsd Protocol.
Clang Static Analyzer
Clang Static Analyzer is a source code analysis tool that can search for C, C ++, and Objective-C vulnerabilities.
Currently, Clang Static Analyzer can be used as an independent tool or run in the Xcode Development Environment (Mac OS. Clang Static Analyzer, as an independent tool, can be started from a command line (such as a ubuntu terminal) and run in the process of building a code base.
As part of the Clang project, Clang Static Analyzer is a open source software. Just like the Clang compiler, Clang Static Analyzer can be integrated into other applications like a C ++ library.
Scan-build & scan-view
Scan-build is a command line tool that helps users run static analyzer to check source code so that they can build normally. Static analyzer and code compilation do not affect each other and are executed simultaneously. That is, when a project is under construction, the source code is analyzed by the static analyzer and the source code vulnerability is found. For example:
$ scan-build make
When the build is complete, the result is displayed as a web page to the user. Similar to scan-build, scan-view is also a command line tool. It can open the web page generated by scan-build and display the bug report.
How to install
The methods for installing Clang on different platforms are different: the installation method for Mac OS x is mac OS CLANG. This article introduces the installation method for Ubuntu, other platforms except Mac OS X are similar to ubuntu.
Apt-get Installation
You can directly install the SDK using apt-get. You only need to enter the following in the terminal:
$ sudo apt-get install clang
This method is applicable to users who do not have high version requirements and has the advantage of being simple and fast.
Source code Installation
If you have high requirements on the Clang version, you need to manually compile and install the Clang source code, which is also recommended on the official website. The advantage of this method is that you can get the latest version, but it is time-consuming and troublesome.
- Compile Clang
- Adjust the scan-build and scan-view directories
Make install from the official website does not install the scan-build and scan-view tools. After compilation, the compiled scan-build and scan-view are displayed in $ (SRCDIR)/tools/clang/tools/scan-build and $ (SRCDIR) /tools/clang/tools/scan-view ($ (SRCDIR) is the llvm path to download ). To prevent accidental deletion of these two directories, these two directories are usually put under the root directory (for example, put under/usr/share/clang), if the current directory is under the llvm directory.$ sudo mkdir /usr/share/clang/scan-build -p$ sudo cp ./tools/clang/tools/scan-build/* /usr/share/clang/scan-build/$ sudo mkdir /usr/share/clang/scan-view -p$ sudo cp -r ./tools/clang/tools/scan-view/* /usr/share/clang/scan-view/$ cd /usr/bin$ sudo ln -s ../share/clang/scan-build/scan-build$ sudo ln -s ../share/clang/scan-view/scan-view
Now, the scan-build and scan-view tools can be used, but since the source code script does not specify the clang path, the clang path must be written every time you use the scan-build tool. To solve this problem, you can modify the scan-build script.sudo vi /usr/bin/scan-build# Find 'clang'if (!defined $AnalyzerDiscoveryMethod) {- - $Clang = Cwd::realpath("$RealBin/bin/clang");+ $Clang = Cwd::realpath("/usr/bin/clang"); if (!defined $Clang || ! -x $Clang) { $Clang = Cwd::realpath("$RealBin/clang"); }
That is, change the first $ RealBin to/usr. In this way, the Clang location is specified to scan-build. Clang Static Analyzer is fully installed.
Standard Operation ProcedureClang Compiler
Similar to GCC, Clang also integrates the precompiler, assembler, and linker, and uses the following syntax format:
clang [options] <inputs>
Most of the compilation options are similar to those of GCC. For example,-E only performs pre-compilation,-S generates assembly language files (ended with. s), and-c options only compile without linking. For more information about the options, see man clang or clang-help. For details about the Clang compiler user manual, see clang compiler user's MANUAL.
Unlike GCC, you can use the -- analyze option to start a static analyzer, as shown in figure
$ clang --analyze test.c
When running this command, Clang Static Analyzer is started to analyze bugs in test. c.
Scan-build and scan-view
Checkers
Clang Static Analyzer uses different checker to detect different types of bugs in the source code. The static analyzer uses 6 types of checkers (default checker) by default ):
- Core Checkers: provides some general checks, such as whether to be divided by 0, whether to use a null pointer, and using uninitialized parameters.
- C ++ Checkers: Provides C ++ checks.
- Dead Code Checkers: Check for unused Code.
- OS X Checkers: checks the use of Objective-C and Apple's SDKs.
- Security Checkers: checks the use of insecure APIs and CERT-based Secure Coding Standards.
- Unix Checkers: checks the usage of Unix and POSIX APIs.
Among them, each type of checkers contains different checker to check bugs of different subdivision types. For example, a checker in the core checkers class: core. CallAndMessage. This checker is mainly responsible for checking the logic errors or information expression errors of function calls, such as uninitialized parameters and empty function pointers. For example:
// Cstruct S { int x;};void f(struct S s);void test() { struct S s; f(s); // warn: passed-by-value arg contain uninitialized data}
You can use the -- help-checkers option to view the default checkers list. -Enable-checker and-disable-checker are used to use and disable checker, for example
$ scan-build --help-checkers$ scan-build -enable-checker core.CallAndMessage gcc test.c$ scan-build -disable-checker core.CallAndMessage gcc test.c
If a checker is not prohibited, scan-build automatically uses the default checkers. For more information about The checkers function, see "The functions of default checkers" or available checkers.
The flow chart of scan-build & ccc-analyzer
Scan-build is a perl script used to start the static analyzer. Ccc-analyzer, like scan-build, is also a perl script. The ccc-analyzer script is an intermediate file, which is called by scan-build without direct access.
Scan-build flow chart
The preceding part is the first half of the scan-build script execution and is used to process scan-build option.
In the second part, scan-build mainly processes command and command option.
Scan-build summary:
Ccc-analyzer flow chart
Summary of ccc-analyzer script process:
How to use scan-build to find bugs to analyze a code using uninitialized function pointers analysis MIPS architecture private appsThe functions of default checkers in linux bit
In Clang Static Analyzer, default checkers has six types: Core Checkers, C ++ Checkers, Dead Code Checkers, OS X Checkers, Security Checkers, and Unix Checkers. The functions of each checker in five categories except OS X Checkers are described below.
Note: