futex_wait_uninitialized_heap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 TEST_NAME "futex-wait-uninitialized-heap"
  37. #define WAIT_US 5000000
  38. static int child_blocked = 1;
  39. static int child_ret;
  40. void *buf;
  41. void usage(char *prog)
  42. {
  43. printf("Usage: %s\n", prog);
  44. printf(" -c Use color\n");
  45. printf(" -h Display this help message\n");
  46. printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  47. VQUIET, VCRITICAL, VINFO);
  48. }
  49. void *wait_thread(void *arg)
  50. {
  51. int res;
  52. child_ret = RET_PASS;
  53. res = futex_wait(buf, 1, NULL, 0);
  54. child_blocked = 0;
  55. if (res != 0 && errno != EWOULDBLOCK) {
  56. error("futex failure\n", errno);
  57. child_ret = RET_ERROR;
  58. }
  59. pthread_exit(NULL);
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. int c, ret = RET_PASS;
  64. long page_size;
  65. pthread_t thr;
  66. while ((c = getopt(argc, argv, "chv:")) != -1) {
  67. switch (c) {
  68. case 'c':
  69. log_color(1);
  70. break;
  71. case 'h':
  72. usage(basename(argv[0]));
  73. exit(0);
  74. case 'v':
  75. log_verbosity(atoi(optarg));
  76. break;
  77. default:
  78. usage(basename(argv[0]));
  79. exit(1);
  80. }
  81. }
  82. page_size = sysconf(_SC_PAGESIZE);
  83. buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
  84. MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  85. if (buf == (void *)-1) {
  86. error("mmap\n", errno);
  87. exit(1);
  88. }
  89. ksft_print_header();
  90. ksft_print_msg("%s: Test the uninitialized futex value in FUTEX_WAIT\n",
  91. basename(argv[0]));
  92. ret = pthread_create(&thr, NULL, wait_thread, NULL);
  93. if (ret) {
  94. error("pthread_create\n", errno);
  95. ret = RET_ERROR;
  96. goto out;
  97. }
  98. info("waiting %dus for child to return\n", WAIT_US);
  99. usleep(WAIT_US);
  100. ret = child_ret;
  101. if (child_blocked) {
  102. fail("child blocked in kernel\n");
  103. ret = RET_FAIL;
  104. }
  105. out:
  106. print_result(TEST_NAME, ret);
  107. return ret;
  108. }