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

tests: use {UBSAN,ASAN}_OPTIONS to abort on error #238

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

N-R-K
Copy link
Contributor

@N-R-K N-R-K commented Nov 9, 2024

c20991d added -ftrapv, presumably to to catch overflows in the CI, but this doesn't really work because UBSan overrides the trap:

[/tmp]~> cat test.c
#include <limits.h>
int main(void)
{
	int a = 5;
	a += INT_MAX;
	return 0;
}
[/tmp]~> gcc -fsanitize=undefined -ftrapv test.c -o test
[/tmp]~> ./test || echo "FAIL"
test.c:5:4: runtime error: signed integer overflow: 5 + 2147483647 cannot be represented in type 'int'

-fsanitize-trap gives the wanted trapping behavior but it doesn't print any diagnostic message:

[/tmp]~> gcc -fsanitize=undefined -fsanitize-trap test.c -o test
[/tmp]~> ./test || echo "FAIL"
zsh: illegal hardware instruction  ./test
FAIL

it's best to use {ASAN,UBSAN}_OPTIONS env vars to configure it to abort on error. this prints diagnostic and also traps:

[/tmp]~> gcc -fsanitize=undefined test.c -o test
[/tmp]~> UBSAN_OPTIONS='abort_on_error=1:halt_on_error=1' ./test || echo "FAIL"
test.c:5:4: runtime error: signed integer overflow: 5 + 2147483647 cannot be represented in type 'int'
zsh: IOT instruction  UBSAN_OPTIONS='abort_on_error=1:halt_on_error=1' ./test
FAIL

c20991d added `-ftrapv`, presumably to to catch overflows in
the CI, but this doesn't really work because UBSan overrides the
trap:

	[/tmp]~> cat test.c
	#include <limits.h>
	int main(void)
	{
		int a = 5;
		a += INT_MAX;
		return 0;
	}
	[/tmp]~> gcc -fsanitize=undefined -ftrapv test.c -o test
	[/tmp]~> ./test || echo "FAIL"
	test.c:5:4: runtime error: signed integer overflow: 5 + 2147483647 cannot be represented in type 'int'

`-fsanitize-trap` gives the wanted trapping behavior but it
doesn't print any diagnostic message:

	[/tmp]~> gcc -fsanitize=undefined -fsanitize-trap test.c -o test
	[/tmp]~> ./test || echo "FAIL"
	zsh: illegal hardware instruction  ./test
	FAIL

it's best to use `{ASAN,UBSAN}_OPTIONS` env vars to configure it
to abort on error. this prints diagnostic and also traps:

	[/tmp]~> gcc -fsanitize=undefined test.c -o test
	[/tmp]~> UBSAN_OPTIONS='abort_on_error=1:halt_on_error=1' ./test || echo "FAIL"
	test.c:5:4: runtime error: signed integer overflow: 5 + 2147483647 cannot be represented in type 'int'
	zsh: IOT instruction  UBSAN_OPTIONS='abort_on_error=1:halt_on_error=1' ./test
	FAIL
@cdown
Copy link
Owner

cdown commented Nov 9, 2024

Thank you!

@cdown cdown merged commit ed7f823 into cdown:develop Nov 9, 2024
1 check passed
@N-R-K N-R-K deleted the sanitizer-env branch November 10, 2024 00:38
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

Successfully merging this pull request may close these issues.

2 participants