<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><div dir="ltr">I would be happy to see this discussion continue on the MPICH devel list even if it gets into LLVM weeds. Tools for ensuring count-safety are vital to MPICH and related efforts in the MPI community, and I think many of the people who are subscribed to <a href="mailto:devel@mpich.org">devel@mpich.org</a> would be interested in this topic. At the very least, they should tolerate it, since everyone on that list knows how to use email filters ;-)<div><br>Best,</div><div><br></div><div>Jeff</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 1, 2015 at 10:35 AM, Ignacio Laguna <span dir="ltr"><<a href="mailto:lagunaperalt1@llnl.gov" target="_blank">lagunaperalt1@llnl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Rob,<br>
<br>
Thanks for replying! I'm answering your questions inline below.<span class=""><br>
<br>
On 10/1/15 9:15 AM, Rob Latham wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
On 09/30/2015 04:08 PM, Ignacio Laguna wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I found some 'potential' integer overflows when running mpich at large<br>
scale and/or with large inputs in gather.c. I believe that they are<br>
related to ticket #1767<br>
(<a href="http://trac.mpich.org/projects/mpich/ticket/1767" rel="noreferrer" target="_blank">http://trac.mpich.org/projects/mpich/ticket/1767</a>), but I didn't see any<br>
other bug reports about them so I thought I should confirm with mpich<br>
developers.<br>
<br>
In addition to this case in line 171:<br>
<br>
98 int tmp_buf_size, missing;<br>
...<br>
169 if (nbytes < MPIR_CVAR_GATHER_VSMALL_MSG_SIZE)<br>
tmp_buf_size++;<br>
170<br>
171 tmp_buf_size *= nbytes;<br>
<br>
which I believe it's fixed in mpich-3.2b4 (where tmp_buf_size is<br>
declared as a 64-bit MPI_Aint),<br>
</blockquote>
<br>
I fixed this (I sure hope!) over the summer in commit 68f8c7aa798f7009<br>
<br>
</blockquote>
<br></span>
Yes, this is fixed (according to my analysis), so don't worry, I just used it as a reference.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I found the following cases in the same<br>
file src/mpi/coll/gather.c (in both mpich-3.1.4 and mpich-3.2b4):<br>
<br>
Case 1:<br>
<br>
222 mpi_errno = MPIC_Recv(((char *)recvbuf +<br>
223 (((rank + mask) % comm_size)*recvcount*extent)),<br>
224 recvblks * recvcount, recvtype, src,<br>
225 MPIR_GATHER_TAG, comm,<br>
226 &status, errflag);<br>
<br>
In line 223 I believe we get an integer overflow as follows. Suppose I<br>
run 2^20 = 1,048,576 ranks and do a gather with 4,096 elements. In this<br>
case (if I understand the algorithm well), ((rank + mask) % comm_size)<br>
would be 2^20 / 2 = 524,288, and recvcount = 4,096. Then the ((rank +<br>
mask) % comm_size)*recvcount expression would overflow: 524,288 * 4,096<br>
= 2,147,483,648, and become negative.<br>
</blockquote>
<br>
We're doing address math here, so I hope the compiler is not trying to<br>
store intermediate results in an integer. However, if LLVM is saying<br>
otherwise, then sure, let's promote those types.<br>
<br>
((rank + mask) % comm_size)*recvcount is of type int*int, which is<br>
then multiplied by an MPI_Aint... yeah, looks suspicious.<br>
</blockquote>
<br></span>
Even when they are intermediate operations, the compiler could place the result of ((rank + mask) % comm_size)*recvcount in an 32-bit integer register. In fact, in llvm with -O0 optimization level that's what it does. This is the portion of the code in llvm IR:<br>
<br>
%mul220 = mul nsw i32 %rem219.pre, %recvcount<br>
%conv221 = sext i32 %mul220 to i64<br>
%mul222 = mul nsw i64 %conv221, %extent.1851<br>
%add.ptr223 = getelementptr inbounds i8* %recvbuf, i64 %mul222<br>
<br>
In the first line it multiplies the result of ((rank + mask) % comm_size) with recvcount. Note that this is a 32-bit multiplication (it says i32). Then in the 2nd line it extends the result to a 64-bit register (unfortunately at this point %mul220 is already wrapped around) and stores it in %conv221. Then it multiplies it by %extent, and then gets an element of the array (with getelementptr).<br>
<br>
I'm not sure if this would be a problem with higher optimization levels, though, but we should not rely on that.<br>
<br>
I also verified these things are propagated to actual machine registers by looking at the assembly code. Here's an example:<br>
<br>
$ cat test.c<br>
#include <stdio.h><br>
int main()<br>
{<br>
int val1, val2;<br>
long long tmp, final;<br>
val1 = (1 << 20) / 2;<br>
val2 = 4096;<br>
tmp = 4;<br>
final = val1 * val2 * tmp;<br>
printf("val1: %d, val2: %d, final: %lld\n", val1, val2, final);<br>
return 0;<br>
}<br>
<br>
I compiled it with clang and -O0 and this is what I get when I run it:<br>
<br>
$ ./test<br>
val1: 524288, val2: 4096, final: -8589934592<br>
<br>
This is the assembly code in my Mac:<br>
<br>
$ otool -vVt test<br>
test:<br>
(__TEXT,__text) section<br>
_main:<br>
0000000100000f00 pushq %rbp<br>
0000000100000f01 movq %rsp, %rbp<br>
0000000100000f04 subq $0x30, %rsp<br>
0000000100000f08 leaq 0x6f(%rip), %rdi<br>
0000000100000f0f movl $0x0, -0x4(%rbp)<br>
0000000100000f16 movl $0x80000, -0x8(%rbp)<br>
0000000100000f1d movl $0x1000, -0xc(%rbp)<br>
0000000100000f24 movq $0x4, -0x18(%rbp)<br>
0000000100000f2c movl -0x8(%rbp), %eax<br>
0000000100000f2f imull -0xc(%rbp), %eax<br>
0000000100000f33 movslq %eax, %rcx<br>
0000000100000f36 imulq -0x18(%rbp), %rcx<br>
<br>
The first multiplication with imull is stored in %eax, and then second one in %rcx, but at this point %eax already has an overflow.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
There are quite a few places where we do math on recvcount and<br>
recvblocks -- does your static analysis show problems with all of those<br>
spots?<br>
<br>
</blockquote>
<br></span>
I checked all MPI calls in mpich-3.2b4 and the analysis doesn't report anything else. Now, this doesn't mean there are no more bugs--just that it could not find anything else :-)<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
When multiplied with 'extent', which is size_t or MPI_Aint, it will<br>
become negative I believe or a huge positive which in any case will<br>
point to the wrong location in the recvbuf buffer, unless of course this<br>
wraparound behavior is intended.<br>
</blockquote>
<br>
Nope, wraparound sure is not intended! We would have no hope of<br>
cleaning up all these locations if it were not for clang's<br>
-Wshorten64-to-32 flag.<br>
<br>
</blockquote>
<br></span>
Thanks! I was just double checking. In some programs wraparounds are actually intended.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Case 2:<br>
<br>
There might be a similar problem in line 224 in the above code. With<br>
2^20 ranks, recvblks becomes 524,288 (again if I understand well the<br>
algorithm), so the recvblks * recvcount operation will also overflow.<br>
<br>
I might be wrong on this -- I'm catching these issues with LLVM symbolic<br>
analysis -- so they can be totally false positives,<br>
</blockquote>
<br>
Tell me more about LLVM symbolic analysis! We have started using<br>
coverity for static analysis but yes, there are a lot of messages<br>
obscuring the genuine bugs.<br>
</blockquote>
<br></span>
I can tell you more in a separate email (I know this mailing list is only for mpich discussions).<br>
<br>
Basically we are looking for scale-dependent bugs, i.e., bugs that will only manifest at large scale and/or with large inputs. It does data-flow analysis to understand what code is dependent on things like the size of the problem (in mpich and/or MPI apps this is basically code that depends on the size of the communicators, for example). It then performs tests to see if things could go wrong at large scale, e.g., by replacing ranks = 1 million values and so on. And then it reports what line of the code and file the problem is.<br>
<br>
Having said that, coverity should find much more bugs. This is very specialized for scale-dependent bugs.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
but I just wanted to<br>
check with the mpich developers if they are valid issues or not. If they<br>
are, I believe fixes can be easy to implement (just make all these<br>
computations size_t).<br>
</blockquote>
<br>
I promoted the prototype for MPIC_Recv to this:<br>
<br>
int MPIC_Recv(void *buf, MPI_Aint count,<br>
MPI_Datatype datatype, int source, int tag,<br>
MPID_Comm *comm_ptr, MPI_Status *status,<br>
MPIR_Errflag_t *errflag)<br>
<br>
Top-level MPI routines are stuck using an 'int' count, but internally we<br>
can (and now do) use a larger MPI_Aint type.<br>
<br>
True, int*int can overflow, but since that parameter to MPIC_Recv is of<br>
the larger MPI_Aint type, we're ok here. LLVM disagrees?<br>
<br>
</blockquote>
<br></span>
Once you have a version I can download with the patches, I can run the analysis again and see if I can catch more things. Just let me know and I would be happy to test it.<br>
<br>
In general, overflows can also occur in intermediate computations, depending on how code is optimized, I believe.<br>
<br>
Thanks!<br>
<br>
Ignacio<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
==rob<br>
<br>
</blockquote><div class="HOEnZb"><div class="h5">
_______________________________________________<br>
discuss mailing list <a href="mailto:discuss@mpich.org" target="_blank">discuss@mpich.org</a><br>
To manage subscription options or unsubscribe:<br>
<a href="https://lists.mpich.org/mailman/listinfo/discuss" rel="noreferrer" target="_blank">https://lists.mpich.org/mailman/listinfo/discuss</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Jeff Hammond<br><a href="mailto:jeff.science@gmail.com" target="_blank">jeff.science@gmail.com</a><br><a href="http://jeffhammond.github.io/" target="_blank">http://jeffhammond.github.io/</a></div>
</div>