安全函数过滤使用场景
用户可以通过在数据竞争检查命令devkit advisor dr-check之后,添加-s/--safe-file参数的方式,实现在动态内存一致性检测期间不检测数据竞争的安全函数。
添加-s/--safe-file参数需指定cfg配置文件,文件中写入需要过滤安全函数的名称。若为cpp文件,必须写入重载后的安全函数名称。
安全函数过滤使用示例
本示例通过指定安全函数过滤cfg配置文件,执行数据竞争检查后实现安全函数过滤操作,也可参考安全函数过滤的学习视频。
- 此处以自定义类添加锁为例,请根据实际情况在-s指定的配置文件中添加安全函数。“/home/advisor/test”文件内容:
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
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; }
“/home/advisor/test”:ELF文件路径,请根据实际情况进行修改。
- 执行数据竞争检查。
1
devkit advisor dr-check -f /home/advisor/test
返回信息如下,从返回信息中的加粗信息可以看出有2行代码推荐修改。
Executing data race 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 recommendable code lines to modify. For the details information, please check: /home/DevKit_Software/dr-check_20250510030615_554d.json /home/DevKit_Software/dr-check_20250510030615_554d.html /home/DevKit_Software/dr-check_20250510030615_554d.csv
- 通过nm命令可查看test文件中first_thread和second_thread函数重载后名称。
1
nm test
回显信息如下:
1 2
0000000000400a84 T _Z12first_threadPv 0000000000400ac4 T _Z13second_threadPv
- 创建配置文件safe_functions.cfg,写入内容。
1 2
_Z12first_threadPv _Z13second_threadPv
- 再次执行数据竞争检查。
devkit advisor dr-check -f /home/advisor/test -s /home/testcase/safe_functions.cfg
“/home/testcase/safe_functions.cfg”:cfg配置文件,指定在动态数据竞争检测中不进行检查的安全函数,请根据实际情况进行修改。
返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改,实现安全函数过滤操作。
Executing data race 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 recommendable code lines to modify. For the details information, please check: /home/DevKit_Software/dr-check_20250510031515_663d.json /home/DevKit_Software/dr-check_20250510031515_663d.html /home/DevKit_Software/dr-check_20250510031515_663d.csv
父主题: 数据竞争检查