[mpich2-dev] [PATCH 4/4] Issue 4120: Fix dataloop copy to not update NULL pointers

Jeff Parker jjparker at us.ibm.com
Sun Mar 30 23:54:12 CDT 2008


Function PREPEND_PREFIX(Dataloop_copy) copies a dataloop tree.  It memcpy's 
the tree in one chunk, and then calls PREPEND_PREFIX(Dataloop_update)
which fixes up pointers in the copy by adding the difference between the 
src and dest.  But, if the pointer being fixed is NULL, the copy gets a 
non-NULL pointer, which is later used as the source of another copy, which 
ultimately causes pointer overflow, and hence, the assertion that we added
for MPI_Aint work is hit.  The fix is to only update the pointer if it is 
not NULL.

Signed-off-by: Jeff Parker <jjparker at us.ibm.com>
---
 .../src/mpid/common/datatype/dataloop/dataloop.c   |  112 +++++++++++++-------
 1 files changed, 72 insertions(+), 40 deletions(-)

diff --git a/lib/mpi/mpich2/src/mpid/common/datatype/dataloop/dataloop.c b/lib/mpi/mpich2/src/mpid/common/datatype/dataloop/dataloop.c
index 82f0530..054094b 100644
--- a/lib/mpi/mpich2/src/mpid/common/datatype/dataloop/dataloop.c
+++ b/lib/mpi/mpich2/src/mpid/common/datatype/dataloop/dataloop.c
@@ -114,7 +114,7 @@ void PREPEND_PREFIX(Dataloop_copy)(void *dest,
      * adding this difference to them. This way we can just copy the
      * structure, including pointers, in one big block.
      */
-    ptrdiff = MPI_VOID_PTR_CAST_TO_MPI_AINT ( (char *) dest - (char *) src );
+    ptrdiff = (DLOOP_Offset) ( (char *) dest - (char *) src );
 
     /* traverse structure updating pointers */
     PREPEND_PREFIX(Dataloop_update)(dest, ptrdiff);
@@ -152,72 +152,104 @@ void PREPEND_PREFIX(Dataloop_update)(DLOOP_Dataloop *dataloop,
 	     * LHS, so we get this much nastier form instead (using common
 	     * struct for contig and vector): 
 	     */
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.cm_t.dataloop + ptrdiff); 
-	    dataloop->loop_params.cm_t.dataloop = 
-		(DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.cm_t.dataloop + ptrdiff);
+
+	    if (dataloop->loop_params.cm_t.dataloop)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.cm_t.dataloop + ptrdiff); 
+
+		dataloop->loop_params.cm_t.dataloop = 
+		    (DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.cm_t.dataloop + ptrdiff);
+	    }
 
 	    if (!(dataloop->kind & DLOOP_FINAL_MASK))
 		PREPEND_PREFIX(Dataloop_update)(dataloop->loop_params.cm_t.dataloop, ptrdiff);
 	    break;
 
 	case DLOOP_KIND_BLOCKINDEXED:
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.offset_array + ptrdiff);
-	    dataloop->loop_params.bi_t.offset_array = 
-		(DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.offset_array + ptrdiff);
+	    if (dataloop->loop_params.bi_t.offset_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.offset_array + ptrdiff);
+		dataloop->loop_params.bi_t.offset_array = 
+		    (DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.offset_array + ptrdiff);
+	    }
 
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.dataloop + ptrdiff);
-	    dataloop->loop_params.bi_t.dataloop = 
-		(DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.dataloop + ptrdiff);
+	    if (dataloop->loop_params.bi_t.dataloop)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.dataloop + ptrdiff);
+		dataloop->loop_params.bi_t.dataloop = 
+		    (DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.bi_t.dataloop + ptrdiff);
+	    }
 
 	    if (!(dataloop->kind & DLOOP_FINAL_MASK))
 		PREPEND_PREFIX(Dataloop_update)(dataloop->loop_params.bi_t.dataloop, ptrdiff);
 	    break;
 
 	case DLOOP_KIND_INDEXED:
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.blocksize_array + ptrdiff);
-	    dataloop->loop_params.i_t.blocksize_array = 
-		(DLOOP_Count *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.blocksize_array + ptrdiff);
+	    if (dataloop->loop_params.i_t.blocksize_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.blocksize_array + ptrdiff);
+		dataloop->loop_params.i_t.blocksize_array = 
+		    (DLOOP_Count *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.blocksize_array + ptrdiff);
+	    }
 
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.offset_array + ptrdiff);
-	    dataloop->loop_params.i_t.offset_array = 
-		(DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.offset_array + ptrdiff);
+	    if (dataloop->loop_params.i_t.offset_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.offset_array + ptrdiff);
+		dataloop->loop_params.i_t.offset_array = 
+		    (DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.offset_array + ptrdiff);
+	    }
 
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.dataloop + ptrdiff);
-	    dataloop->loop_params.i_t.dataloop = 
-		(DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.dataloop + ptrdiff);
+	    if (dataloop->loop_params.i_t.dataloop)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.dataloop + ptrdiff);
+		dataloop->loop_params.i_t.dataloop = 
+		    (DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.i_t.dataloop + ptrdiff);
+	    }
 
 	    if (!(dataloop->kind & DLOOP_FINAL_MASK))
 		PREPEND_PREFIX(Dataloop_update)(dataloop->loop_params.i_t.dataloop, ptrdiff);
 	    break;
 
 	case DLOOP_KIND_STRUCT:
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.blocksize_array + ptrdiff);
-	    dataloop->loop_params.s_t.blocksize_array = 
-		(DLOOP_Count *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.blocksize_array + ptrdiff);
+	    if (dataloop->loop_params.s_t.blocksize_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.blocksize_array + ptrdiff);
+		dataloop->loop_params.s_t.blocksize_array = 
+		    (DLOOP_Count *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.blocksize_array + ptrdiff);
+	    }
 
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.offset_array + ptrdiff);
-	    dataloop->loop_params.s_t.offset_array = 
-		(DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.offset_array + ptrdiff);
+	    if (dataloop->loop_params.s_t.offset_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.offset_array + ptrdiff);
+		dataloop->loop_params.s_t.offset_array = 
+		    (DLOOP_Offset *) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.offset_array + ptrdiff);
+	    }
 
