如何检查运行时是否启用了消毒剂?

问题描述 投票:0回答:1

我有单元测试来验证各种事情。例如,它检查我是否可以向进程发送

SIGUSR1
,并且进程在收到该信号时执行预期的操作。

这按预期工作。那里几乎没有问题,除了当使用消毒剂编译时,其他一些函数(在我的例子中是

ppoll()
)收到中断。这是因为当我创建线程时,我关闭了许多信号。其中一些是 sanitizer 和/或 catch2 正在使用的。我需要的是我的较低级别的库不要弄乱这些信号。

以下是我发现有效(未评论)并导致问题(已评论)的列表。

SIGKILL
不是问题,但尝试忽略该信号确实没有意义。编号为 32 和 33 的信号在内部使用,如果您不想破坏 pthread 实现(至少在 Linux 下),则不能对其进行修改。

// SIGHUP, -- not compatible with catch2 and/or sanitizer
// SIGINT, -- not compatible with catch2 and/or sanitizer
SIGQUIT,
// SIGILL, -- not compatible with catch2 and/or sanitizer
SIGTRAP,
// SIGABRT, -- not compatible with catch2 and/or sanitizer
SIGBUS,
// SIGFPE, -- not compatible with catch2 and/or sanitizer
// SIGKILL, -- cannot be ignored
SIGUSR1,
// SIGSEGV, -- not compatible with catch2 and/or sanitizer
SIGUSR2,
SIGPIPE,
SIGALRM,
// SIGTERM, -- not compatible with catch2 and/or sanitizer
SIGSTKFLT,
SIGCHLD,
// SIGCONT, -- cannot be ignored
// SIGSTOP, -- cannot be ignored
//SIGTSTP, -- not compatible with catch2 and/or sanitizer
SIGTTIN,
SIGTTOU,
SIGURG,
SIGXCPU,
SIGXFSZ,
SIGVTALRM,
// SIGPROF, -- not compatible with catch2 and/or sanitizer
SIGWINCH,
// SIGIO, -- not compatible with catch2 and/or sanitizer
SIGPWR,
SIGSYS,
// 32 -- used by pthread implementation
// 33 -- used by pthread implementation
SIGRTMIN,
SIGRTMIN+1,
SIGRTMIN+2,
SIGRTMIN+3,
SIGRTMIN+4,
SIGRTMIN+5,
SIGRTMIN+6,
SIGRTMIN+7,
SIGRTMIN+8,
SIGRTMIN+9,
SIGRTMIN+10,
SIGRTMIN+11,
SIGRTMIN+12,
SIGRTMIN+13,
SIGRTMIN+14,
SIGRTMIN+15,
SIGRTMAX-14,
SIGRTMAX-13,
SIGRTMAX-12,
SIGRTMAX-11,
SIGRTMAX-10,
SIGRTMAX-9,
SIGRTMAX-8,
SIGRTMAX-7,
SIGRTMAX-6,
SIGRTMAX-5,
SIGRTMAX-4,
SIGRTMAX-3,
SIGRTMAX-2,
SIGRTMAX-1,
SIGRTMAX,

我希望仅当单元测试与消毒剂一起运行时,才从列表中删除标记为“与 catch2 和/或消毒剂不兼容”的信号。但是,当我编译处理这些信号的库时,它本身可能不会使用清理程序进行编译。因此,当我链接到它时,目前它不知道我们是否有消毒剂。我发现的唯一方法是在编译时使用

#ifdef
等:

#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
... code when sanitizer is activated in this process ...
#endif

我需要的是在我的代码中进行测试,例如:

if(sanitizer_is_running())
{
    ...remove breaking signals from my list...
}

你知道如何实现这个必须在运行时工作的

sanitizer_is_running()
函数吗?

linux g++ runtime address-sanitizer
1个回答
0
投票

我找到了一种可行的方法,但我认为它相当丑陋。希望有人能想出一个更清洁的解决方案。

事实是,如果链接到 sanitizer 库,则可以访问库中定义的函数。这意味着我们可以搜索这些符号之一,如果存在,我们就知道我们正在使用消毒剂运行。

#include <dlfcn.h>

...

if(dlsym(RTLD_DEFAULT, "__lsan_enable") != nullptr)
{
    // ... here we know that the sanitizer is active ...
}

我进行了测试,它可以在我的环境(即 Linux 上)中运行。它也可以在其他 Unix 系统上运行。

© www.soinside.com 2019 - 2024. All rights reserved.