emi testing: finding 1000+ bugs in gcc and llvm...
TRANSCRIPT
![Page 1: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/1.jpg)
EMI Testing: Finding 1000+ Bugs in GCC and LLVM in 3 Years
Zhendong Su University of California, Davis
![Page 2: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/2.jpg)
MehrdadAfshari VuLe ChengnianSun QirunZhang
h5p://web.cs.ucdavis.edu/~su/emi-project/
![Page 3: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/3.jpg)
compilers
int main () { // do something … return 0;}
…call putsmovl $0, %eaxpopq %rbpret
Developers’ belief: Compilersarefaithfultranslators
![Page 4: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/4.jpg)
Howtrustworthyarecompilers?
Howtheyimpactsecurity&reliability?
let’s reflect on this trust
![Page 5: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/5.jpg)
In2014,GCC4.9miscompiledtheLinuxkernel!“Ok,soI‘mlookingatthecodegenera8onandyourcompilerispureandu;er****.……AddingJakubtothecc,becausegcc-4.9.0seemstobeterminallybroken.”--LinusTorvalds
![Page 6: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/6.jpg)
Type #1 bugs
![Page 7: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/7.jpg)
llvm bug 14972
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
![Page 8: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/8.jpg)
developer comment
“...very,veryconcerningwhenI
gottotherootcause,andvery
annoyingtofix…”
h/p://llvm.org/bugs/show_bug.cgi?id=14972
![Page 9: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/9.jpg)
Type #2 “bugs”
![Page 10: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/10.jpg)
gcc “bug” 8537 #include <string>using std::string;#include <memory>
// The specifics of this function are not important for // demonstrating this bug.const string getPasswordFromUser() const;
bool isPasswordCorrect() { bool isPasswordCorrect = false; string Password("password");
if(Password == getPasswordFromUser()) { isPasswordCorrect = true; }
// Removed from the optimized code although it secures // the code by wiping the password from memory
memset(Password, 0, sizeof(Password)); return isPasswordCorrect;}
![Page 11: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/11.jpg)
debatable … From:"JosephD.Wagner"<[email protected]>To:<[email protected]>,[email protected],<[email protected]>,
[email protected],[email protected],<[email protected]>Cc:Subject:RE:opSmizaSon/8537:OpSmizerRemovesCodeNecessaryforSecurityDate:Sun,17Nov200208:59:53-0600
Directquotefrom:h/p://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Bug-Criteria.html"Ifthecompilerproducesvalidassemblycodethatdoesnotcorrectlyexecutetheinputsourcecode,thatisacompilerbug."Sotoallyounaysayersouttherewhoclaimthisisaprogrammingerrororpoorcoding,YES,ITISABUG!
![Page 12: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/12.jpg)
Type #3 “bugs”
![Page 13: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/13.jpg)
how about this one? char *buf = ...;char *buf_end = ...; unsigned int len = ...; if (buf + len >= buf_end) return; // len too large
if (buf + len < buf) return; //overflow, buf+len wrapped around
// write to buf[0..len-1]...
![Page 14: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/14.jpg)
even more debatable …
q Pointer overflow is undefined behavior u Compiler assumes code has no undefined behavior
u Thus compiler assumes: buf + len cannot overflow
u Then compiler infers: if (buf + len < buf) ) if (0)
q But this is a security threat u Use a large len to trigger buffer overflow
![Page 15: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/15.jpg)
Type #1 bugs: EMI Testing
How to find 1000+ GCC/LLVM bugs in 3 years?
real
![Page 16: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/16.jpg)
vision
P≡P1
P2 P3
PkPn
…
![Page 17: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/17.jpg)
key challenges
q Generation u How to generate different, yet equivalent tests?
q Validation u How to check that tests are indeed equivalent?
q Both are long-standing hard issues
![Page 18: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/18.jpg)
equiv. modulo inputs
q Relax equiv. wrt a given input i u Must: P(i) = Pk(i) on input i
u Okay: P(j) ≠ Pk(j) on all input j ≠ i
q Exploit close interplay between u Dynamic program execution on some input
u Static compilation for all input
![Page 19: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/19.jpg)
profile
programP
outputO
inputI executedunexecuted
![Page 20: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/20.jpg)
mutate
…..
O
I
O
I
O
I
O
I
![Page 21: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/21.jpg)
mutate
…..
O
I
O
I
O
I
O
IequivalentwrtI
![Page 22: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/22.jpg)
revisit challenges
q Generation (easy) u How to generate different, yet equivalent tests?
q Validation (easy) u How to check that tests are indeed equivalent?
q Both are long-standing hard issues
![Page 23: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/23.jpg)
llvm bug 14972
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
![Page 24: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/24.jpg)
seed file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.out
![Page 25: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/25.jpg)
seed file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.out
unexecuted
![Page 26: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/26.jpg)
transformed file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
![Page 27: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/27.jpg)
reduced file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
![Page 28: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/28.jpg)
llvm bug autopsy
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
GVN:loadstructusing32-bitload
![Page 29: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/29.jpg)
llvm bug autopsy
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
GVN:loadstructusing32-bitload
SRoA:readpastthestruct’send è
undefinedbehavior
![Page 30: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/30.jpg)
llvm bug autopsy
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
GVN:loadstructusing32-bitload
remove
SRoA:readpastthestruct’send è
undefinedbehavior
![Page 31: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/31.jpg)
seed file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.out
![Page 32: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/32.jpg)
transformed file
$clang–m32–O0test.c;./a.out$clang–m32–O1test.c;./a.outAborted(coredumped)
![Page 33: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/33.jpg)
gcc bug 58731
$gcc–O0test.c;./a.out$gcc–O3test.c;./a.out^C
![Page 34: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/34.jpg)
gcc bug autopsy
$gcc–O0test.c;./a.out$gcc–O3test.c;./a.out^C
PRE:loopinvariant
![Page 35: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/35.jpg)
gcc bug autopsy
LIM
$gcc–O0test.c;./a.out$gcc–O3test.c;./a.out^C
![Page 36: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/36.jpg)
gcc bug autopsy
integeroverflow
$gcc–O0test.c;./a.out$gcc–O3test.c;./a.out^C
![Page 37: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/37.jpg)
seed program
$gcc–O0test.c;./a.out$gcc–O3test.c;./a.out
nolongeraloopinvariant
![Page 38: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/38.jpg)
orion
q First and simplest EMI realization
q Targeting C compilers
u Randomly prune unexecuted code
u Very effective
![Page 39: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/39.jpg)
bug counts (till 03/2014)
GCC LLVM TOTALReported 111 84 195MarkedDuplicate 28 7 35Confirmed 79 68 147Fixed 56 54 110
![Page 40: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/40.jpg)
bug types
GCC LLVM TOTALWrongcode 46 49 95Crash 23 10 33Performance 10 9 19
![Page 41: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/41.jpg)
bug importance
q Most bugs have already been fixed
q Many were critical, release-blocking
q Some affected real-world projects
![Page 42: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/42.jpg)
Athena(OOPSLA’15)Prune&injectdeadcode
Hermes(OOPSLA’16)Mutatelivecode
Orion(PLDI’14)Prunedeadcode
![Page 43: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/43.jpg)
Athena: gcc bug 61383
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;
elseif(h)break;}}intmain(){fn1();return0;}
$gcc-O0test.c;./a.out$gcc–O2test.c;./a.outFloatingpointexception(coredumped)
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;
elsec=1;}}intmain(){fn1();return0;}
Athena
SeedProgramP Bug-triggeringVariant
![Page 44: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/44.jpg)
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;elsec=1;}}intmain(){fn1();return0;}
Current
![Page 45: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/45.jpg)
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;else;}}intmain(){fn1();return0;}
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;elsec=1;}}intmain(){fn1();return0;}
Current Proposal
Delete“c=1”
![Page 46: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/46.jpg)
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;elseif(h)break;}}intmain(){fn1();return0;}
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){h=(f==0)?0:1%f;if(f<1)c=0;else;}}intmain(){fn1();return0;}
Current Proposal
Insertthestatementbelow
Context Statement
1.Requiresloop if(i)break;2.inti;
![Page 47: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/47.jpg)
inta,c,d,e=1,f;intfn1(){inth;for(;d<1;d=e){
h=(f==0)?0:1%f;if(f<1)c=0;elseif(h)break;}}intmain(){fn1();return0;}
Bug-triggeringVariantinta,c,d,e=1,f;intfn1(){inth;
intg=1%f;for(;d<1;d=e){h=(f==0)?0:g;if(f<1)c=0;elseif(h)break;}}intmain(){fn1();return0;}
MiscompiledExecutable
loopinvariant
hoisted
![Page 48: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/48.jpg)
Hermes q Profile and record variable values q Synthesize code snippets
§ Ensure no undefined behavior
§ Ensure EMI property locally o Maintain same program state at entry & exit
![Page 49: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/49.jpg)
Hermes: llvm 26266 chara;intb,c=9,d,e;voidfn1(){unsignedf=1;intg=8,h=5;for(;a!=6;a--){int*i=&h,*j;for(;;){//b=0,c=9,e=0,f=1,g=8,h=5if(d<=8)break;*i=0;for(;*j<=0;);}}}intmain(){fn1();return0;}
intbackup_g=e,backup_f=~1;if(g&&h){backup_g=g;backup_f=f;f=-(~(c&&b)|-~(e*~backup_f));if(c<f)abort();}g=backup_g;f=backup_f;
SeedProgramP EMIVariant
int*i=&h,*j;for(;;){//b=0,c=9,e=0,f=1,g=8,h=5
if(d<=8)break;*i=0;for(;*j<=0;);}
![Page 50: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/50.jpg)
intbackup_g=e,backup_f=~1;if(g&&h){backup_g=g;backup_f=f;f=-(~(c&&b)|-~(e*~backup_f));
if(c<f)abort();}g=backup_g;f=backup_f;
EMIVariant
int*i=&h,*j;for(;;){//b=0,c=9,e=0,f=1,g=8,h=5
if(d<=8)break;*i=0;for(;*j<=0;);}
$clang-m32-O0test.c$./a.out$clang-m32-O1test.c$./a.outAborted(coredumped)
Clang(mistakenly)deemsthispredicatealwaystrue
![Page 51: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/51.jpg)
if/while(P){//P=falsewrtprofiledstatesS//Sanysynthesizedcompilablecode}
if(P){//P=truewrtprofiledstatesS//Sisoriginalcodeatthisprogrampoint}
intbackup_v=valid-expressionif(true-predicate){backup_v=vv=valid-expressionif/while(false-predicate){printv}}v=backup_v
![Page 52: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/52.jpg)
~(c&&b)|-~(e*~f)
worklist=[“b”,“c”,“e”,“f”]
env:b=0,c=9,e=0,f=1,g=8,h=5
![Page 53: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/53.jpg)
worklist=[“b”,“c”,“e”,“f”]
env:b=0,c=9,e=0,f=1,g=8,h=5
Operands:“b”,“e” Operator:/ Validity:Invalid
~(c&&b)|-~(e*~f)
![Page 54: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/54.jpg)
worklist=[“b”,“c”,“e”,“f”]
env:b=0,c=9,e=0,f=1,g=8,h=5
Operands:“c”,“b” Operator:&& Validity:Valid
~(c&&b)|-~(e*~f)
![Page 55: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/55.jpg)
worklist=[“b”,“c”,“e”,“f”]
env:b=0,c=9,e=0,f=1,g=8,h=5
worklist=[“c&&b”,“e”,“f”]
~(c&&b)|-~(e*~f)
![Page 56: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/56.jpg)
worklist=[“b”,“c”,“e”,“f”]
env:b=0,c=9,e=0,f=1,g=8,h=5
worklist=[“c&&b”,“e”,“f”]
worklist=[“c&&b”,“e”,“~f”]
~(c&&b)|-~(e*~f)
worklist=[“~(c&&b)|-~(e*~f)”]
……
![Page 57: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/57.jpg)
bug counts (till recently)
GCC LLVM TOTALReported 643 498 1141Fixed 397 239 636
• ISSTA’15:Stress-tesinglink-imeopimizaion• ICSE’16:Analyzingcompilers’diagnosicsupport• Recent:Skeletalprogramenumeraion
![Page 58: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/58.jpg)
ongoing & future work
q Extend EMI to handle floating-point code
q Adapt EMI to other languages & settings
q Support bounded compiler verification
![Page 59: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/59.jpg)
how about CompCert?
$ccomptest.c;./a.out$ccomp–interptest.cTime21:programterminated(exitcode=0)$
//test.cintmain(){inta[2]=0;return0;}
//test.light.cintmain(void){inta[2];*(a+0)=0;*(a+1)=0;return0;}
![Page 60: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/60.jpg)
how about CompCert?
$ccomp-2.4test.c;./a.out$-116$ccomp-2.0test.c;./a.out$140
//test.c#include<stdio.h>intmain(){inti='\214';prinx("%d\n",i);return0;}
![Page 61: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/61.jpg)
how about CompCert?
$ccomp-2.6test.cFatalerror:excepionFile"ia32/Asmexpand.ml",line191,……$
//test.cvolailelonglonga;unsignedb;intmain(){a=b;return0;}
![Page 62: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/62.jpg)
how about CompCert?
$ccomp-2.6–interp–quiettest.c00$ccomp-2.7–interp–quiettest.c02$
//test.c#include<stdio.h>intmain(){intt=prinx("0\n");prinx("%d\n",t);return0;}
![Page 63: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/63.jpg)
how about CompCert?
$ccomp-2.7test.c…Fatalerror:excepionFile"backend/Regalloc.ml",line741,……$
//test.cinta,b,c,d,e,f,g;voidfn1(){inth,i,j;if(g){g=1;L1:if(1);}shortk=~j;intl=1/(h&e&~d+((k&~h)-((1|i)&(a|c)))),m=~~j/(~h|d+a);j=l&m|h;if(j);j=k;intn=~i|f,o=~b-1/-n*~i,p=f;gotoL1;}intmain(){if(a)fn1();return0;}
![Page 64: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/64.jpg)
EMI q EMI is general and widely applicable
u Can test compilers, analysis and transformation tools
u Generates real-world tests
u Requires no reference compilers
q EMI realizations are very effective u Have uncovered 1141 bugs in GCC and LLVM
u Many were long latent and miscompilations
![Page 65: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/65.jpg)
conclusion
q Compilers are important & complex
q They impact reliable & secure coding
q EMI offers powerful compiler testing
q Much more work is needed
![Page 66: EMI Testing: Finding 1000+ Bugs in GCC and LLVM …web.cs.ucdavis.edu/~su/emi-project/emi-testing.pdfgcc “bug” 8537 #include using std::string; #include](https://reader034.vdocuments.us/reader034/viewer/2022043003/5f83c1408910117ba62b5609/html5/thumbnails/66.jpg)
char *buf = ...;char *buf_end = ...; unsigned int len = ...;
if (buf + len >= buf_end) return; // len too large
if (buf + len < buf) return;//overflow, buf+len wrapped// write to buf[0..len-1]
#include <string>using std::string;#include <memory>const string getPasswordFromUser() const;bool isPasswordCorrect() { bool isPasswordCorrect = false; string Password("password"); if(Password == getPasswordFromUser()) { isPasswordCorrect = true; } memset(Password, 0, sizeof(Password)); return isPasswordCorrect;}
GCC LLVM TOTAL
Reported 643 498 1141
Fixed 397 239 636
Thankyou!
EMITesing:Finding1000+BugsinGCCandLLVM