futex_wait_uninitialized_heap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /******************************************************************************
  2. *
  3. * Copyright FUJITSU LIMITED 2010
  4. * Copyright KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DESCRIPTION
  12. * Wait on uninitialized heap. It shold be zero and FUTEX_WAIT should
  13. * return immediately. This test is intent to test zero page handling in
  14. * futex.
  15. *
  16. * AUTHOR
  17. * KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
  18. *
  19. * HISTORY
  20. * 2010-Jan-6: Initial version by KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
  21. *
  22. *****************************************************************************/
  23. #include <pthread.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/mman.h>
  27. #include <syscall.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <linux/futex.h>
  33. #include <libgen.h>
  34. #include "logging.h"
  35. #include "futextest.h"
  36. #define WAIT_US 5000000
  37. static int child_blocked = 1;
  38. static int child_ret;
  39. void *buf;
  40. void usage(char *prog)
  41. {
  42. printf("Usage: %s\n", prog);
  43. printf(" -c Use color\n");
  44. printf(" -h Display this help message\n");
  45. printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  46. VQUIET, VCRITICAL, VINFO);
  47. }
  48. void *wait_thread(void *arg)
  49. {
  50. int res;
  51. child_ret = RET_PASS;
  52. res = futex_wait(buf, 1, NULL, 0);
  53. child_blocked = 0;
  54. if (res != 0 && errno != EWOULDBLOCK) {
  55. error("futex failure\n", errno);
  56. child_ret = RET_ERROR;
  57. }
  58. pthread_exit(NULL);
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. int c, ret = RET_PASS;
  63. long page_size;
  64. pthread_t thr;
  65. while ((c = getopt(argc, argv, "chv:")) != -1) {
  66. switch (c) {
  67. case 'c':
  68. log_color(1);
  69. break;
  70. case 'h':
  71. usage(basename(argv[0]));
  72. exit(0);
  73. case 'v':
  74. log_verbosity(atoi(optarg));
  75. break;
  76. default:
  77. usage(basename(argv[0]));
  78. exit(1);
  79. }
  80. }
  81. page_size = sysconf(_SC_PAGESIZE);
  82. buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
  83. MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  84. if (buf == (void *)-1) {
  85. error("mmap\n", errno);
  86. exit(1);
  87. }
  88. printf("%s: Test the uninitialized futex value in FUTEX_WAIT\n",
  89. basename(argv[0]));
  90. ret = pthread_create(&thr, NULL, wait_thread, NULL);
  91. if (ret) {
  92. error("pthread_create\n", errno);
  93. ret = RET_ERROR;
  94. goto out;
  95. }
  96. info("waiting %dus for child to return\n", WAIT_US);
  97. usleep(WAIT_US);
  98. ret = child_ret;
  99. if (child_blocked) {
  100. fail("child blocked in kernel\n");
  101. ret = RET_FAIL;
  102. }
  103. out:
  104. print_result(ret);
  105. return ret;
  106. }