I have a MEX file compiled normally with g++.
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).
However, despite this, once I get to MATLAB and attempt to call the MEX file, I get:
Invalid MEX-file '(path to mex file)': undefined symbol: __asan_option_detect_stack_use_after_return.
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.
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.
Full steps I used for anyone who needs this:
1: Install libasan (for me it was "yum install libasan", but it might vary)
2: Add -fsanitize=address
to the LDFLAGs and CFLAGs of the makefile building the MEX files and object files for my project.
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: In a terminal, do:
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)
Then do:
export ASAN_OPTIONS=halt_on_error=false
5: Finally, call MATLAB:
matlab -nojvm -nodesktop -nosplash
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:
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