<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

Linux內核代碼的靜態檢查

冬至子 ? 來源:linux與soc ? 作者:linux與soc ? 2023-06-05 14:50 ? 次閱讀

1.Sparse介紹

Linus在2004年開發了kernel代碼靜態檢查工具,可以檢查出kernel中潛在的風險代碼,Sparse通過 gcc 的擴展屬性__attribute__ 以及自己定義的 __context__來對代碼進行靜態檢查,重點檢查kernel代碼中的變量類型和各類鎖。

那么如何指定代碼可以被Sparse檢查的到呢?以__iomem為例,可以對需要檢查的變量類型放到特定文件中,以__CHECKER__進行限定即可。

#ifdef __CHECKER__
# define __user  __attribute__((noderef, address_space(1)))
# define __kernel __attribute__((address_space(0)))
# define __safe  __attribute__((safe))
# define __force __attribute__((force))
# define __nocast __attribute__((nocast))
# define __iomem __attribute__((noderef, address_space(2)))
# define __must_hold(x) __attribute__((context(x,1,1)))
# define __acquires(x) __attribute__((context(x,0,1)))
# define __releases(x) __attribute__((context(x,1,0)))
# define __acquire(x) __context__(x,1)
# define __release(x) __context__(x,-1)
# define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
# define __percpu __attribute__((noderef, address_space(3)))
# define __rcu  __attribute__((noderef, address_space(4)))
# define __private __attribute__((noderef))
extern void __chk_user_ptr(const volatile void __user *);
extern void __chk_io_ptr(const volatile void __iomem *);
# define ACCESS_PRIVATE(p, member) (*((typeof((p)- >member) __force *) &(p)- >member))
#else /* __CHECKER__ */
# ifdef STRUCTLEAK_PLUGIN
#  define __user __attribute__((user))
# else
#  define __user
# endif
# define __kernel
# define __safe
# define __force
# define __nocast
# define __iomem
# define __chk_user_ptr(x) (void)0
# define __chk_io_ptr(x) (void)0
# define __builtin_warning(x, y...) (1)
# define __must_hold(x)
# define __acquires(x)
# define __releases(x)
# define __acquire(x) (void)0
# define __release(x) (void)0
# define __cond_lock(x,c) (c)
# define __percpu
# define __rcu
# define __private
# define ACCESS_PRIVATE(p, member) ((p)- >member)
#endif /* __CHECKER__ */

2. 安裝Sparse工具

通常來說,開發環境中沒有Sparse工具,需要手動安裝,否則報如下錯誤:

ubuntu16@ubuntu16:linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 C=2
  CHECK   scripts/mod/empty.c
/bin/sh: 1: sparse: not found
scripts/Makefile.build:261: recipe for target 'scripts/mod/empty.o' failed
  1. 獲取Sparse代碼
ubuntu16@ubuntu16:~$ git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git
Cloning into 'sparse'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 20026 (delta 6), reused 0 (delta 0), pack-reused 20009
Receiving objects: 100% (20026/20026), 4.29 MiB | 924.00 KiB/s, done.
Resolving deltas: 100% (14035/14035), done.
Checking connectivity... done.
  1. 編譯Sparse
ubuntu16@ubuntu16:~$ cd sparse/
ubuntu16@ubuntu16:sparse$ make
Makefile:152: Your system does not have libxml, disabling c2xml
Makefile:170: Your system does not have sqlite3, disabling semind
Makefile:192: Your system does not have gtk3/gtk2, disabling test-inspect
Makefile:226: Your system does not have llvm, disabling sparse-llvm
  1. 安裝Sparse
ubuntu16@ubuntu16:sparse$ make install
Makefile:152: Your system does not have libxml, disabling c2xml
Makefile:170: Your system does not have sqlite3, disabling semind
Makefile:192: Your system does not have gtk3/gtk2, disabling test-inspect
Makefile:226: Your system does not have llvm, disabling sparse-llvm
INSTALL /home/ubuntu16/bin/sparse
INSTALL /home/ubuntu16/bin/cgcc
INSTALL /home/ubuntu16/share/man/man1/sparse.1
INSTALL /home/ubuntu16/share/man/man1/cgcc.1
ubuntu16@ubuntu16:sparse$

3.執行Sparse靜態檢查

從kernel頂層Makefile中可以看出,當編譯kernel時通過指定C=1C=2,可以調用到Sparse進行代碼靜態檢查。

192 # Call a source code checker (by default, "sparse") as part of the
193 # C compilation.
194 #
195 # Use 'make C=1' to enable checking of only re-compiled files.
196 # Use 'make C=2' to enable checking of *all* source files, regardless
197 # of whether they are re-compiled or not.
198 #
199 # See the file "Documentation/dev-tools/sparse.rst" for more details,
200 # including where to get the "sparse" utility.

執行結果:

ubuntu16@ubuntu16:linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 C=2
  CHECK   scripts/mod/empty.c
  CALL    scripts/atomic/check-atomics.sh
  CALL    scripts/checksyscalls.sh
  CHECK   arch/arm/vfp/vfpmodule.c
  CHECK   init/main.c
