Introduction
When investigating some memory issues (wild pointers, out-of-bounds access, etc.), you can use the AddressSanitizer (ASan) tool.
Compared with switching to the Stomp allocator, ASan runs at the cost of roughly halving performance, and can still catch many issues.
Performance benchmark reference: AddressSanitizerPerformanceNumbers · google/sanitizers Wiki
Using DS on Linux
Basic workflow
Considering the impact of enabling ASan on the workflow and performance, you generally need to deploy some separate sacrificial environments.
- Add the
-EnableAsan flag after the existing packaging arguments to enable ASan-related compile flags - At startup, set the
ASAN_OPTIONS environment variable to control ASan-related configuration - Add the
-ansimalloc flag after the original startup arguments - When an exception occurs, check the output from
stderr - Optionally, configure
log_path to specify the output file used on exceptions
Obtaining stack traces
Specifying ASAN_SYMBOLIZER_PATH makes the output include stack traces via llvm-symbolizer; compared with addr2line, this can provide a significant performance improvement.
See: AddressSanitizerCallStack · google/sanitizers Wiki
This makes the log output a few seconds slower, which seems acceptable.
You can use a format string to set stack_trace_format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // %% - represents a '%' character;
// %n - frame number (copy of frame_no);
// %p - PC in hex format;
// %m - path to module (binary or shared object);
// %o - offset in the module in hex format;
// %f - function name;
// %q - offset in the function in hex format (*if available*);
// %s - path to source file;
// %l - line in the source file;
// %c - column in the source file;
// %F - if function is known to be <foo>, prints "in <foo>", possibly
// followed by the offset in this function, but only if source file
// is unknown;
// %S - prints file/line/column information;
// %L - prints location information: file/line/column, if it is known, or
// module+offset if it is known, or (<unknown module>) string.
// %M - prints module basename and offset, if it is known, or PC.
|
However, in testing it seemed unable to obtain function line numbers; I judged this to be an llvm issue, while addr2line works.
Under windows, using llvm-symbolizer.exe also succeeds.
Also, in testing, under windows the MinGW version of addr2line can also obtain stack traces, but it is unacceptably slow.
Recommended configuration
Enable stack trace output and set the log path:
1
| export ASAN_OPTIONS="symbolize=1:print_stacktrace=1:log_path=/home/crash/ds-${版本}-asan-crash.log"
|
Issue records
Slow loading after enabling ASan
Check whether fast_unwind_on_malloc is set to 0; if fast_unwind_on_malloc is disabled, performance will be greatly affected.
See: Address Sanitizer | Android NDK | Android Developers
Stack trace restoration
Wrote a golang script for quickly analyzing stack traces:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
| package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func convertStackToAddr2line(inputFile, outputFile, binaryPath string) error {
infile, err := os.Open(inputFile)
if err != nil {
return fmt.Errorf("无法打开输入文件: %v", err)
}
defer func(infile *os.File) {
err := infile.Close()
if err != nil {
fmt.Printf("关闭输入文件时出错: %v\n", err)
}
}(infile)
outfile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("无法创建输出文件: %v", err)
}
defer func(outfile *os.File) {
err := outfile.Close()
if err != nil {
fmt.Printf("关闭输出文件时出错: %v\n", err)
}
}(outfile)
scanner := bufio.NewScanner(infile)
writer := bufio.NewWriter(outfile)
defer func(writer *bufio.Writer) {
err := writer.Flush()
if err != nil {
fmt.Printf("刷新输出文件时出错: %v\n", err)
}
}(writer)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
parts := strings.Fields(line)
var address string
for _, part := range parts {
if strings.HasPrefix(part, "0x") {
address = part
break
}
}
if address != "" {
cmd := exec.Command("addr2line", "-e", binaryPath, address)
output, err := cmd.Output()
if err != nil {
_, err := fmt.Fprintf(os.Stderr, "执行 addr2line 失败: %v\n", err)
if err != nil {
return err
}
continue
}
s := strings.TrimSpace(string(output))
_, err = writer.WriteString(s + "\n")
if err != nil {
return err
}
fmt.Println(s)
}
} else {
_, err := writer.WriteString(line + "\n")
if err != nil {
return err
}
fmt.Println(line)
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("读取文件时出错: %v", err)
}
fmt.Printf("转换完成,结果已保存到 %s\n", outputFile)
return nil
}
func main() {
if len(os.Args) != 4 {
fmt.Println("Usage: go run main.go <input_file> <output_file> <binary_path>")
os.Exit(1)
}
inputFile := os.Args[1]
outputFile := os.Args[2]
binaryPath := os.Args[3]
if err := convertStackToAddr2line(inputFile, outputFile, binaryPath); err != nil {
_, err := fmt.Fprintf(os.Stderr, "错误: %v\n", err)
if err != nil {
return
}
os.Exit(1)
}
}
|
References