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