<div dir="ltr">Please check the second entry in the FAQ: <a href="https://github.com/google/sanitizers/wiki/AddressSanitizer#faq">https://github.com/google/sanitizers/wiki/AddressSanitizer#faq</a><div>Tl;Dr: </div><div> for C you need to add -fno-common</div><div> for C++ everything will work out of the box</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 19, 2016 at 3:25 AM, Shi, Steven <span dir="ltr"><<a href="mailto:steven.shi@intel.com" target="_blank">steven.shi@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="m_-224509769445881313WordSection1">
<p class="MsoNormal">Hi Kcc,<u></u><u></u></p>
<p class="MsoNormal">I find both clang and gcc fail to report global buffer overflow if global buffer is not initialized. Below is my test code and build commands. Please advise.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">#include <stdio.h><u></u><u></u></p>
<p class="MsoNormal">int global_array[11];<u></u><u></u></p>
<p class="MsoNormal">int main(int argc, char **argv) {<u></u><u></u></p>
<p class="MsoNormal"> int Num;<u></u><u></u></p>
<p class="MsoNormal"> Num = 11;<u></u><u></u></p>
<p class="MsoNormal"> global_array[Num] = 0x87654321; // global buffer overflow<u></u><u></u></p>
<p class="MsoNormal"> printf("global_array[%d]=0x%x\<wbr>n", Num, global_array[Num]);
<u></u><u></u></p>
<p class="MsoNormal"> return global_array[Num];<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">}<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">$clang global.c -fsanitize=address -g -O0<u></u><u></u></p>
<p class="MsoNormal">$ ./a.out<u></u><u></u></p>
<p class="MsoNormal">global_array[11]=0x87654321<u></u><u></u></p>
<p class="MsoNormal">$gcc-5 global.c -fsanitize=address -g -O0<u></u><u></u></p>
<p class="MsoNormal">$ ./a.out<u></u><u></u></p>
<p class="MsoNormal">global_array[11]=0x87654321<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">But both clang and gcc can report global buffer overflow if global buffer first item is initialized as below.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">#include <stdio.h><u></u><u></u></p>
<p class="MsoNormal">int global_array[11]<b><span style="color:red">={0};</span></b><u></u><u></u></p>
<p class="MsoNormal">int main(int argc, char **argv) {<u></u><u></u></p>
<p class="MsoNormal"> int Num;<u></u><u></u></p>
<p class="MsoNormal"> Num = 11;<u></u><u></u></p>
<p class="MsoNormal"> global_array[Num] = 0x87654321; // global buffer overflow<u></u><u></u></p>
<p class="MsoNormal"> printf("global_array[%d]=0x%x\<wbr>n", Num, global_array[Num]);
<u></u><u></u></p>
<p class="MsoNormal"> return global_array[Num];<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">}<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">$clang global.c -fsanitize=address -g -O0<u></u><u></u></p>
<p class="MsoNormal">$ ./a.out<u></u><u></u></p>
<p class="MsoNormal">==============================<wbr>==============================<wbr>=====<u></u><u></u></p>
<p class="MsoNormal">==12162==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000013808ec at pc 0x0000004eede1 bp 0x7ffe2ae3a490 sp 0x7ffe2ae3a488<u></u><u></u></p>
<p class="MsoNormal">WRITE of size 4 at 0x0000013808ec thread T0<u></u><u></u></p>
<p class="MsoNormal"> #0 0x4eede0 in main /home/jshi19/sanitizer/MSVS_<wbr>GS_Compare/global.c:41:21<u></u><u></u></p>
<p class="MsoNormal"> #1 0x7f83c62a582f in __libc_start_main /build/glibc-GKVZIf/glibc-2.<wbr>23/csu/../csu/libc-start.c:291<u></u><u></u></p>
<p class="MsoNormal" style="text-indent:9.0pt">#2 0x419228 in _start (/home/jshi19/sanitizer/MSVS_<wbr>GS_Compare/a.out+0x419228)<u></u><u></u></p>
<p class="MsoNormal" style="text-indent:9.0pt">….<u></u><u></u></p>
<p class="MsoNormal" style="text-indent:9.0pt"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif">Steven Shi</span></b><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif"><u></u><u></u></span></b></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif">Intel\SSG\STO\UEFI Firmware</span></b><b><span style="font-size:12.0pt;font-family:"Arial",sans-serif"><u></u><u></u></span></b></p>
<p class="MsoNormal"><b><span style="font-family:"Arial",sans-serif"><u></u> <u></u></span></b></p>
<p class="MsoNormal" style="text-align:justify;text-justify:inter-ideograph"><span style="font-size:9.0pt;font-family:"Arial",sans-serif">Tel: <a href="tel:+86%2021%206116%206522" value="+862161166522" target="_blank">+86 021-61166522</a><u></u><u></u></span></p>
<p class="MsoNormal" style="text-align:justify;text-justify:inter-ideograph"><span style="font-size:9.0pt;font-family:"Arial",sans-serif">iNet: 821-6522<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</blockquote></div><br></div>