Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created November 26, 2018 03:52
Show Gist options
  • Save kaityo256/54537a4e30db8a9737a07970feccbb6d to your computer and use it in GitHub Desktop.
Save kaityo256/54537a4e30db8a9737a07970feccbb6d to your computer and use it in GitHub Desktop.
ubsan sample
double func(double a) {}
int main() {
func(1.0);
}
@kaityo256
Copy link
Author

kaityo256 commented Nov 26, 2018

$ gcc --version
gcc (GCC) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (GCC) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc  -fsanitize-undefined-trap-on-error  -fsanitize=undefined ub.c

$ ./a.out; echo $?
0

$ g++ -fsanitize-undefined-trap-on-error -fsanitize=undefined ub.c 

$ ./a.out; echo $?
zsh: illegal hardware instruction (core dumped)  ./a.out
132 

@kaityo256
Copy link
Author

Test on Mac OS X (mojave 10.14.1)

$ g++ ub.c 
ub.c: In function 'double func(double)':
ub.c:1:24: warning: no return statement in function returning non-void [-Wreturn-type]
 double func(double a) {}
                        ^

$ ./a.out; echo $?
0

$ g++ -O1 ub.c 
ub.c: In function 'double func(double)':
ub.c:1:24: warning: no return statement in function returning non-void [-Wreturn-type]
 double func(double a) {}
                        ^

$ ./a.out; echo $?
zsh: illegal hardware instruction  ./a.out
132

$ g++ -Q --help=common |grep sanitize
  -fsanitize-address-use-after-scope 	[disabled]
  -fsanitize-coverage=        		
  -fsanitize-recover          		
  -fsanitize-recover=         		
  -fsanitize-sections=<sec1,sec2,...> 	
  -fsanitize-undefined-trap-on-error 	[disabled]
  -fsanitize=

$ g++ -O1 -Q --help=common |grep sanitize  
  -fsanitize-address-use-after-scope 	[disabled]
  -fsanitize-coverage=        		
  -fsanitize-recover          		
  -fsanitize-recover=         		
  -fsanitize-sections=<sec1,sec2,...> 	
  -fsanitize-undefined-trap-on-error 	[disabled]
  -fsanitize=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment