Filtering Safe Functions
You can add the -s/--safe-file parameter next to the devkit advisor dr-check command to skip dynamic memory consistency check for specified safe functions.
To add the -s/--safe-file parameter, you need to specify the CFG configuration file, and write the names of filtered safe functions in the file. If the source file is a CPP file, write the name of the overloaded safe function.
Example of Filtering Safe Functions
In this example, the CFG configuration file is edited to filter safe functions. After dynamic memory consistency is performed, safe functions are filtered.
- Compile the source file to generate an executable binary file. The following example describes how to add a lock for a user-defined class. Add safe functions to the configuration file specified by -s when needed.
1g++ test.cpp -o test -g -lpthread
The content of the test.cpp file is as follows: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; }
- Perform dynamic memory consistency check. Replace the example ELF file path (/home/advisor/test) with the actual one.
devkit advisor dr-check -f /home/advisor/test
The following information in bold indicates that two lines of code need to be modified.
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 - You can run the nm command to view the names of the reloaded first_thread and second_thread functions in the test file.
1nm test
Command output:
1 2
0000000000400a84 T _Z12first_threadPv 0000000000400ac4 T _Z13second_threadPv
- Create the safe_functions.cfg file and write the following content to the file:
1 2
_Z12first_threadPv _Z13second_threadPv
- Perform dynamic memory consistency check again.
devkit advisor dr-check -f /home/advisor/test -s /home/testcase/safe_functions.cfg
/home/testcase/safe_functions.cfg: This CFG configuration file specifies the safe functions that are not checked during dynamic memory consistency check. Modify the file as required.
The following information in bold indicates that no code lines need to be modified when filtering safe functions.
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