-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathsanity.cpp
More file actions
1588 lines (1243 loc) · 44.2 KB
/
Copy pathsanity.cpp
File metadata and controls
1588 lines (1243 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <check.h>
#include <errno.h>
#include <sys/queue.h>
using namespace std;
#include "gdrapi.h"
#include "gdrapi_internal.h"
#include "gdrconfig.h"
#include "common.hpp"
using namespace gdrcopy::test;
volatile bool expecting_exception_signal = false;
int gpu_id = 0;
void exception_signal_handle(int sig)
{
if (expecting_exception_signal) {
print_dbg("Get signal %d as expected\n", sig);
exit(EXIT_SUCCESS);
}
print_dbg("Unexpectedly get exception signal");
}
static void init_cuda(int dev_id)
{
CUdevice dev;
CUcontext dev_ctx;
ASSERTDRV(cuInit(0));
ASSERTDRV(cuDeviceGet(&dev, dev_id));
ASSERTDRV(cuDevicePrimaryCtxRetain(&dev_ctx, dev));
ASSERTDRV(cuCtxSetCurrent(dev_ctx));
}
static void finalize_cuda(int dev_id)
{
CUdevice dev;
ASSERTDRV(cuDeviceGet(&dev, dev_id));
ASSERTDRV(cuDevicePrimaryCtxRelease(dev));
}
/**
* Sends given file descriptior via given socket
*
* @param socket to be used for fd sending
* @param fd to be sent
* @return sendmsg result
*
* @note socket should be (PF_UNIX, SOCK_DGRAM)
*/
int sendfd(int socket, int fd) {
char dummy = '$';
struct msghdr msg;
struct iovec iov;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
iov.iov_base = &dummy;
iov.iov_len = sizeof(dummy);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = cmsgbuf;
msg.msg_controllen = CMSG_LEN(sizeof(int));
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*(int*) CMSG_DATA(cmsg) = fd;
int ret = sendmsg(socket, &msg, 0);
if (ret == -1) {
print_dbg("sendmsg failed with %s", strerror(errno));
}
return ret;
}
/**
* Receives file descriptor using given socket
*
* @param socket to be used for fd recepion
* @return received file descriptor; -1 if failed
*
* @note socket should be (PF_UNIX, SOCK_DGRAM)
*/
int recvfd(int socket) {
int len;
int fd;
char buf[1];
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
char cms[CMSG_SPACE(sizeof(int))];
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
msg.msg_name = 0;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = (caddr_t) cms;
msg.msg_controllen = sizeof cms;
len = recvmsg(socket, &msg, 0);
if (len < 0) {
print_dbg("recvmsg failed with %s", strerror(errno));
return -1;
}
if (len == 0) {
print_dbg("recvmsg failed no data");
return -1;
}
cmsg = CMSG_FIRSTHDR(&msg);
memmove(&fd, CMSG_DATA(cmsg), sizeof(int));
return fd;
}
BEGIN_GDRCOPY_TEST(basic)
{
expecting_exception_signal = false;
MB();
init_cuda(gpu_id);
const size_t _size = 256*1024+16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
print_dbg("buffer size: %zu\n", size);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh = null_mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
ASSERTDRV(gpuMemFree(d_A));
finalize_cuda(0);
}
END_GDRCOPY_TEST
BEGIN_GDRCOPY_TEST(basic_with_tokens)
{
expecting_exception_signal = false;
MB();
init_cuda(gpu_id);
const size_t _size = 256*1024+16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
print_dbg("buffer size: %zu\n", size);
CUdeviceptr d_A;
CUDA_POINTER_ATTRIBUTE_P2P_TOKENS tokens = {0,0};
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuPointerGetAttribute(&tokens, CU_POINTER_ATTRIBUTE_P2P_TOKENS, d_A));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh = null_mh;
CUdeviceptr d_ptr = d_A;
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, tokens.p2pToken, tokens.vaSpaceToken, &mh), 0);
ASSERT_NEQ(mh, null_mh);
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
ASSERTDRV(gpuMemFree(d_A));
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test ensures that gdrcopy returns error when trying to map
* unaligned addresses. In addition, it tests that mapping hand-aligned
* addresses by users are successful.
*
*/
BEGIN_GDRCOPY_TEST(basic_unaligned_mapping)
{
expecting_exception_signal = false;
MB();
init_cuda(gpu_id);
// Allocate for a few bytes so that cuMemAlloc returns an unaligned address
// in the next allocation. This behavior is observed in GPU Driver 410 and
// above.
const size_t fa_size = 4;
CUdeviceptr d_fa;
ASSERTDRV(gpuMemAlloc(&d_fa, fa_size));
print_dbg("First allocation: d_fa=0x%llx, size=%zu\n", d_fa, fa_size);
const size_t A_size = GPU_PAGE_SIZE + sizeof(int);
CUdeviceptr d_A, d_A_boundary;
// Try until we get an unaligned address. Give up after 100 times.
for (int i = 0; i < 100; ++i) {
ASSERTDRV(gpuMemAlloc(&d_A, A_size, false, true));
d_A_boundary = d_A & GPU_PAGE_MASK;
if (d_A != d_A_boundary)
break;
}
print_dbg("Second allocation: d_A=0x%llx, size=%zu, GPU-page-boundary 0x%llx\n", d_A, A_size, d_A_boundary);
if (d_A == d_A_boundary) {
print_dbg("d_A is aligned. Waiving this test.\n");
ASSERTDRV(cuMemFree(d_A));
exit(EXIT_WAIVED);
}
print_dbg("d_A is unaligned\n");
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
// Try mapping with unaligned address. This should fail.
print_dbg("Try mapping d_A as is.\n");
gdr_mh_t A_mh = null_mh;
ASSERT_EQ(gdr_pin_buffer(g, d_A, A_size, 0, 0, &A_mh), 0);
ASSERT_NEQ(A_mh, null_mh);
void *A_bar_ptr = NULL;
// Expect gdr_map to fail with unaligned address
ASSERT_NEQ(gdr_map(g, A_mh, &A_bar_ptr, A_size), 0);
ASSERT_EQ(gdr_unpin_buffer(g, A_mh), 0);
print_dbg("Mapping d_A failed as expected.\n");
print_dbg("Align d_A and try mapping it again.\n");
// In order to align d_A, we move to the next GPU page. The reason is that
// the first GPU page may belong to another allocation.
CUdeviceptr d_aligned_A = (d_A + GPU_PAGE_SIZE) & GPU_PAGE_MASK;
off_t aligned_A_offset = d_aligned_A - d_A;
size_t aligned_A_size = A_size - aligned_A_offset;
print_dbg("Pin and map aligned address: d_aligned_A=0x%llx, offset=%lld, size=%zu\n", d_aligned_A, aligned_A_offset, aligned_A_size);
gdr_mh_t aligned_A_mh = null_mh;
void *aligned_A_bar_ptr = NULL;
ASSERT_EQ(gdr_pin_buffer(g, d_aligned_A, aligned_A_size, 0, 0, &aligned_A_mh), 0);
ASSERT_NEQ(aligned_A_mh, null_mh);
// expect gdr_map to success
ASSERT_EQ(gdr_map(g, aligned_A_mh, &aligned_A_bar_ptr, aligned_A_size), 0);
// Test accessing the mapping
int *aligned_A_map_ptr = (int *)aligned_A_bar_ptr;
aligned_A_map_ptr[0] = 7;
// The first allocation and d_A should share a GPU page. We should make
// sure that freeing the first allocation would not accidentally unmap
// d_aligned_A as the d_aligned_A mapping starts from the next GPU page.
gdr_mh_t fa_mh = null_mh;
ASSERT_EQ(gdr_pin_buffer(g, d_fa, fa_size, 0, 0, &fa_mh), 0);
ASSERT_NEQ(fa_mh, null_mh);
void *fa_bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, fa_mh, &fa_bar_ptr, fa_size), 0);
ASSERTDRV(gpuMemFree(d_fa));
// Test accessing aligned_A_map_ptr again. This should not cause segmentation fault.
aligned_A_map_ptr[0] = 9;
ASSERT_EQ(gdr_unpin_buffer(g, aligned_A_mh), 0);
ASSERT_EQ(gdr_close(g), 0);
ASSERTDRV(gpuMemFree(d_A));
finalize_cuda(0);
}
END_GDRCOPY_TEST
BEGIN_GDRCOPY_TEST(data_validation)
{
expecting_exception_signal = false;
MB();
init_cuda(gpu_id);
const size_t _size = 256*1024+16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
print_dbg("buffer size: %zu\n", size);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuMemsetD8(d_A, 0xA5, size));
ASSERTDRV(cuCtxSynchronize());
uint32_t *init_buf = new uint32_t[size];
uint32_t *copy_buf = new uint32_t[size];
init_hbuf_walking_bit(init_buf, size);
memset(copy_buf, 0xA5, size * sizeof(*copy_buf));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
ASSERT(!info.mapped);
void *bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, mh, &bar_ptr, size), 0);
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
ASSERT(info.mapped);
int off = d_ptr - info.va;
print_dbg("off: %d\n", off);
uint32_t *buf_ptr = (uint32_t *)((char *)bar_ptr + off);
print_dbg("check 1: MMIO CPU initialization + read back via cuMemcpy D->H\n");
init_hbuf_walking_bit(buf_ptr, size);
ASSERTDRV(cuMemcpyDtoH(copy_buf, d_ptr, size));
ASSERT_EQ(compare_buf(init_buf, copy_buf, size), 0);
memset(copy_buf, 0xA5, size * sizeof(*copy_buf));
ASSERTDRV(cuMemsetD8(d_A, 0xA5, size));
ASSERTDRV(cuCtxSynchronize());
print_dbg("check 2: gdr_copy_to_bar() + read back via cuMemcpy D->H\n");
gdr_copy_to_mapping(mh, buf_ptr, init_buf, size);
ASSERTDRV(cuMemcpyDtoH(copy_buf, d_ptr, size));
ASSERT_EQ(compare_buf(init_buf, copy_buf, size), 0);
memset(copy_buf, 0xA5, size * sizeof(*copy_buf));
ASSERTDRV(cuMemsetD8(d_A, 0xA5, size));
ASSERTDRV(cuCtxSynchronize());
print_dbg("check 3: gdr_copy_to_bar() + read back via gdr_copy_from_bar()\n");
gdr_copy_to_mapping(mh, buf_ptr, init_buf, size);
gdr_copy_from_mapping(mh, copy_buf, buf_ptr, size);
ASSERT_EQ(compare_buf(init_buf, copy_buf, size), 0);
memset(copy_buf, 0xA5, size * sizeof(*copy_buf));
ASSERTDRV(cuMemsetD8(d_A, 0xA5, size));
ASSERTDRV(cuCtxSynchronize());
int extra_dwords = 5;
int extra_off = extra_dwords * sizeof(uint32_t);
print_dbg("check 4: gdr_copy_to_bar() + read back via gdr_copy_from_bar() + %d dwords offset\n", extra_dwords);
gdr_copy_to_mapping(mh, buf_ptr + extra_dwords, init_buf, size - extra_off);
gdr_copy_from_mapping(mh, copy_buf, buf_ptr + extra_dwords, size - extra_off);
ASSERT_EQ(compare_buf(init_buf, copy_buf, size - extra_off), 0);
memset(copy_buf, 0xA5, size * sizeof(*copy_buf));
ASSERTDRV(cuMemsetD8(d_A, 0xA5, size));
ASSERTDRV(cuCtxSynchronize());
extra_off = 11;
print_dbg("check 5: gdr_copy_to_bar() + read back via gdr_copy_from_bar() + %d bytes offset\n", extra_off);
gdr_copy_to_mapping(mh, (char*)buf_ptr + extra_off, init_buf, size - extra_off);
gdr_copy_from_mapping(mh, copy_buf, (char*)buf_ptr + extra_off, size - extra_off);
ASSERT_EQ(compare_buf(init_buf, copy_buf, size - extra_off), 0);
print_dbg("unmapping\n");
ASSERT_EQ(gdr_unmap(g, mh, bar_ptr, size), 0);
print_dbg("unpinning\n");
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
ASSERTDRV(gpuMemFree(d_A));
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test ensures that accessing to gdr_map'ed region is not possible
* after gdr_close.
*
* Step:
* 1. Initialize CUDA and gdrcopy
* 2. Do gdr_map(..., &bar_ptr, ...)
* 3. Do gdr_close
* 4. Attempt to access to bar_ptr after 3. should fail
*/
BEGIN_GDRCOPY_TEST(invalidation_access_after_gdr_close)
{
expecting_exception_signal = false;
MB();
struct sigaction act;
act.sa_handler = exception_signal_handle;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGBUS, &act, 0);
srand(time(NULL));
const size_t _size = sizeof(int) * 16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
int mydata = (rand() % 1000) + 1;
init_cuda(gpu_id);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuMemsetD8(d_A, 0x95, size));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
print_dbg("Mapping bar1\n");
void *bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, mh, &bar_ptr, size), 0);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
int off = d_ptr - info.va;
volatile int *buf_ptr = (volatile int *)((char *)bar_ptr + off);
// Write data
print_dbg("Writing %d into buf_ptr[0]\n", mydata);
buf_ptr[0] = mydata;
print_dbg("Calling gdr_close\n");
ASSERT_EQ(gdr_close(g), 0);
print_dbg("Trying to read buf_ptr[0] after gdr_close\n");
expecting_exception_signal = true;
MB();
int data_from_buf_ptr = buf_ptr[0];
MB();
expecting_exception_signal = false;
MB();
ASSERT_NEQ(data_from_buf_ptr, mydata);
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test ensures that accessing to gdr_map'ed region is not possible
* after cuMemFree.
*
* Step:
* 1. Initialize CUDA and gdrcopy
* 2. Do gdr_map(..., &bar_ptr, ...)
* 3. Do cuMemFree
* 4. Attempt to access to bar_ptr after 3. should fail
*/
BEGIN_GDRCOPY_TEST(invalidation_access_after_cumemfree)
{
expecting_exception_signal = false;
MB();
struct sigaction act;
act.sa_handler = exception_signal_handle;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGBUS, &act, 0);
srand(time(NULL));
const size_t _size = sizeof(int) * 16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
int mydata = (rand() % 1000) + 1;
init_cuda(gpu_id);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuMemsetD8(d_A, 0x95, size));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
print_dbg("Mapping bar1\n");
void *bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, mh, &bar_ptr, size), 0);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
int off = d_ptr - info.va;
volatile int *buf_ptr = (volatile int *)((char *)bar_ptr + off);
// Write data
print_dbg("Writing %d into buf_ptr[0]\n", mydata);
buf_ptr[0] = mydata;
print_dbg("Calling cuMemFree\n");
ASSERTDRV(gpuMemFree(d_A));
print_dbg("Trying to read buf_ptr[0] after cuMemFree\n");
expecting_exception_signal = true;
MB();
int data_from_buf_ptr = buf_ptr[0];
MB();
expecting_exception_signal = false;
MB();
ASSERT_NEQ(data_from_buf_ptr, mydata);
ASSERT_EQ(gdr_unmap(g, mh, bar_ptr, size), 0);
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test ensures that cuMemFree destroys only the mapping it is
* corresponding to.
*
* Step:
* 1. Initialize CUDA and gdrcopy
* 2. cuMemAlloc(&d_A, ...); cuMemAlloc(&d_B, ...)
* 3. Do gdr_map(..., &bar_ptr_A, ...) of d_A
* 4. Do gdr_map(..., &bar_ptr_B, ...) of d_B
* 5. Do cuMemFree(d_A)
* 6. Verify that bar_ptr_B is still accessible
*/
BEGIN_GDRCOPY_TEST(invalidation_two_mappings)
{
expecting_exception_signal = false;
MB();
srand(time(NULL));
const size_t _size = sizeof(int) * 16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
int mydata = (rand() % 1000) + 1;
init_cuda(gpu_id);
CUdeviceptr d_A[2];
for (int i = 0; i < 2; ++i) {
ASSERTDRV(gpuMemAlloc(&d_A[i], size));
ASSERTDRV(cuMemsetD8(d_A[i], 0x95, size));
}
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh[2];
volatile int *buf_ptr[2];
void *bar_ptr[2];
print_dbg("Mapping bar1\n");
for (int i = 0; i < 2; ++i) {
CUdeviceptr d_ptr = d_A[i];
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh[i]), 0);
ASSERT_NEQ(mh[i], null_mh);
bar_ptr[i] = NULL;
ASSERT_EQ(gdr_map(g, mh[i], &bar_ptr[i], size), 0);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh[i], &info), 0);
int off = d_ptr - info.va;
buf_ptr[i] = (volatile int *)((char *)bar_ptr[i] + off);
}
// Write data
print_dbg("Writing data to both mappings %d and %d respectively\n", mydata, mydata + 1);
buf_ptr[0][0] = mydata;
buf_ptr[1][0] = mydata + 1;
print_dbg("Validating that we can read the data back\n");
ASSERT_EQ(buf_ptr[0][0], mydata);
ASSERT_EQ(buf_ptr[1][0], mydata + 1);
print_dbg("cuMemFree and thus destroying the first mapping\n");
ASSERTDRV(gpuMemFree(d_A[0]));
print_dbg("Trying to read and validate the data from the second mapping after the first mapping has been destroyed\n");
ASSERT_EQ(buf_ptr[1][0], mydata + 1);
ASSERTDRV(gpuMemFree(d_A[1]));
for (int i = 0; i < 2; ++i) {
ASSERT_EQ(gdr_unmap(g, mh[i], bar_ptr[i], size), 0);
ASSERT_EQ(gdr_unpin_buffer(g, mh[i]), 0);
}
ASSERT_EQ(gdr_close(g), 0);
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test is intended to check the security hole originated from not
* doing invalidation correctly. In a nutshell, it ensures that the parent
* process cannot spy on the child process.
*
* Step:
* 1. Fork the process
* 2.C Child: Waiting for parent's signal before continue
*
* 2.P Parent: Initialize CUDA and gdrcopy
* 3.P Parent: Do gdr_map then cuMemFree without gdr_unmap
* 4.P Parent: Signal child and wait for child's signal
*
* 3.C Child: Initialize CUDA and gdrcopy
* 4.C Child: Do gdr_map, signal parent, and wait for parent's signal
*
* 5.P Parent: Check whether it can access to its gdr_map'ed data or not and
* compare with the data written by child. If gdrdrv does not handle
* invalidation properly, child's data will be leaked to parent.
*/
BEGIN_GDRCOPY_TEST(invalidation_fork_access_after_cumemfree)
{
expecting_exception_signal = false;
MB();
int filedes_0[2];
int filedes_1[2];
int read_fd;
int write_fd;
ASSERT_NEQ(pipe(filedes_0), -1);
ASSERT_NEQ(pipe(filedes_1), -1);
srand(time(NULL));
const size_t _size = sizeof(int) * 16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
const char *myname;
fflush(stdout);
fflush(stderr);
pid_t pid = fork();
ASSERT(pid >= 0);
myname = pid == 0 ? "child" : "parent";
print_dbg("%s: Start\n", myname);
if (pid == 0) {
close(filedes_0[0]);
close(filedes_1[1]);
read_fd = filedes_1[0];
write_fd = filedes_0[1];
int cont = 0;
do {
print_dbg("%s: waiting for cont signal from parent\n", myname);
ASSERT_EQ(read(read_fd, &cont, sizeof(int)), sizeof(int));
print_dbg("%s: receive cont signal %d from parent\n", myname, cont);
} while (cont != 1);
}
else {
close(filedes_0[1]);
close(filedes_1[0]);
read_fd = filedes_0[0];
write_fd = filedes_1[1];
struct sigaction act;
act.sa_handler = exception_signal_handle;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGBUS, &act, 0);
}
int mydata = (rand() % 1000) + 1;
// Make sure that parent's and child's mydata are different.
// Remember that we do srand before fork.
if (pid == 0)
mydata += 10;
init_cuda(gpu_id);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuMemsetD8(d_A, 0x95, size));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
void *bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, mh, &bar_ptr, size), 0);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
int off = d_ptr - info.va;
volatile int *buf_ptr = (volatile int *)((char *)bar_ptr + off);
print_dbg("%s: writing buf_ptr[0] with %d\n", myname, mydata);
buf_ptr[0] = mydata;
if (pid == 0) {
print_dbg("%s: signal parent that I have written\n", myname);
ASSERT_EQ(write(write_fd, &mydata, sizeof(int)), sizeof(int));
int cont = 0;
print_dbg("%s: waiting for signal from parent before calling cuMemFree\n", myname);
do {
ASSERT_NEQ(read(read_fd, &cont, sizeof(int)), -1);
} while (cont != 1);
}
print_dbg("%s: read buf_ptr[0] before cuMemFree get %d\n", myname, buf_ptr[0]);
print_dbg("%s: calling cuMemFree\n", myname);
ASSERTDRV(gpuMemFree(d_A));
if (pid > 0) {
int msg = 1;
ASSERT_EQ(write(write_fd, &msg, sizeof(int)), sizeof(int));
int child_data = 0;
print_dbg("%s: waiting for child write signal\n", myname);
do {
ASSERT_EQ(read(read_fd, &child_data, sizeof(int)), sizeof(int));
} while (child_data == 0);
print_dbg("%s: trying to read buf_ptr[0]\n", myname);
expecting_exception_signal = true;
MB();
int data_from_buf_ptr = buf_ptr[0];
MB();
expecting_exception_signal = false;
MB();
print_dbg("%s: read buf_ptr[0] after child write get %d\n", myname, data_from_buf_ptr);
print_dbg("%s: child data is %d\n", myname, child_data);
ASSERT_EQ(write(write_fd, &msg, sizeof(int)), sizeof(int));
ASSERT_NEQ(child_data, data_from_buf_ptr);
}
ASSERT_EQ(gdr_unmap(g, mh, bar_ptr, size), 0);
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test makes sure that child processes cannot spy on the parent
* process if the parent does fork without doing gdr_unmap first.
*
* Step:
* 1. Initilize CUDA and gdrcopy
* 2. Do gdr_map
* 3. Fork the process
*
* 4.P Parent: Waiting for child to exit
*
* 4.C Child: Attempt to access the gdr_map'ed data and compare with what
* parent writes into that region. If gdrdrv does not invalidate the
* mapping correctly, child can spy on parent.
*/
BEGIN_GDRCOPY_TEST(invalidation_fork_after_gdr_map)
{
expecting_exception_signal = false;
MB();
int filedes_0[2];
int filedes_1[2];
int read_fd;
int write_fd;
ASSERT_NEQ(pipe(filedes_0), -1);
ASSERT_NEQ(pipe(filedes_1), -1);
const size_t _size = sizeof(int) * 16;
const size_t size = (_size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
const char *myname;
init_cuda(gpu_id);
CUdeviceptr d_A;
ASSERTDRV(gpuMemAlloc(&d_A, size));
ASSERTDRV(cuMemsetD8(d_A, 0x95, size));
gdr_t g = gdr_open();
ASSERT_NEQ(g, (void*)0);
gdr_mh_t mh;
CUdeviceptr d_ptr = d_A;
// tokens are optional in CUDA 6.0
// wave out the test if GPUDirectRDMA is not enabled
ASSERT_EQ(gdr_pin_buffer(g, d_ptr, size, 0, 0, &mh), 0);
ASSERT_NEQ(mh, null_mh);
void *bar_ptr = NULL;
ASSERT_EQ(gdr_map(g, mh, &bar_ptr, size), 0);
gdr_info_t info;
ASSERT_EQ(gdr_get_info(g, mh, &info), 0);
int off = d_ptr - info.va;
volatile int *buf_ptr = (volatile int *)((char *)bar_ptr + off);
fflush(stdout);
fflush(stderr);
pid_t pid = fork();
ASSERT(pid >= 0);
myname = pid == 0 ? "child" : "parent";
print_dbg("%s: Start\n", myname);
srand(time(NULL));
int mynumber = rand() % 1000 + 1;
if (pid == 0) {
close(filedes_0[0]);
close(filedes_1[1]);
read_fd = filedes_1[0];
write_fd = filedes_0[1];
srand(rand());
int cont = 0;
do {
print_dbg("%s: waiting for cont signal from parent\n", myname);
ASSERT_EQ(read(read_fd, &cont, sizeof(int)), sizeof(int));
print_dbg("%s: receive cont signal %d from parent\n", myname, cont);
} while (cont != 1);
}
else {
close(filedes_0[1]);
close(filedes_1[0]);
read_fd = filedes_0[0];
write_fd = filedes_1[1];
}
if (pid > 0) {
print_dbg("%s: writing buf_ptr[0] with %d\n", myname, mynumber);
buf_ptr[0] = mynumber;
}
if (pid == 0) {
struct sigaction act;
act.sa_handler = exception_signal_handle;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGBUS, &act, 0);
sigaction(SIGSEGV, &act, 0);
expecting_exception_signal = true;
MB();
}
print_dbg("%s: trying to read buf_ptr[0]\n", myname);
int data_from_buf_ptr = buf_ptr[0];
print_dbg("%s: read buf_ptr[0] get %d\n", myname, data_from_buf_ptr);
if (pid == 0) {
MB();
expecting_exception_signal = false;
MB();
print_dbg("%s: should not be able to read buf_ptr[0] anymore!! aborting!!\n", myname);
exit(EXIT_FAILURE);
}
if (pid > 0) {
print_dbg("%s: signaling child\n", myname);
int msg = 1;
ASSERT_EQ(write(write_fd, &msg, sizeof(int)), sizeof(int));
print_dbg("%s: waiting for child to exit\n", myname);
// Child should exit because of sigbus
int child_exit_status = -EINVAL;
ASSERT(wait(&child_exit_status) == pid);
ASSERT_EQ(child_exit_status, EXIT_SUCCESS);
print_dbg("%s: trying to read buf_ptr[0] after child exits\n", myname);
data_from_buf_ptr = buf_ptr[0];
print_dbg("%s: read buf_ptr[0] after child exits get %d\n", myname, data_from_buf_ptr);
ASSERT_EQ(data_from_buf_ptr, mynumber);
ASSERT_EQ(gdr_unmap(g, mh, bar_ptr, size), 0);
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERTDRV(gpuMemFree(d_A));
ASSERT_EQ(gdr_close(g), 0);
}
finalize_cuda(0);
}
END_GDRCOPY_TEST
/**
* This unit test ensures that child cannot do gdr_map on what parent has
* prepared with gdr_pin_buffer. This situation emulates when the parent
* forgets that it has gdr_pin_buffer without gdr_map before doing fork.
*
* Step:
* 1. Initilize CUDA and gdrcopy
* 2. Do gdr_pin_buffer
* 3. Fork the process
*
* 4.P Parent: Waiting for child to exit