是否存在可同时检测内存安全与数据竞争的算法?相关学术研究问询
结合AddressSanitizer与ThreadSanitizer的研究改进问询
我关注结合AddressSanitizer(ASAN)与ThreadSanitizer(TSAN)的相关问题,现从理论计算机科学视角提出此问询,源于此前的一场讨论。
存在缺陷的C++程序
#include <cstdio> #include <cstdlib> #include <cstring> #include <thread> #include <mutex> static std::mutex m; static char *buf; static void a() { for (;;) { char *b; { std::lock_guard<std::mutex> g{m}; b = buf; } if (b) puts(b); } } static void b() { for (;;) { { std::lock_guard<std::mutex> g{m}; buf = strdup("foo"); } { std::lock_guard<std::mutex> g{m}; buf = static_cast<char*>(realloc(buf, 10000)); strcpy(buf, "barbarbar"); } { std::lock_guard<std::mutex> g{m}; free(buf); buf = nullptr; } } } int main() { auto thread_a = std::thread(a); auto thread_b = std::thread(b); thread_a.join(); // unreachable thread_b.join(); return 0; }
不同工具的测试表现
- 使用GCC 14.2.0或Clang 20并添加
-fsanitize=address编译时,程序会立即因heap-use-after-free错误终止,原因是a()使用了指针的过期副本。 - 未添加特殊选项编译后用
valgrind ./a.out运行时,在我的系统中程序需运行近4分钟才会首次报告Invalid read of size 1。这是因为Valgrind通过单线程抢占式调度模拟多线程,因此需要恰好的时机在a()与b()之间切换上下文才能复现错误。 - 类似地,使用GCC或Clang添加
-fsanitize=thread编译后,程序似乎能正常运行而无任何问题。
问题
是否有近期学术研究可在该方面改进ThreadSanitizer算法?
内容的提问来源于stack exchange,提问作者Marko Mäkelä
相关产品推荐
相关产品推荐

