Rate This Document
Findability
Accuracy
Completeness
Readability

Enabling KASAN

Kernel Address Sanitize (KASAN) is a dynamic memory error detector. Use this tool to detect memory overwrites and the use of released memory. Enabling KASAN helps quickly locate the problem if a memory problem occurs. However, after KASAN is enabled, problems that are difficult to reproduce may occur.

The kernel integrates KASAN. Enable the KASAN option and recompile the kernel so that related functions can be used. KASAN is introduced in Linux kernel 4.0. Therefore, the selected kernel version must be 4.0 or later. In addition, GCC 4.9.2 is required to support the basic KASAN function, and GCC 5.0 or later is required to support more KASAN functions. The following steps use CentOS 7.6 on the Kunpeng platform as an example:

  1. Download the RPM package kernel-alt-xxx.src.rpm.
  2. Install the dependencies.
    yum install gcc net-tools openssl-devel python-devel git bison java-devel numactl-devel ncurses-devel xmlto  asciidoc newt-devel pciutils-devel bc 
    yum install rpm-build redhat-rpm-config asciidoc hmaccalc perl-ExtUtils-Embed pesign xmlto 
    yum install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel 
    yum install ncurses-devel newt-devel numactl-devel pciutils-devel python-devel zlib-devel
  3. Update GCC.
    yum install -y centos-release-scl centos-release-scl-rh
    yum install -y devtoolset-8-gcc devtoolset-8-gcc-c++
    source scl_source enable devtoolset-8
  4. Ensure that the mockbuild user and the group to which the mockbuild user belongs are valid.
    1
    2
    groupadd mockbuild
    useradd mockbuild -g mockbuild
    
  5. Install the RPM package.
    1
    rpm –ivh kernel-alt-xxx.src.rpm
    

    After the installation is complete, the RPM build project is automatically deployed in /root/rpmbuild/SPECS and /root/rpmbuild/SOURCES.

  6. Build rpmbuild.
    1
    2
    cd /root/rpmbuild/SPECS
    rpmbuild -bp --target=$(uname -m) kernel-alt.spec
    
  7. Compile the .config configuration file. Replace the example file path and file name with the actual ones.
    1
    2
    cd /root/rpmbuild/BUILD/kernel-alt-xxx/linux-xxx.aarch64
    make menuconfig
    

    On the menuconfig screen, choose Kernel hacking > Memory Debugging > KASan: runtime memory debugger, select KASan: runtime memory debugger, change Instrumentation type to Inline instrumentation, save the file, and exit.

  8. Compile the kernel. Replace the example file path and file name with the actual ones.
    1
    2
    3
    4
    cd /root/rpmbuild/BUILD/kernel-alt-xxx/linux-xxx.aarch64
    make -j
    make modules_install
    make install
    
  9. Restart the system.
    1
    reboot
    

    After the restart is complete, select the corresponding kernel. The kernel startup takes 5 to 15 minutes.