一、安装VS2019
请移步至https://my.visualstudio.com/Downloads?q=visual%20studio%202019&wt.mc_id=o~msft~vscom~older-downloads选择 Visual Studio Professional 2019 (version 16.11.16)专业版下载
key请自行Google获取,谢谢。
二、VS2019工程属性配置ASAN地址消毒
Turn on AddressSanitizer & Turn off edit and continue
Turn off /INCREMENTAL (incremental linking)
三、配置ASAN_OPTIONS环境变量
log_path=asan.log:表示程序运行时asan若检测到内存操作问题,会记录在此,请打开该文件进行分析。
help=1:Print the flag descriptions
可在ASAN_OPTIONS环境变量中提供如下配置,仅供参考:halt_on_error=1:quarantine_size_mb=512:thread_local_quarantine_size_kb=128:log_path=asan.log:alloc_dealloc_mismatch=1:allocation_size_too_big=1:calloc_overflow=1:double_free=1:dynamic_stack_buffer_overflow=1:global_buffer_overflow=1:heap_buffer_overflow=1:heap_use_after_free=1:invalid_allocation_alignment=1:memcpy_param_overlap=1:new_delete_type_mismatch=1:stack_buffer_overflow=1:stack_buffer_underflow=1:stack_use_after_return=1:stack_use_after_scope=1:strncat_param_overlap=1:use_after_poison=1:detect_stack_use_after_return=1:allow_user_poisoning=1:check_initialization_order=1:report_globals=1:debug=1:sleep_before_dying=5:atexit=1:print_stats=1:strict_init_order=1:detect_invalid_pointer_pairs=2:detect_container_overflow=1:detect_odr_violation=2:verbosity=2:detect_deadlocks=1:color=always:help=1
以下选项是Windows所支持且常用的,亲测可用:
halt_on_error=0:quarantine_size_mb=1024:thread_local_quarantine_size_kb=512:log_path=asan.log:alloc_dealloc_mismatch=1:new_delete_type_mismatch=1:detect_stack_use_after_return=1:allow_user_poisoning=1:check_initialization_order=1:report_globals=1:debug=1:sleep_before_dying=5:atexit=1:print_stats=1:strict_init_order=1:detect_invalid_pointer_pairs=2:detect_container_overflow=1:detect_odr_violation=2:verbosity=2:detect_deadlocks=1:color=always:detect_stack_use_after_return=1:allocator_may_return_null=1:windows_hook_legacy_allocators=1
另外,请在项目属性==>C/C++==>Command Line==>Additional Options里添加/fsanitize-address-use-after-return或/fno-sanitize-address-vcasan-lib支持,ASAN_OPTIONS里增加
detect_stack_use_after_return=1
友情提示:以上配置OK之后,尤其是ASAN_OPTIONS环境变量配置成功后,请重启VS2019或Windows生效。
四、遗留问题
1.halt_on_error=0选项亲测功能不生效,即未在asan工具检测到内存问题后继续运行程序,而是闪退。待研究。
五、参考文献
1) 关于 ASAN - 知乎