arch/arm/vfp/vfpmodule.c:38:17: warning: symbol 'vfp_vector' was not declared. Should it be static?
arch/arm/vfp/vfpmodule.c:56:17: warning: symbol 'vfp_current_hw_state' was not declared. Should it be static?
arch/arm/vfp/vfpmodule.c:323:6: warning: symbol 'VFP_bounce' was not declared. Should it be static?
arch/arm/vfp/vfpmodule.c:714:6: warning: symbol 'kernel_neon_begin' was not declared. Should it be static?
arch/arm/vfp/vfpmodule.c:745:6: warning: symbol 'kernel_neon_end' was not declared. Should it be static?
...
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • ARM
    ARM
    +關注

    關注

    134

    文章

    8725

    瀏覽量

    363088
  • LINUX內核
    +關注

    關注

    1

    文章

    312

    瀏覽量

    21425
  • gcc編譯器
    +關注

    關注

    0

    文章

    78

    瀏覽量

    3263
收藏 人收藏

    評論

    相關推薦

    linux內核代碼

    linux內核代碼
    發表于 08-20 22:52

    Linux內核代碼

    Linux內核代碼本章講述在L i n u x內核源碼中,應該從何處開始查找特定的內核函數。本書并不要求讀者具有C語言編程能力,也不要求讀
    發表于 02-09 15:24 ?36次下載

    Linux內核代碼漫游

    Linux內核代碼漫游 本章試圖以順序的方式來解釋Linux代碼,以幫助讀者對源代碼的體系
    發表于 02-09 15:27 ?26次下載

    Linux 內核代碼

    Linux 內核代碼 實模式setup階段setup用于體系結構相關的硬件初始化工作,在arch目錄中的各個系統結構的平臺相關都有類似功能的代碼。在32位的x86平臺中,s
    發表于 02-10 13:45 ?28次下載

    嵌入式LINUX內核網絡棧(源代碼)

    本文選擇 LINUX-1.2.13 內核所包含的網絡部分代碼分析(注意網絡部分代碼內核代碼的演
    發表于 05-12 10:39 ?57次下載
    嵌入式<b class='flag-5'>LINUX</b><b class='flag-5'>內核</b>網絡棧(源<b class='flag-5'>代碼</b>)

    Linux內核代碼情景分析(全冊高清帶書簽)

    Linux內核代碼情景分析(全冊高清帶書簽)
    發表于 01-14 15:20 ?52次下載

    Linux內核代碼感悟

    直接訪問校內外的 lxr 網站)的。如果在 windows 下也可以用 source insight。以下的當前路徑為內核代碼路徑,通常為/usr/src/linux。內核版本為 2
    發表于 09-11 17:01 ?18次下載
    <b class='flag-5'>Linux</b><b class='flag-5'>內核</b><b class='flag-5'>代碼</b>感悟

    怎樣去讀Linux內核代碼

    怎樣去讀Linux內核代碼
    發表于 10-25 10:15 ?13次下載
    怎樣去讀<b class='flag-5'>Linux</b><b class='flag-5'>內核</b>源<b class='flag-5'>代碼</b>

    Linux內核代碼情景分析(全冊高清帶書簽)pdf下載

    Linux內核代碼情景分析需要的拿走吧
    發表于 01-04 16:57 ?8次下載

    如何在ZC702板上運行Linux內核代碼

    了解如何獲取Xilinx Linux內核代碼,配置它,構建內核和設備樹,最后在ZC702板上運行新內核。
    的頭像 發表于 11-23 07:09 ?3179次閱讀

    關于Linux內核代碼風格

    從而導致的問題。因為當時代碼量不大,所以解決問題的時間相對較少。在代碼量增大的情況下可以借助工具進行自動修改。 快速修改編碼風格的工具 scripts/checkpatch.pl 這是一個檢查patch是否符合
    的頭像 發表于 04-25 14:50 ?1598次閱讀

    Linux內核代碼60%都是驅動?

    為什么Linux內核代碼60%都是驅動? 如果每支持新的設備就加入驅動,內核會不會變得越來越臃腫?
    的頭像 發表于 07-11 11:48 ?498次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>內核</b><b class='flag-5'>代碼</b>60%都是驅動?

    linux內核代碼詳解

     在安裝好的Linux系統中,內核的源代碼位于/ust/src/linux.如果是從GNU網站下載的Linux
    發表于 09-06 17:01 ?2次下載

    Linux內核如何使用結構體和函數指針?

    我將結合具體的Linux內核驅動框架代碼來展示Linux內核如何使用結構體和函數指針。
    的頭像 發表于 09-06 14:17 ?629次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>內核</b>如何使用結構體和函數指針?

    代碼檢查的方式有三種

    【摘要】?代碼檢查中,提到的編程規范,規則集,規則,規則用例(場景、誤報、檢出)分別代表什么意思呢? 在 SAST 靜態檢查領域,代碼
    的頭像 發表于 02-25 10:08 ?603次閱讀
    <b class='flag-5'>代碼</b><b class='flag-5'>檢查</b>的方式有三種
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>