Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: -fsanitize=address doesn't work with exceptions #3760

Open
mikey-b opened this issue Jun 12, 2021 · 10 comments
Open

Windows: -fsanitize=address doesn't work with exceptions #3760

mikey-b opened this issue Jun 12, 2021 · 10 comments

Comments

@mikey-b
Copy link

mikey-b commented Jun 12, 2021

Hi all,

On Windows 10 64bit, LDC2 1.27.0-beta1. I am having issues with throwing exceptions in constructors. Address sanatiser is able to catch the issue as an access violation. Here is example code to trigger this issue:

import std.stdio;

class example: Exception {
	this(string msg, string file = __FILE__, size_t line = __LINE__) {
		super(msg, file, line);
	}
}

class test {
	this() {		
		throw new example("this is a test");
	}
}

void main() {
	try {
		auto t = new test();
	} catch (example e) {
		writeln(e.msg);
	}
}

Compiled with:
ldc2.exe -g -fsanitize='address' .\test.d -of='test.exe'

Access Violation is on "writeln(e.msg)" line.

Kind regards,
Mike Brown

@JohanEngelen
Copy link
Member

Please paste the full ASan output here.

@mikey-b
Copy link
Author

mikey-b commented Jun 12, 2021

Hi Johan,

Sure, here it is (It is identical on all runs)

Kind regards,
Mike

.\test.exe
=================================================================
==14804==ERROR: AddressSanitizer: access-violation on unknown address 0x000000000020 (pc 0x7ff6fdf51506 bp 0x00e7804ffa80 sp 0x00e7804ff9a0 T0)
==14804==The signal is caused by a READ memory access.
==14804==Hint: address points to the zero page.
==14804==*** WARNING: Failed to initialize DbgHelp! ***
==14804==*** Most likely this means that the app is already ***
==14804==*** using DbgHelp, possibly with incompatible flags. ***
==14804==*** Due to technical reasons, symbolization might crash ***
==14804==*** or produce wrong results. ***
#0 0x7ff6fdf51505 in _Dmain C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.d:19
#1 0x7ff6fdfc4ec4 in _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074ec4)
#2 0x7ff6fdfc4b96 in _d_run_main2 (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074b96)
#3 0x7ff6fdfc4e52 in _d_wrun_main (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074e52)
#4 0x7ff6fdf51993 in wmain C:\Program Files\LDC 1.27\import\core\internal\entrypoint.d:32
#5 0x7ff6fdfeac33 in invoke_main d:\agent_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:90
#6 0x7ff6fdfeac33 in __scrt_common_main_seh d:\agent_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
#7 0x7ffcd2f47033 (C:\WINDOWS\System32\KERNEL32.DLL+0x180017033)
#8 0x7ffcd4a42650 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180052650)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: access-violation C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.d:19 in _Dmain
==14804==ABORTING

@mikey-b
Copy link
Author

mikey-b commented Jun 12, 2021

Hi again Johan,

I apologise, it isnt the same on each run. Here is another ASAN report:

.\test.exe

==9532==ERROR: AddressSanitizer: unknown-crash on address 0x0279dcbe5e88 at pc 0x7ff6fdf6b4d9 bp 0x00d05432e640 sp 0x00d05432e680
READ of size 4096 at 0x0279dcbe5e88 thread T0
#0 0x7ff6fdf6b4d8 in __asan_wrap_memmove D:\a\1\s\compiler-rt\lib\sanitizer_common\sanitizer_common_interceptors.inc:793
#1 0x7ff6fdfeea82 in _fwrite_nolock minkernel\crts\ucrt\src\appcrt\stdio\fwrite.cpp:94
#2 0x7ff6fdfee97e in <lambda_5856287d7ecd2be6c9197bb4007c3f6e>::operator() minkernel\crts\ucrt\src\appcrt\stdio\fwrite.cpp:37
#3 0x7ff6fdfee921 in __crt_seh_guarded_call::operator()<<lambda_2831f20263db5b546e098b45503eb778>,<lambda_5856287d7ecd2be6c9197bb4007c3f6e> &,<lambda_0838d7e100fbcbd261b69cfea6abb102>

