test_optimization.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include "../graphene-ipc.h"
  9. #define PAGE_SIZE 4096
  10. #define MAX_PIDBUF 7
  11. #define TEST_PAGES 20
  12. struct mapping {
  13. unsigned long addr;
  14. unsigned long len;
  15. unsigned long perms;
  16. unsigned long flags;
  17. };
  18. int main(int argc, char **argv)
  19. {
  20. char *pages[TEST_PAGES];
  21. int p0[2];
  22. int64_t token;
  23. if (argc == 1) {
  24. // Server
  25. int pid;
  26. struct gipc_send gs;
  27. unsigned long addr[TEST_PAGES];
  28. unsigned long len[TEST_PAGES];
  29. int gfd;
  30. int i;
  31. int rv;
  32. /* Map some anonymous memory, fill with crap */
  33. for (i = 0; i < TEST_PAGES; i++) {
  34. pages[i] = mmap(NULL, PAGE_SIZE * TEST_PAGES,
  35. PROT_READ|PROT_WRITE,
  36. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  37. if (!pages[i]) {
  38. perror("mmap");
  39. return -1;
  40. }
  41. printf("Allocated %d pages at %p\n", TEST_PAGES, pages[i]);
  42. memset(pages[i], 'a' + i, 256);
  43. pages[i][256] = '\0';
  44. memset(pages[i] + (TEST_PAGES-7) * PAGE_SIZE, 'b' + i, 256);
  45. pages[i][256] = '\0';
  46. memset(pages[i] + (TEST_PAGES-1) * PAGE_SIZE, 'c' + i, 256);
  47. pages[i][256] = '\0';
  48. }
  49. /* Set up a communications channel */
  50. if (pipe(p0) == -1) {
  51. printf("Pipe failed %d\n", errno);
  52. return -1;
  53. }
  54. /* fork + exec this program with an arg */
  55. pid = fork();
  56. if (pid == -1) {
  57. perror("Fork");
  58. return -1;
  59. } else if (pid == 0) {
  60. char *args[3];
  61. args[0] = argv[0];
  62. args[1] = "quack";
  63. args[2] = NULL;
  64. close(p0[1]);
  65. dup2(p0[0], 0);
  66. execv(argv[0], args);
  67. printf("Failed to EXEC %s %s!, %d\n", argv[0], args[0], errno);
  68. return -1;
  69. }
  70. /* Parent */
  71. close (p0[0]);
  72. /* Open an IPC link */
  73. gfd = open (GIPC_FILE, O_RDWR);
  74. if (gfd < 0) {
  75. printf ("Fd is %d %d\n", gfd, errno);
  76. return -1;
  77. }
  78. token = ioctl(gfd, GIPC_CREATE, 0);
  79. if (token < 0) {
  80. printf ("Failed to create the gipc queue. %d\n", errno);
  81. return -1;
  82. }
  83. /* Write the token to the child */
  84. rv = write(p0[1], &token, sizeof(token));
  85. if ( rv != sizeof(token)) {
  86. perror("Failed to write map size\n");
  87. return -1;
  88. }
  89. /* Send each region */
  90. gs.entries = 1;
  91. gs.addr = addr;
  92. gs.len = len;
  93. i = 1;
  94. rv = write(p0[1], &i, sizeof(int));
  95. if ( rv != sizeof(int)) {
  96. perror("Failed to write map size\n");
  97. return -1;
  98. }
  99. for (i = 0; i < TEST_PAGES; i++) {
  100. struct mapping map = {(unsigned long) pages[i],
  101. TEST_PAGES * PAGE_SIZE,
  102. PROT_READ|PROT_WRITE,
  103. MAP_PRIVATE|MAP_ANONYMOUS};
  104. rv = write(p0[1], &map, sizeof(map));
  105. if ( rv != sizeof(map)) {
  106. perror("Failed to write map\n");
  107. return -1;
  108. }
  109. addr[i] = (unsigned long) pages[i];
  110. len[i] = TEST_PAGES * PAGE_SIZE;
  111. }
  112. rv = ioctl(gfd, GIPC_SEND, &gs);
  113. if (rv)
  114. printf ("[server] Bad rv %d (2)\n", errno);
  115. close (gfd);
  116. close (p0[1]);
  117. /* Exit */
  118. wait();
  119. } else {
  120. int rv, gfd;
  121. int max, i;
  122. struct gipc_recv gr;
  123. /* Read the token from parent */
  124. rv = read(0, &token, sizeof(token));
  125. if (rv != sizeof(token)) {
  126. perror ("Size read failed\n");
  127. return -1;
  128. }
  129. /* Open the gipc channel */
  130. gfd = open (GIPC_LINK, O_RDWR);
  131. if (gfd < 0) {
  132. printf ("Fd is %d %d\n", gfd, errno);
  133. return -1;
  134. }
  135. /* Join the queue */
  136. rv = ioctl(gfd, GIPC_JOIN, token);
  137. if (rv < 0) {
  138. printf ("Failed to join GIPC queue - %d\n", errno);
  139. return -1;
  140. }
  141. /* Read count of descriptors */
  142. rv = read(0, &max, sizeof(int));
  143. if (rv != sizeof(int)) {
  144. perror ("Size read failed\n");
  145. return -1;
  146. }
  147. /* For each memory descriptor on the pipe */
  148. for (i = 0; i < max; i++) {
  149. struct mapping map;
  150. char *empty_zone;
  151. rv = read(0, &map, sizeof(map));
  152. if (rv != sizeof(map)) {
  153. perror ("Size read failed\n");
  154. return -1;
  155. }
  156. gr.len = map.len;
  157. gr.addr = map.addr;
  158. // Perms?
  159. /* map the memory */
  160. rv = ioctl(gfd, GIPC_RECV, &gr);
  161. if (rv) {
  162. printf ("Bad map %p rv %d %d\n", (void *)gr.addr, rv, errno);
  163. return 1;
  164. }
  165. /* Do some sanity checks that it worked */
  166. if ('a' + i != ((char *) map.addr)[250]) {
  167. printf("Failed to map right shiz\n");
  168. return -1;
  169. }
  170. printf("%s\n", (char *) map.addr);
  171. printf("%s\n", (char *) map.addr + (TEST_PAGES -7)*PAGE_SIZE);
  172. printf("%s\n", (char *) map.addr + (TEST_PAGES -1)*PAGE_SIZE);
  173. /* Now touch some of the pages, which was not sent by parent */
  174. empty_zone = (char *) (map.addr + 2*PAGE_SIZE);
  175. memset(empty_zone, 'd' + i, 256);
  176. empty_zone[256]=0;
  177. printf("content at empty zone %s\n", (char *) empty_zone);
  178. }
  179. close(gfd);
  180. }
  181. return 0;
  182. }