鲲鹏社区首页
中文
注册
开发者
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

安全函数过滤使用场景

用户可以通过在内存一致性动态检查命令devkit advisor dr-check之后,添加-s/--safe-file参数的方式,实现在动态内存一致性检测期间不检测内存动态一致性的安全函数。

添加-s/--safe-file参数需指定CFG配置文件,文件中写入需要过滤安全函数的名称。若为cpp文件,必须写入重载后的安全函数名称。

安全函数过滤使用示例

本示例通过指定安全函数过滤CFG配置文件,执行内存一致性动态检查后实现安全函数过滤操作,也可参考安全函数过滤的学习视频

  1. 编译源码文件,生成可执行二进制文件。此处以自定义类添加锁为例,请根据实际情况在-s指定的配置文件中添加安全函数。
    1
    g++ test.cpp -o test -g -lpthread
    
    “test.cpp”文件内容如下:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #include <atomic>
    #include <iostream>
    #include <pthread.h>
    
    class my_custom_spinlock{
        std::atomic_flag is_locked ={false};
    
    public:
      void lock(){
          while (is_locked.test_and_set(std::memory_order_acquire)){}
      }
    
      void unlock (){
          is_locked.clear (std::memory_order_release);
      }
    };
    
    int data_race = 0;
    
    my_custom_spinlock spinlock;
    
    void* first_thread(void* arg){
        spinlock.lock();
        data_race = 5;
        spinlock.unlock();
        return 0;
    }
    void* second_thread(void* arg){
        spinlock.lock();
        data_race = 3;
    
        spinlock.unlock();
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t thr1, thr2;
        pthread_create(&thr1, NULL, first_thread, NULL);
        pthread_create(&thr2, NULL, second_thread, NULL);
        pthread_join(thr1, NULL);
        pthread_join(thr2, NULL);
        for(int i = 1; i <argc; ++i){
            std::cout<< argv[i] << std::endl;
        }
    
        std::cout<< data_race << std::endl;
        return 0;
    }
    
  2. 执行内存一致性动态检查。“/home/advisor/test”为ELF文件路径,请根据实际情况进行修改。
    devkit advisor dr-check -f /home/advisor/test

    返回信息如下,从返回信息中的加粗信息可以看出有2行代码推荐修改。

    Executing dynamic memory consistency check task, please wait...
    The pid of the memtracer process is 1362149
    Scanned time: 2025/05/10 03:06:15
    
    Configuration:
        ELF filepath: /home/advisor/test
        Scan source code path: /home/testcase/project
        Generate report path: /home/DevKit_Software
        Generate report type: all
        Task Timeout Interval: The timeout period is not set.
        Log level: info
    
    Summary:
    There are 2 recommended code lines to modify.
    
    For the details information on multi-thread races, please check:
        /home/DevKit_Software/dr-check_test_20250510030615_554d.json
        /home/DevKit_Software/dr-check_test_20250510030615_554d.html
        /home/DevKit_Software/dr-check_test_20250510030615_554d.csv
  3. 通过nm命令可查看test文件中first_thread和second_thread函数重载后名称。
    1
    nm test
    

    回显信息如下:

    1
    2
    0000000000400a84 T _Z12first_threadPv
    0000000000400ac4 T _Z13second_threadPv
    
  4. 创建配置文件safe_functions.cfg,写入内容。
    1
    2
    _Z12first_threadPv
    _Z13second_threadPv
    
  5. 再次执行内存一致性动态检查。
    devkit advisor dr-check -f /home/advisor/test -s /home/testcase/safe_functions.cfg

    “/home/testcase/safe_functions.cfg”:cfg配置文件,指定在内存动态一致性检测中不进行检查的安全函数,请根据实际情况进行修改。

    返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改,实现安全函数过滤操作。

    Executing dynamic memory consistency check task, please wait...
    The pid of the memtracer process is 1362149
    Scanned time: 2025/05/10 03:15:15
    
    Configuration:
        ELF filepath: /home/advisor/test
        Scan source code path: /home/testcase/project
        Generate report path: /home/DevKit_Software
        Generate report type: all
        Task Timeout Interval: The timeout period is not set.
        Log level: info
    
    Summary:
    There are 0 recommended code lines to modify.
    
    For the details information on multi-thread races, please check:
        /home/DevKit_Software/dr-check_test_20250510031515_663d.json
        /home/DevKit_Software/dr-check_test_20250510031515_663d.html
        /home/DevKit_Software/dr-check_test_20250510031515_663d.csv