chapel-lang/chapel — pull request #746 by hildeth (2014-11-04 23:16:44+00:00)
Title: Fix overlapping memory in memcpy() reported by valgrind.

This was a slam-dunk. I replaced every direct call to memcpy() in our runtime code with a
call to chpl_memcpy(), then defined the latter as an always-inlined function that calls
through to the real memcpy().

The symptoms of the valgrind error are that the source and destination address are the
same (as I suspected). So I added an assert for that error case and tracked down the
caller. In chpl_gen_comm_get, there is the possibility that the source and destination
address are the same, if the data being fetched resides on the same locale. That is where
I fixed the problem.

It would be possible to also fix the problem in the calling module code, but there we
would have to add a primitive to perform pointer comparison. It is far easier to do this
in the runtime code.

I concluded that it would be helpful to leave the chpl_memcpy wrapper in place. If NDEBUG
is true, then the assert will expand to nothing, and the whole routine will inline to a
direct call to memcpy. So the runtime cost of this extra code should be nil (with
optimizations enabled).