可以使用-fsanitize = address运行MEX文件吗? [英] Can MEX files be run with -fsanitize=address?

查看:90
本文介绍了可以使用-fsanitize = address运行MEX文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用G ++正常编译的MEX文件.

I have a MEX file compiled normally with g++.

我最近将其编译版本更改为使用clang ++,并在 LDFLAGS CFLAGS 中都包含了 -fsanitize = address (注意:无CXX该项目存在标志).

I recently changed its compilation to use clang++, and included -fsanitize=address into both the LDFLAGS and CFLAGS (note: no CXX flags exist for this project).

但是,尽管如此,一旦我进入MATLAB并尝试调用MEX文件,我就会得到:

However, despite this, once I get to MATLAB and attempt to call the MEX file, I get:

无效的MEX文件'(mex文件的路径)':未定义符号:__asan_option_detect_stack_use_after_return.

当人们弄乱正确连接地址清理器的链接时,该特定错误确实很常见.但是,从字面上看,我正在编译的每个文件都被链接到其中.每个.o,每个.mexa64.

That specific error is really common when people mess up linking in the address sanitizer correctly. However, in literally every file I'm compiling it's linked in. Every .o, every .mexa64.

我怀疑这是因为MATLAB本身不具备此功能,但是我不确定.其他MEX开发人员的一些指导将是很棒的.

I suspect this is because MATLAB itself isn't capable of that, but I'm unsure. Some guidance from other MEX developers would be fantastic.

我为需要此操作的任何人使用的完整步骤:

1:安装libasan(对我来说是"yum install libasan",但可能有所不同)

1: Install libasan (for me it was "yum install libasan", but it might vary)

2:将 -fsanitize = address 添加到makefile的LDFLAG和CFLAG中,以为我的项目构建MEX文件和目标文件.

2: Add -fsanitize=address to the LDFLAGs and CFLAGs of the makefile building the MEX files and object files for my project.

3:进行清洁,并确保已包含库(我使用g ++构建,显然clang默认为libasan的STATIC版本,在这种情况下,即实际上未编译正在运行的可执行文件时,它将不起作用与libasan)

3: Make clean and make to ensure it has the library included (I built with g++, apparently clang defaults to the STATIC version of libasan, which won't work in situations like this where the running executable isn't actually compiled with libasan)

4:在终端中,执行以下操作:

4: In a terminal, do:

export LD_PRELOAD =/lib64/libasan.so.5 (尽管此位置有所不同.我通过这篇文章找到了如何找到它的方法:从 gcc/clang 获取 libasan 的位置)

export LD_PRELOAD=/lib64/libasan.so.5 (this location varies though. I found out how to locate it using this post: Get location of libasan from gcc/clang)

然后做:

导出ASAN_OPTIONS = halt_on_error = false

5:最后,调用MATLAB:

5: Finally, call MATLAB:

matlab -nojvm -nodesktop -nosplash

6:然后(这可能是我的项目专用的),我CD到MATLAB项目所在的目录中,做了 addpath(genpath('.'))添加其所有内容文件,最后调用完成该工作的实际MATLAB脚本.

6: Then (and this might be specific to my project) I CD'd into the directory where the MATLAB project was, did addpath(genpath('.')) to add all its files, and finally called the actual MATLAB script that does the work.

结果是绿色和红色错误,例如:

The result was errors in green and red, like:

地址0x(某些地址)位于线程T(线程号)的堆栈中摘要:AddressSanitizer:memcpy-param-overlap(libasan.so.5 +(一些数字))或者错误:AddressSanitizer:memcpy-param-overlap:内存范围[范围]和[范围]重叠

Address 0x(some address) is located in stack of thread T(thread number) SUMMARY: AddressSanitizer: memcpy-param-overlap (libasan.so.5+(some number)) or ERROR: AddressSanitizer: memcpy-param-overlap: memory ranges [range] and [range] overlap

推荐答案

已消毒的库(MEX文件是共享库)希望 libasan.so 链接到主可执行文件(在这种情况下为MATLAB))或在开始时 LD_PRELOAD .由于您无法重建MATLAB,因此第二种方法是唯一的机会.

Sanitized libraries (MEX-files are shared libraries) expect libasan.so to be either linked to main executable (in that case MATLAB) or LD_PRELOADed at start. As you can't rebuild MATLAB, the second approach is your only chance.

它可能无法正常运行,因为Asan可能会在MATLAB启动代码中发现内存问题,并在到达MEX文件之前中止运行.您可以使用 export ASAN_OPTIONS = halt_on_error = false 忽略这些错误(它们仍会报告,但至少会继续执行).

It might not work smoothly because Asan would likely find memory issues in MATLAB startup code and abort before it gets to your MEX-file. You can use export ASAN_OPTIONS=halt_on_error=false to ignore those errors (they'll still be reported but at least execution will continue).

请注意,您的问题与在未经过消毒的解释器中运行经过消毒的二进制插件的其他问题类似(请参见例如

As a side note, your issue is similar to other questions about running sanitized binary plugins in unsanitized interpreters (see e.g. similar question for Python, although that one is Clang-centric).

这篇关于可以使用-fsanitize = address运行MEX文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