Skip to content

Commit 65cec3e

Browse files
bosilcaggouaillardet
authored andcommitted
Validate the packed data
Use htonl and htons to check that the packed data is indeed the correct data. (cherry picked from commit 383b78d)
1 parent f3195ef commit 65cec3e

File tree

1 file changed

+149
-22
lines changed

1 file changed

+149
-22
lines changed

test/datatype/external32.c

Lines changed: 149 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <mpi.h>
16+
#include <arpa/inet.h>
17+
18+
static int verbose = 0;
1619

1720
void dump_hex(void* what, size_t length)
1821
{
@@ -22,8 +25,82 @@ void dump_hex(void* what, size_t length)
2225
}
2326
}
2427

28+
typedef int (*checker_t)(void*, void*, MPI_Datatype, int, void*);
29+
30+
int check_contiguous( void* send_buffer, void* packed,
31+
MPI_Datatype datatype, int count, void* arg )
32+
{
33+
int i, resultlen;
34+
char typename[MPI_MAX_OBJECT_NAME];
35+
36+
MPI_Type_get_name(datatype, typename, &resultlen);
37+
38+
if( (datatype == MPI_INT) || (datatype == MPI_INT32_T) ) {
39+
uint32_t val;
40+
for( i = 0 ; i < count; i++ ) {
41+
val = htonl(((uint32_t*)send_buffer)[i]);
42+
if( val != ((uint32_t*)packed)[i] ) {
43+
printf("Error at position %d expected %x found %x (type %s)\n",
44+
i, ((uint32_t*)packed)[i], ((uint32_t*)send_buffer)[i], typename);
45+
return -1;
46+
}
47+
}
48+
} else if( (datatype == MPI_SHORT) || (datatype == MPI_INT16_T) ) {
49+
uint16_t val;
50+
for( i = 0 ; i < count; i++ ) {
51+
val = htons(((uint16_t*)send_buffer)[i]);
52+
if( val != ((uint16_t*)packed)[i] ) {
53+
printf("Error at position %d expected %x found %x (type %s)\n",
54+
i, ((uint16_t*)packed)[i], ((uint16_t*)send_buffer)[i], typename);
55+
return -1;
56+
}
57+
}
58+
} else {
59+
printf("Unknown type\n");
60+
return -1;
61+
}
62+
return 0;
63+
}
64+
65+
int check_vector( void* send_buffer, void* packed,
66+
MPI_Datatype datatype, int count, void* arg )
67+
{
68+
int i, resultlen;
69+
char typename[MPI_MAX_OBJECT_NAME];
70+
MPI_Datatype origtype = (MPI_Datatype)arg;
71+
72+
MPI_Type_get_name(datatype, typename, &resultlen);
73+
74+
if( (origtype == MPI_INT) || (origtype == MPI_INT32_T) ) {
75+
uint32_t val;
76+
for( i = 0 ; i < count; i++ ) {
77+
val = htonl(((uint32_t*)send_buffer)[2*i]);
78+
if( val != ((uint32_t*)packed)[i] ) {
79+
printf("Error at position %d expected %x found %x (type %s)\n",
80+
i, ((uint32_t*)packed)[i], ((uint32_t*)send_buffer)[2*i], typename);
81+
return -1;
82+
}
83+
}
84+
} else if( (origtype == MPI_SHORT) || (origtype == MPI_INT16_T) ) {
85+
uint16_t val;
86+
for( i = 0 ; i < count; i++ ) {
87+
val = htons(((uint16_t*)send_buffer)[2*i]);
88+
if( val != ((uint16_t*)packed)[i] ) {
89+
printf("Error at position %d expected %x found %x (type %s)\n",
90+
i, ((uint16_t*)packed)[i], ((uint16_t*)send_buffer)[2*i], typename);
91+
return -1;
92+
}
93+
}
94+
} else {
95+
printf("Unknown %s type\n", typename);
96+
return -1;
97+
}
98+
return 0;
99+
}
100+
25101
int pack_unpack_datatype( void* send_data, MPI_Datatype datatype, int count,
26-
void* recv_data )
102+
void* recv_data,
103+
checker_t validator, void* validator_arg)
27104
{
28105
MPI_Aint position = 0, buffer_size;
29106
void* buffer;
@@ -39,6 +116,10 @@ int pack_unpack_datatype( void* send_data, MPI_Datatype datatype, int count,
39116
error = MPI_Pack_external("external32", (void*)send_data, count, datatype,
40117
buffer, buffer_size, &position);
41118
if( MPI_SUCCESS != error ) goto return_error_code;
119+
if( 0 != validator(send_data, buffer, datatype, count, validator_arg) ) {
120+
printf("Error during pack external. Bailing out\n");
121+
return -1;
122+
}
42123

43124
printf("packed "); dump_hex(buffer, buffer_size); printf("\n");
44125

@@ -56,44 +137,90 @@ int main(int argc, char *argv[])
56137
{
57138
MPI_Init(&argc, &argv);
58139

59-
/* Simple contiguous data */
140+
/* Simple contiguous data: MPI_INT32_T */
60141
{
61-
int send_data[2] = {1234, 5678};
62-
int recv_data[2] = {-1, -1};
63-
64-
printf("send data %08x %08x \n", send_data[0], send_data[1]);
65-
printf("data "); dump_hex(&send_data, sizeof(int) * 2); printf("\n");
142+
int32_t send_data[2] = {1234, 5678};
143+
int32_t recv_data[2] = {-1, -1};
66144

67-
(void)pack_unpack_datatype( send_data, MPI_INT, 2,
68-
recv_data );
145+
if( verbose ) {
146+
printf("send data %08x %08x \n", send_data[0], send_data[1]);
147+
printf("data "); dump_hex(&send_data, sizeof(int32_t) * 2); printf("\n");
148+
}
149+
(void)pack_unpack_datatype( send_data, MPI_INT32_T, 2,
150+
recv_data, check_contiguous, (void*)(ptrdiff_t)MPI_INT32_T );
151+
if( verbose ) {
152+
printf("recv "); dump_hex(&recv_data, sizeof(int32_t) * 2); printf("\n");
153+
printf("recv data %08x %08x \n", recv_data[0], recv_data[1]);
154+
}
155+
if( (send_data[0] != recv_data[0]) || (send_data[1] != recv_data[1]) ) {
156+
printf("Error during external32 pack/unack for contiguous types (MPI_INT32_T)\n");
157+
exit(-1);
158+
}
159+
}
160+
/* Simple contiguous data: MPI_INT16_T */
161+
{
162+
int16_t send_data[2] = {1234, 5678};
163+
int16_t recv_data[2] = {-1, -1};
69164

70-
printf("recv "); dump_hex(&recv_data, sizeof(int) * 2); printf("\n");
71-
printf("recv data %08x %08x \n", recv_data[0], recv_data[1]);
165+
if( verbose ) {
166+
printf("send data %08x %08x \n", send_data[0], send_data[1]);
167+
printf("data "); dump_hex(&send_data, sizeof(int16_t) * 2); printf("\n");
168+
}
169+
(void)pack_unpack_datatype( send_data, MPI_INT16_T, 2,
170+
recv_data, check_contiguous, (void*)(ptrdiff_t)MPI_INT16_T );
171+
if( verbose ) {
172+
printf("recv "); dump_hex(&recv_data, sizeof(int16_t) * 2); printf("\n");
173+
printf("recv data %08x %08x \n", recv_data[0], recv_data[1]);
174+
}
72175
if( (send_data[0] != recv_data[0]) || (send_data[1] != recv_data[1]) ) {
73-
printf("Error duing external32 pack/unack for contiguous types\n");
176+
printf("Error during external32 pack/unack for contiguous types (MPI_INT16_T)\n");
74177
exit(-1);
75178
}
76179
}
77180

78181
/* Vector datatype */
79182
printf("\n\nVector datatype\n\n");
80183
{
81-
int send_data[3] = {1234, 0, 5678};
82-
int recv_data[3] = {-1, -1, -1};
184+
int32_t send_data[3] = {1234, 0, 5678};
185+
int32_t recv_data[3] = {-1, -1, -1};
83186
MPI_Datatype ddt;
84187

85-
MPI_Type_vector(2, 1, 2, MPI_INT, &ddt);
188+
MPI_Type_vector(2, 1, 2, MPI_INT32_T, &ddt);
86189
MPI_Type_commit(&ddt);
87-
printf("send data %08x %x08x %08x \n", send_data[0], send_data[1], send_data[2]);
88-
printf("data "); dump_hex(&send_data, sizeof(int) * 3); printf("\n");
89-
90-
(void)pack_unpack_datatype( send_data, ddt, 1, recv_data );
190+
if( verbose ) {
191+
printf("send data %08x %x08x %08x \n", send_data[0], send_data[1], send_data[2]);
192+
printf("data "); dump_hex(&send_data, sizeof(int32_t) * 3); printf("\n");
193+
}
194+
(void)pack_unpack_datatype( send_data, ddt, 1, recv_data, check_vector, (void*)(ptrdiff_t)MPI_INT32_T );
195+
if( verbose ) {
196+
printf("recv "); dump_hex(&recv_data, sizeof(int32_t) * 3); printf("\n");
197+
printf("recv data %08x %08x %08x \n", recv_data[0], recv_data[1], recv_data[2]);
198+
}
199+
MPI_Type_free(&ddt);
200+
if( (send_data[0] != recv_data[0]) || (send_data[2] != recv_data[2]) ) {
201+
printf("Error duing external32 pack/unack for vector types (MPI_INT32_T)\n");
202+
exit(-1);
203+
}
204+
}
205+
{
206+
int16_t send_data[3] = {1234, 0, 5678};
207+
int16_t recv_data[3] = {-1, -1, -1};
208+
MPI_Datatype ddt;
91209

92-
printf("recv "); dump_hex(&recv_data, sizeof(int) * 3); printf("\n");
93-
printf("recv data %08x %08x %08x \n", recv_data[0], recv_data[1], recv_data[2]);
210+
MPI_Type_vector(2, 1, 2, MPI_INT16_T, &ddt);
211+
MPI_Type_commit(&ddt);
212+
if( verbose ) {
213+
printf("send data %08x %x08x %08x \n", send_data[0], send_data[1], send_data[2]);
214+
printf("data "); dump_hex(&send_data, sizeof(int16_t) * 3); printf("\n");
215+
}
216+
(void)pack_unpack_datatype( send_data, ddt, 1, recv_data, check_vector, (void*)(ptrdiff_t)MPI_INT16_T );
217+
if( verbose ) {
218+
printf("recv "); dump_hex(&recv_data, sizeof(int16_t) * 3); printf("\n");
219+
printf("recv data %08x %08x %08x \n", recv_data[0], recv_data[1], recv_data[2]);
220+
}
94221
MPI_Type_free(&ddt);
95222
if( (send_data[0] != recv_data[0]) || (send_data[2] != recv_data[2]) ) {
96-
printf("Error duing external32 pack/unack for vector types\n");
223+
printf("Error duing external32 pack/unack for vector types (MPI_INT16_T)\n");
97224
exit(-1);
98225
}
99226
}

0 commit comments

Comments
 (0)