Does anyone use /fsanitize=address for C++/Fortran code

Dear all,

I have a code which is a mixed C++/Fortran code. The C++ is the master code, which will call Fortran subroutines. I usually develope on windows with Visual Studio 2022 + Intel OneAPI.

Now in Fortran, to prevent or identify Heisenbugs, I usually use the below left hand side flags (screenshot from ChatGpt),

which can be easily set in Visual Studio

However, sometimes there are also some Heisenbugs from C++ part too. Usually comes from accessing the wrong index of an array, like array has 5 elemement, but it accidently access the 6th element.
So, I am trying to see if C++ has similar flags as Fortran, like, /traceback /check:pointer /check:bounds /check:shape /check:uninit /check:format /check:output_conversion /check:stack

But it seems C++ do not have such flags that can be easily used, other than something like /Zi like below. This is more or less like /traceback flags in Fortran. However, unlike Fortran’s flags in Fortran code, this /Zi sometimes cannot locate to the C++ source code where memory corruption occur.

In the code I am working on, it runs no problem in debug mode. But it has memory corruption issues in release mode. So I start the debugger in visual studio 2022 in release mode, and because of this, when memory corruption occur, the call stack can not trace back to the exact subroutine which caused the problem, the call stack will just locate to something like ??? (that is probably it is in release mode, some optimizations caused these ???).
ChatGPT says something like, heap corruption is often detected later (usually on free, delete, or another allocation). In other words, what the debugger call stack shows, is where the system finally realized the heap was damaged, not when and where you overwrote the buffer.

But I want to know when and where it overwrote the buffer, this is how we can find at which line in the code in which subroutine, that the problem occur. At first I tried Intel OneAPI’s gdb, but this thing seems to be similar as the visual studio’s debugger, call back only shows some ???. Then I tried WinDbg which comes with Windows SDK and has GUI which give quite a vintage feeling, but it works good actually. Its call stack can locate to the exact subroutine (which cause the problem) in the source code. Problem solved.

In C++, it looks like the most similar flags with those Fortran debug flags, is

/fsanitize=address

But long words short, this flag is not easy to use. Unlike Fortran’s flags which just work, when run the exe built with this /fsanitize=address flag, it always report some errors or missing some dll stuff.

I wonder guys, in C++/Fortran mixed code how do you do things like /traceback, /check:bounds for the C++ part? Many thanks!

PS.
I mean it is a little unbelievable that visual studio’s debugger cannot do what a vintage looking WinDbg can do, consider that both come from Microsoft :rofl:

2 Likes

At least with clang and gcc/gfortran, -fsanitize=address needs to be passed to both the compiler and the linker, due to the runtime library. If you’re getting errors due to a missing dll, it sounds like that’s what’s happening. I’m not sure how visual studio works, but you probably just need to add it as a link flag as well.

ASan is great for detecting these sorts of memory errors, and is much, much faster than tools like valgrind.

I also recommend using clang-tidy on the C++ part, it can statically detect bugprone constructions, as well as things like performance bugs.

1 Like