-	    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.dataloop_array + ptrdiff);
-	    dataloop->loop_params.s_t.dataloop_array = 
-		(DLOOP_Dataloop **) MPI_AINT_CAST_TO_VOID_PTR
-		(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.dataloop_array + ptrdiff);
+	    if (dataloop->loop_params.s_t.dataloop_array)
+	    {
+		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.dataloop_array + ptrdiff);
+		dataloop->loop_params.s_t.dataloop_array = 
+		    (DLOOP_Dataloop **) MPI_AINT_CAST_TO_VOID_PTR
+		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) dataloop->loop_params.s_t.dataloop_array + ptrdiff);
+	    }
 
 	    /* fix the N dataloop pointers too */
 	    looparray = dataloop->loop_params.s_t.dataloop_array;
 	    for (i=0; i < dataloop->loop_params.s_t.count; i++) {
-		MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) looparray[i] + ptrdiff);
-		looparray[i] = (DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
-		    (MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) looparray[i] + ptrdiff);
+		if (looparray[i])
+		{
+		    MPID_Ensure_Aint_fits_in_pointer(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) looparray[i] + ptrdiff);
+		    looparray[i] = (DLOOP_Dataloop *) MPI_AINT_CAST_TO_VOID_PTR
+			(MPI_VOID_PTR_CAST_TO_MPI_AINT (char *) looparray[i] + ptrdiff);
+		}
 	    }
 
 	    if (dataloop->kind & DLOOP_FINAL_MASK) break;
-- 
1.5.3.7




More information about the mpich2-dev mailing list