Rate This Document
Findability
Accuracy
Completeness
Readability

Data Race Check

The data race check function checks for any dynamic memory inconsistency problem when C/C++ source code is running on the Kunpeng platform (specifically Kunpeng 920), and provides the check result and also suggestions on inserting memory barriers.

Data race check can be performed only for single-process applications, and the GCC version must be later than 9.4.0.

Command Function

Dynamically checks for data race in source code.

Syntax

devkit advisor dr-check [-h | --help] {-f ELF_FILE | --elf-file ELF_FILE} [-i INPUT_PATH | --input INPUT_PATH] [-s SAFE_FILE | --safe-file SAFE_FILE][-p ELF_PARAMS | --elf-params ELF_PARAMS] [-r {all,json,html,csv} | --report-type {all,json,html,csv}] [-l {0,1,2,3} | --log-level {0,1,2,3}] [--set-timeout TIMEOUT]

Parameter Description

Table 1 Parameter description

Parameter

Option

Description

-h/--help

-

Obtains help information.

-i/--input

input_path

Path of the source code folder corresponding to the ELF file.

-f/--elf-file

elf_file

ELF file path. This parameter is mandatory. The ELF file to be tested must contain debugging information (add the -g option during compilation).

-s/--safe-file

safe_file

CFG configuration file. It specifies the secure functions that are skipped in dynamic data race check. The name of a C++ function must be the name after the C++ function is reloaded.

-p/--elf-params

elf_parameter

Input parameters required for executing the ELF file.

-o/--output

output_path

Path for storing scan reports. By default, scan reports are stored in the current execution path. A report name is the format of Module name_Timestamp.

-l/--log-level

0/1/2/3

Log level, which defaults to 1.
  • 0: DEBUG
  • 1: INFO
  • 2: WARNING
  • 3: ERROR

-r/--report-type

all/json/html/csv

Scan report format, which defaults to all.
  • all: generates reports in JSON, HTML, and CSV formats.
  • json: generates a report only in JSON format.
  • html: generates a report only in HTML format.
  • csv: generates a report only in CSV format.

--set-timeout

time

Timeout interval of a task, in minutes. If the execution duration exceeds the timeout interval, the task exits. By default, there is no timeout interval. The task will be executed until it is complete.

Example

In this example, the source file in the /home/testcase/project directory is scanned. Replace the example directory with the actual one.

devkit advisor dr-check -i /home/testcase/project -f /home/advisor/test

The following information is displayed and a report is generated:

Scanned time: 2024/07/16 14:01:30
 
Configuration:
    Elf file path: /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_20240716140130_6480.json
    /home/ DevKit_Software /dr-check_20240716140130_6480.html
    /home/ DevKit_Software /dr-check_20240716140130_6480.csv

Example of Secure Functions

To add the -s/--safe-funcs parameter, you need to specify the CFG configuration file. In the configuration file, write the name of a secure function in each line. If the source file is a CPP file, write the name of the overloaded secure function.

The following example describes how to add a lock for a user-defined class. Add secure functions based on your requirements.

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;
}

You can run the nm command to view the names of the reloaded first_thread and second_thread functions.

nm test

0000000000400a84 T _Z12first_threadPv
0000000000400ac4 T _Z13second_threadPv

You can create the safe_functions.cfg file and write the following content to the file:

_Z12first_threadPv
_Z13second_threadPv

In this case, run the following command:

devkit advisor dr-check -i /home/testcase/project -f /home/advisor/test -s /home/testcase/safe_functions.cfg

The following information is displayed and a report is generated:

Executing data race check task, please wait...
Scanned time: 2024/09/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 0 recommendable code lines to modify.

For the details information, please check:
    /home/DevKit_Software/dr-check_20240910030615_554d.json
    /home/DevKit_Software/dr-check_20240910030615_554d.html
    /home/DevKit_Software/dr-check_20240910030615_554d.csv

Output Report

Table 2 Output report parameters

Parameter

Description

Configuration

Displays the paths of the software source file and executable file.

Source File to Be Modified

Displays information such as the path of the source file that needs to be modified.