vendredi 4 août 2023

Clang ASan fails when handling exceptions in try/catch block on Windows (AddressSanitizer: access-violation on unknown address)

Setup: Windows 10, Clang 16.0.3, ASan (-fsanitize=address), -O0

Any code related to handling exceptions e.g., try/catch, will result in a report about access-violation (additionally undefined sanitazation also would not be happy about it) or will change the behaviour of the program.

#include <exception>
#include <iostream>

int main() {
    try {
        throw std::exception("test");
    } catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    return 0;
}

Any variation from this list const std::exception &ex, const std::exception ex, std::exception &ex, std::exception ex result is some sort of bad behavior.

const std::exception &ex, const std::exception ex result directly in ASan error report like this:

==4384==ERROR: AddressSanitizer: access-violation on unknown address 0x00000000000e (pc 0x7ff74e3d12ca bp 0x008e4edcfaf0 sp 0x008e4edcd7f0 T0)
==4384==The signal is caused by a READ memory access.
==4384==Hint: address points to the zero page.
    #0 0x7ff74e3d12c9 in main C:\projects\test\asan.cpp:8
    #1 0x7ff74e478ccf in _CallSettingFrame d:\a01\_work\6\s\src\vctools\crt\vcruntime\src\eh\amd64\handlers.asm:49
    #2 0x7ff74e46e6bb in __FrameHandler3::CxxCallCatchBlock(struct _EXCEPTION_RECORD *) d:\a01\_work\6\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:1521
    #3 0x7fffdfbb1715  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800a1715)
    #4 0x7ff74e3d113b in main C:\projects\test\asan.cpp:6
    #5 0x7ff74e435d6b in invoke_main d:\a01\_work\6\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #6 0x7ff74e435d6b in __scrt_common_main_seh d:\a01\_work\6\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #7 0x7fffdf757613  (C:\WINDOWS\System32\KERNEL32.DLL+0x180017613)
    #8 0x7fffdfb626b0  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800526b0)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: access-violation C:\projects\test\asan.cpp:8 in main
==4384==ABORTING

While std::exception &ex, std::exception ex have altered behaviour and will contain some gibberish with a potential error report:

HГ─@]├HН♣▌f
=================================================================
==23388==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x7ff72ef69186 in thread T0
    #0 0x7ff72eed1e1d in free C:\src\llvm_package_16.0.3\llvm-project\compiler-rt\lib\asan\asan_malloc_win.cpp:82

Address 0x7ff72ef69186 is a wild pointer inside of access range of size 0x000000000001.
SUMMARY: AddressSanitizer: bad-free C:\src\llvm_package_16.0.3\llvm-project\compiler-rt\lib\asan\asan_malloc_win.cpp:82 in free
==23388==ABORTING

I've spent a lot of time thying to find the source of the error, but could not find anything. The only clue is that compiling without address sanitazation fixes everything.

Aucun commentaire:

Enregistrer un commentaire