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