惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
S
Secure Thoughts
A
Arctic Wolf
V
Vulnerabilities – Threatpost
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Scott Helme
Scott Helme
L
LINUX DO - 最新话题
L
LangChain Blog
量子位
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
W
WeLiveSecurity
Last Week in AI
Last Week in AI
美团技术团队
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
V2EX - 技术
V2EX - 技术
雷峰网
雷峰网
Security Latest
Security Latest
小众软件
小众软件
J
Java Code Geeks
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Spread Privacy
Spread Privacy
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
The Blog of Author Tim Ferriss
M
MIT News - Artificial intelligence

博客园 - CoderZh

[公告]这里的博客将不再更新,最新博客请移步至blog.coderzh.com 使用Jekyll官方的ReadMore摘要功能 小记:《技术进步引发的灵感革命》网易游戏学院第二届公开日 强制链接静态库所有符号(包括未被使用的) 开启Github之旅 Cocos2d-x3.6 Android编译问题 读《程序员的思维训练》 最近的一些变化 成大事者不纠结 读《从0到1》 OpenWrt资料汇总 C++开源代码覆盖率工具OpenCppCoverage介绍(Windows) Facebook网络模拟测试工具ATC使用 为什么心跳包(HeartBeat)是必须的? DigitalOcean上使用Tornado+MongoDB+Nginx+Supervisor+DnsPod快速搭建个人博客 创业三年来的一些感想 - 创业篇1 创业三年来的一些感想 - 游戏篇 ViEmuVS2013-3.2.1 破解 从CEGUI源码看代码规范
使用ThreadSanitizer线程检查工具
CoderZh · 2015-08-15 · via 博客园 - CoderZh

ThreadSanitizer又叫TSan,是一个检查线程Data Race的C/C++工具。它集成在新版的gcc和clang中,通过编译时加-fsanitize=thread,可以在运行时检测出Data Race的问题。

ThreadSanitizer官网:https://code.google.com/p/thread-sanitizer

Data Race

Data Race是指多个线程在没有正确加锁的情况下,同时访问同一块数据,并且至少有一个线程是写操作,对数据的读取和修改产生了竞争,从而导致各种不可预计的问题。

Data Race的问题非常难查,Data Race一旦发生,结果是不可预期的,也许直接就Crash了,也许导致执行流程错乱了,也许把内存破坏导致之后某个时刻突然Crash了。

环境要求

  1. Linux x86_64,内核版本不要太旧。(经测试,公司旧的开发机Linux内核是2.6.16是跑不了的,新的tlinux内核3.10.0可以)
  2. gcc 4.8版本以上(clang也集成了,3.2版本以上)

官方示例

$ cat simple_race.cc
#include <pthread.h>
#include <stdio.h>
 
int Global;
 
void *Thread1(void *x) {
  Global++;
  return NULL;
}
 
void *Thread2(void *x) {
  Global--;
  return NULL;
}
 
int main() {
  pthread_t t[2];
  pthread_create(&t[0], NULL, Thread1, NULL);
  pthread_create(&t[1], NULL, Thread2, NULL);
  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);
}

上面的代码在不加锁的情况下,两个线程同时去修改Global变量,从而导致Data Race。使用gcc的-fsanitize=thread 编译,执行

$ g++ simple_race.cc -fsanitize=thread -fPIE -pie -g
$ ./a.out
==================
WARNING: ThreadSanitizer: data race (pid=26327)
  Write of size 4 at 0x7f89554701d0 by thread T1:
    #0 Thread1(void*) simple_race.cc:8 (exe+0x000000006e66)
 
  Previous write of size 4 at 0x7f89554701d0 by thread T2:
    #0 Thread2(void*) simple_race.cc:13 (exe+0x000000006ed6)
 
  Thread T1 (tid=26328, running) created at:
    #0 pthread_create tsan_interceptors.cc:683 (exe+0x00000001108b)
    #1 main simple_race.cc:19 (exe+0x000000006f39)
 
  Thread T2 (tid=26329, running) created at:
    #0 pthread_create tsan_interceptors.cc:683 (exe+0x00000001108b)
    #1 main simple_race.cc:20 (exe+0x000000006f63)
==================
ThreadSanitizer: reported 1 warnings

执行程序,如果发生Data Race,错误信息会直接输出出来。如果错误信息比较多,重定向输出流到文件里,慢慢分析:

$ ./a.out >result.txt 2>&1

关键要点

  1. 除了加-fsanitize=thread外,一定要加-fPIE -pie。
  2. -g 是为了能显示文件名和行号。
  3. 如果分生成obj(-c)和link两个步骤,每一步都加:thread -fPIE -pie -g,并且在link的时候加-ltsan
  4. 只支持64位,最好指定编译64位(-m64)
  5. 如果依赖其他静态库,其他静态库编译时必须指定-fPIC(如果不是请重编)