Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created November 26, 2018 03:18
Show Gist options
  • Save kaityo256/8c5d9113947195a9351b75253854ba3e to your computer and use it in GitHub Desktop.
Save kaityo256/8c5d9113947195a9351b75253854ba3e to your computer and use it in GitHub Desktop.
SIGILL on Mac OS X (Mojave) and GCC 8.2.0
#include <iostream>
#include <string>
double func(std::string &str) {
}
int main() {
std::string str = "test";
func(str);
}
@kaityo256
Copy link
Author

kaityo256 commented Nov 26, 2018

$ g++ --version 
g++ (Homebrew GCC 8.2.0) 8.2.0
Copyright (C) 2018 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++ -O3 test.cpp  
test.cpp: In function 'double func(std::__cxx11::string&)':
test.cpp:5:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }

$ ./a.out
zsh: illegal hardware instruction  ./a.out

$ g++ -O3 -S test.cpp
test.cpp: In function 'double func(std::__cxx11::string&)':
test.cpp:5:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }

Why does the above generate ud2?

	.text
	.section __TEXT,__text_cold,regular,pure_instructions
	.globl __Z4funcRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
__Z4funcRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE:
LFB1564:
	ud2
LFE1564:
	.globl _main
_main:
LFB1565:
	ud2
LFE1565:
	.section __TEXT,__text_startup,regular,pure_instructions
	.align 4
__GLOBAL__sub_I_test.cpp:
LFB2049:
	leaq	__ZStL8__ioinit(%rip), %rdi
	subq	$8, %rsp
LCFI0:
	call	__ZNSt8ios_base4InitC1Ev
	movq	__ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi
	addq	$8, %rsp
LCFI1:
	leaq	___dso_handle(%rip), %rdx
	leaq	__ZStL8__ioinit(%rip), %rsi
	jmp	___cxa_atexit
LFE2049:
	.static_data
__ZStL8__ioinit:
	.space	1
	.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
	.set L$set$0,LECIE1-LSCIE1
	.long L$set$0
LSCIE1:
	.long	0
	.byte	0x1
	.ascii "zR\0"
	.byte	0x1
	.byte	0x78
	.byte	0x10
	.byte	0x1
	.byte	0x10
	.byte	0xc
	.byte	0x7
	.byte	0x8
	.byte	0x90
	.byte	0x1
	.align 3
LECIE1:
LSFDE1:
	.set L$set$1,LEFDE1-LASFDE1
	.long L$set$1
LASFDE1:
	.long	LASFDE1-EH_frame1
	.quad	LFB1564-.
	.set L$set$2,LFE1564-LFB1564
	.quad L$set$2
	.byte	0
	.align 3
LEFDE1:
LSFDE3:
	.set L$set$3,LEFDE3-LASFDE3
	.long L$set$3
LASFDE3:
	.long	LASFDE3-EH_frame1
	.quad	LFB1565-.
	.set L$set$4,LFE1565-LFB1565
	.quad L$set$4
	.byte	0
	.align 3
LEFDE3:
LSFDE5:
	.set L$set$5,LEFDE5-LASFDE5
	.long L$set$5
LASFDE5:
	.long	LASFDE5-EH_frame1
	.quad	LFB2049-.
	.set L$set$6,LFE2049-LFB2049
	.quad L$set$6
	.byte	0
	.byte	0x4
	.set L$set$7,LCFI0-LFB2049
	.long L$set$7
	.byte	0xe
	.byte	0x10
	.byte	0x4
	.set L$set$8,LCFI1-LCFI0
	.long L$set$8
	.byte	0xe
	.byte	0x8
	.align 3
LEFDE5:
	.mod_init_func
	.align 3
	.quad	__GLOBAL__sub_I_test.cpp
	.constructor
	.destructor
	.align 1
	.subsections_via_symbols

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