vccrt\vcruntime\inc\internal_shared.h:204
#4 0x7ff6fdfeebca in __acrt_lock_stream_and_call minkernel\crts\ucrt\inc\corecrt_internal_stdio.h:252
#5 0x7ff6fdfeebca in fwrite minkernel\crts\ucrt\src\appcrt\stdio\fwrite.cpp:33
#6 0x7ff6fdf52851 in _D3std5stdio__T13trustedFwriteTaZQsFNbNiNePOS4core4stdcQBx6_iobufxAaZm C:\Program Files\LDC 1.27\import\std\stdio.d:4799
#7 0x7ff6fdf51bd1 in _D3std5stdio4File17LockingTextWriter__T3putTAyaZQjMFNfMQlZv C:\Program Files\LDC 1.27\import\std\stdio.d:3260
#8 0x7ff6fdf516ef in _D3std5stdio__T7writelnTAyaZQnFNfQjZv C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.d:4328
#9 0x7ff6fdf51515 in _Dmain C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.d:19
#10 0x7ff6fdfc4ec4 in _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074ec4)
#11 0x7ff6fdfc4b96 in _d_run_main2 (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074b96)
#12 0x7ff6fdfc4e52 in _d_wrun_main (C:\Users\Mike Brown\Documents\Projects\nibia-lang\test.exe+0x140074e52)
#13 0x7ff6fdf51993 in wmain C:\Program Files\LDC 1.27\import\core\internal\entrypoint.d:32
#14 0x7ff6fdfeac33 in invoke_main d:\agent_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:90
#15 0x7ff6fdfeac33 in __scrt_common_main_seh d:\agent_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
#16 0x7ffcd2f47033 (C:\WINDOWS\System32\KERNEL32.DLL+0x180017033)
#17 0x7ffcd4a42650 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180052650)

Address 0x0279dcbe5e88 is located in the low shadow area.
SUMMARY: AddressSanitizer: unknown-crash D:\a\1\s\compiler-rt\lib\sanitizer_common\sanitizer_common_interceptors.inc:793 in __asan_wrap_memmove
==9532==ABORTING

@JohanEngelen
Copy link
Member

I'm quite sure this is a problem with ASan on Windows and exceptions. Can you simplify the testcase so that it does not use a class, or phobos stdio. I think simply throwing an exception and catching it will trigger the problem.

@JohanEngelen
Copy link
Member

google/sanitizers#749

@mikey-b
Copy link
Author

mikey-b commented Jun 12, 2021

Hi Johan,

I can confirm that

class example: Exception {
	this(string msg, string file = __FILE__, size_t line = __LINE__) {
		super(msg, file, line);
	}
}

void main() {
	try {
		throw new example("test");
	} catch (example e) {
		auto a = (e.msg == "test");
	}
}

also causes the issue. But access to e.msg is required. Is there any fix or workaround available?

Kind regards,
Mike Brown

@JohanEngelen
Copy link
Member

I don't know the workaround, but when googling a bit, it looks like it is a known issue and some people report that with changing settings in VS it does work...

@kinke kinke changed the title Exceptions in Constructor Windows: -fsanitize=address doesn't work with exceptions Jun 19, 2021
@kinke
Copy link
Member

kinke commented Jun 19, 2021

I am having issues with throwing exceptions in constructors.

The testcase obviously works just fine without ASan, just for clarification.

@mikey-b
Copy link
Author

mikey-b commented Jun 19, 2021 via email

@kinke
Copy link
Member

kinke commented Jun 19, 2021

Wow, sounds extremely weird! :D - A factory returning a variant seems much more reasonable. If you think the original code should really have worked (edit: or reported a better error), please file that as a separate issue with according code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants