<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;
        font-weight:normal;
        font-style:normal;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Quoth Richard Smith:<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:.5in'>In this particular case, where we can statically see that the size of the source is sizeof(struct S), it seems like this is something we could easily warn on, using much the same logic that FORTIFY_SOURCE would use at runtime: effectively, compute `__builtin_object_size(&src)` and compare that to `sizeof(struct S) + 1`. While that seems straightforward enough, it's only going to work in the cases where the pointer arguments are simple enough that we can constant-evaluate them in the frontend. Is that general enough for what you're looking for? (I think as a general strategy, implementing warnings for all the cases that FORTIFY_SOURCE would trap on at runtime would be a great idea.)<o:p></o:p></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>So, to be sure I understand, if instead of this:<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>  memcpy(dest, &src, sizeof(struct S) + 1);<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>we had this:<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>  unsigned famLen = …;  // some passed parameter or computed value<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>  memcpy(dest, &src, sizeof(struct S) + famLen);<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>then, the FE would not be able to warn about this because ‘sizeof(struct S) + famLen’ cannot be constant-evaluated?<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>Chris<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b>From:</b> Richard Smith <richard@metafoo.co.uk> <br><b>Sent:</b> Thursday, December 3, 2020 6:03 PM<br><b>To:</b> Chris Hamilton <chris.hamilton@ericsson.com><br><b>Cc:</b> cfe-dev@lists.llvm.org<br><b>Subject:</b> Re: [cfe-dev] Static check on memcpy()<o:p></o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, 3 Dec 2020 at 14:34, Chris Hamilton via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p></div><div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Hi folks,<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>It’s easy to see how memcpy (and other mem* functions) can cause out-of-bounds reads/writes, such as in this simplified reproducer for a real case we’ve seen:<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   #include <string.h><o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   struct S {<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      int x;<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      int xx;<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      int y[];<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   };<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   char dst[100];<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   <o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   int main(void) {<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      struct S src = {0};<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      src.x = 9999;<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      src.xx = 8888;<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>      memcpy(dst, &src, sizeof(struct S) + 1);<o:p></o:p></p><p style='margin:0in;margin-bottom:.0001pt'>   }<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Here, the size argument to memcpy is clearly just wrong.  But consider that when FAMs are in play (as is hinted at here), designers can get confused and use the wrong size value – probably there are plenty of other circumstances where such coding errors are easy to make, and not easy to spot during review.  At present, CFE can’t catch this during compilation (unless I’ve missed something).  It can be caught by the static analysis check “alpha.unix.cstring.OutOfBounds” – but that’s rather late, rather costly, and rather noisy (which I’m sure is why it’s an alpha check and not a core check).  This seems like something that could be caught and flagged by either a diagnostic or a tidy-check…   Is that reasonable?  If not, why not?<o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>In this particular case, where we can statically see that the size of the source is sizeof(struct S), it seems like this is something we could easily warn on, using much the same logic that FORTIFY_SOURCE would use at runtime: effectively, compute `__builtin_object_size(&src)` and compare that to `sizeof(struct S) + 1`. While that seems straightforward enough, it's only going to work in the cases where the pointer arguments are simple enough that we can constant-evaluate them in the frontend. Is that general enough for what you're looking for? (I think as a general strategy, implementing warnings for all the cases that FORTIFY_SOURCE would trap on at runtime would be a great idea.)<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Alternatively, if we could get a low-false-positive version of that static analyzer check promoted out of alpha, then that would be a more general solution (and would automatically give you a clang-tidy check for this). But it seems better to trap this during compilation rather than after, in the cases where we can.<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Regards,<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><a href="https://www.ericsson.com/" target="_blank"><span style='font-size:10.0pt;font-family:"Arial",sans-serif;text-decoration:none'><img border=0 width=30 height=30 style='width:.3125in;height:.3125in' id="gmail-m_-2988126295748465746_x005f_x0000_i1026" src="https://mediabank.ericsson.net/internet-media/Email_logo_Ericsson.png"></span></a><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><b><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>Chris Hamilton</span></b><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>Compiler Developer</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>BNEW DNEW 4G5G BI BBI 10</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>Mobile: +1-512-955-0143</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'><a href="mailto:chris.hamilton@ericsson.com" target="_blank">chris.hamilton@ericsson.com</a></span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><b><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>“Without inclusion, diversity is only a statistic.” </span></b><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'> <i>-- Börje Ekholm, CEO of Ericsson</i></span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>Ericsson</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>1703 W. 5th Street Suite 600</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>78703,Austin, Texas</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'>United States</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;color:#181818'><a href="https://www.ericsson.com/" target="_blank">ericsson.com</a></span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><a href="https://www.ericsson.com/current_campaign" target="_blank"><span style='font-size:8.0pt;font-family:"Arial",sans-serif;text-decoration:none'><img border=0 width=500 height=80 style='width:5.2083in;height:.8333in' id="gmail-m_-2988126295748465746_x005f_x0000_i1025" src="https://mediabank.ericsson.net/internet-media/Email_Message.gif"></span></a><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'>Our commitment to <a href="https://www.ericsson.com/thecompany/sustainability-corporateresponsibility" target="_blank" title="https://www.ericsson.com/thecompany/sustainability-corporateresponsibility">Technology for Good</a> and <a href="https://www.ericsson.com/thecompany/diversity-inclusion" target="_blank" title="https://www.ericsson.com/thecompany/diversity-inclusion">Diversity and Inclusion</a> contributes to positive change.<br>Follow us on: <a href="https://www.facebook.com/ericsson" target="_blank" title="https://www.facebook.com/ericsson">Facebook</a> <a href="https://www.linkedin.com/company/ericsson" target="_blank" title="https://www.linkedin.com/company/ericsson">LinkedIn</a> <a href="https://twitter.com/Ericsson" target="_blank" title="https://twitter.com/Ericsson">Twitter</a><br><br>Legal entity:</span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#333333'>ERICSSON AB</span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'> registration number </span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#333333'>556056-6258</span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'>, registered office in </span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#333333'>Stockholm</span><span style='font-size:8.0pt;font-family:"Arial",sans-serif;color:#181818'>.<br>This communication is confidential. Our email terms: <a href="https://www.ericsson.com/en/legal/privacy/email-disclaimer" target="_blank" title="https://www.ericsson.com/en/legal/privacy/email-disclaimer">www.ericsson.com/en/legal/privacy/email-disclaimer</a></span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div></div><p class=MsoNormal>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></p></blockquote></div></div></div></div></body></html>