memchr.m4 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # memchr.m4 serial 8
  2. dnl Copyright (C) 2002-2004, 2009-2010 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. AC_DEFUN_ONCE([gl_FUNC_MEMCHR],
  7. [
  8. dnl Check for prerequisites for memory fence checks.
  9. gl_FUNC_MMAP_ANON
  10. AC_CHECK_HEADERS_ONCE([sys/mman.h])
  11. AC_CHECK_FUNCS_ONCE([mprotect])
  12. dnl These days, we assume memchr is present. But just in case...
  13. AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  14. AC_CHECK_FUNCS_ONCE([memchr])
  15. if test $ac_cv_func_memchr = yes; then
  16. # Detect platform-specific bugs in some versions of glibc:
  17. # memchr should not dereference anything with length 0
  18. # http://bugzilla.redhat.com/499689
  19. # memchr should not dereference overestimated length after a match
  20. # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737
  21. # http://sourceware.org/bugzilla/show_bug.cgi?id=10162
  22. # Assume that memchr works on platforms that lack mprotect.
  23. AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works],
  24. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  25. #include <string.h>
  26. #if HAVE_SYS_MMAN_H
  27. # include <fcntl.h>
  28. # include <unistd.h>
  29. # include <sys/types.h>
  30. # include <sys/mman.h>
  31. # ifndef MAP_FILE
  32. # define MAP_FILE 0
  33. # endif
  34. #endif
  35. ]], [[
  36. char *fence = NULL;
  37. #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
  38. # if HAVE_MAP_ANONYMOUS
  39. const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
  40. const int fd = -1;
  41. # else /* !HAVE_MAP_ANONYMOUS */
  42. const int flags = MAP_FILE | MAP_PRIVATE;
  43. int fd = open ("/dev/zero", O_RDONLY, 0666);
  44. if (fd >= 0)
  45. # endif
  46. {
  47. int pagesize = getpagesize ();
  48. char *two_pages =
  49. (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
  50. flags, fd, 0);
  51. if (two_pages != (char *)(-1)
  52. && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
  53. fence = two_pages + pagesize;
  54. }
  55. #endif
  56. if (fence)
  57. {
  58. if (memchr (fence, 0, 0))
  59. return 1;
  60. strcpy (fence - 9, "12345678");
  61. if (memchr (fence - 9, 0, 79) != fence - 1)
  62. return 2;
  63. }
  64. return 0;
  65. ]])], [gl_cv_func_memchr_works=yes], [gl_cv_func_memchr_works=no],
  66. [dnl Be pessimistic for now.
  67. gl_cv_func_memchr_works="guessing no"])])
  68. if test "$gl_cv_func_memchr_works" != yes; then
  69. REPLACE_MEMCHR=1
  70. fi
  71. else
  72. HAVE_MEMCHR=0
  73. fi
  74. if test $HAVE_MEMCHR = 0 || test $REPLACE_MEMCHR = 1; then
  75. AC_LIBOBJ([memchr])
  76. gl_PREREQ_MEMCHR
  77. fi
  78. ])
  79. # Prerequisites of lib/memchr.c.
  80. AC_DEFUN([gl_PREREQ_MEMCHR], [
  81. AC_CHECK_HEADERS([bp-sym.h])
  82. ])