Commit | Line | Data |
---|---|---|
c0c3707f CB |
1 | # memchr.m4 serial 15 |
2 | dnl Copyright (C) 2002-2004, 2009-2019 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 | [ |
c0c3707f CB |
9 | AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
10 | ||
f434ba03 PA |
11 | dnl Check for prerequisites for memory fence checks. |
12 | gl_FUNC_MMAP_ANON | |
13 | AC_CHECK_HEADERS_ONCE([sys/mman.h]) | |
14 | AC_CHECK_FUNCS_ONCE([mprotect]) | |
15 | ||
f434ba03 | 16 | AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) |
770d76d7 PA |
17 | m4_ifdef([gl_FUNC_MEMCHR_OBSOLETE], [ |
18 | dnl These days, we assume memchr is present. But if support for old | |
19 | dnl platforms is desired: | |
20 | AC_CHECK_FUNCS_ONCE([memchr]) | |
21 | if test $ac_cv_func_memchr = no; then | |
22 | HAVE_MEMCHR=0 | |
23 | fi | |
24 | ]) | |
25 | if test $HAVE_MEMCHR = 1; then | |
f434ba03 PA |
26 | # Detect platform-specific bugs in some versions of glibc: |
27 | # memchr should not dereference anything with length 0 | |
c0c3707f | 28 | # https://bugzilla.redhat.com/show_bug.cgi?id=499689 |
f434ba03 | 29 | # memchr should not dereference overestimated length after a match |
c0c3707f CB |
30 | # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737 |
31 | # https://sourceware.org/bugzilla/show_bug.cgi?id=10162 | |
32 | # memchr should cast the second argument to 'unsigned char'. | |
33 | # This bug exists in Android 4.3. | |
f434ba03 PA |
34 | # Assume that memchr works on platforms that lack mprotect. |
35 | AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works], | |
36 | [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ | |
37 | #include <string.h> | |
38 | #if HAVE_SYS_MMAN_H | |
39 | # include <fcntl.h> | |
40 | # include <unistd.h> | |
41 | # include <sys/types.h> | |
42 | # include <sys/mman.h> | |
43 | # ifndef MAP_FILE | |
44 | # define MAP_FILE 0 | |
45 | # endif | |
46 | #endif | |
47 | ]], [[ | |
770d76d7 | 48 | int result = 0; |
f434ba03 PA |
49 | char *fence = NULL; |
50 | #if HAVE_SYS_MMAN_H && HAVE_MPROTECT | |
51 | # if HAVE_MAP_ANONYMOUS | |
52 | const int flags = MAP_ANONYMOUS | MAP_PRIVATE; | |
53 | const int fd = -1; | |
54 | # else /* !HAVE_MAP_ANONYMOUS */ | |
55 | const int flags = MAP_FILE | MAP_PRIVATE; | |
56 | int fd = open ("/dev/zero", O_RDONLY, 0666); | |
57 | if (fd >= 0) | |
58 | # endif | |
59 | { | |
60 | int pagesize = getpagesize (); | |
61 | char *two_pages = | |
62 | (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE, | |
63 | flags, fd, 0); | |
64 | if (two_pages != (char *)(-1) | |
65 | && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0) | |
66 | fence = two_pages + pagesize; | |
67 | } | |
68 | #endif | |
69 | if (fence) | |
70 | { | |
71 | if (memchr (fence, 0, 0)) | |
770d76d7 | 72 | result |= 1; |
f434ba03 PA |
73 | strcpy (fence - 9, "12345678"); |
74 | if (memchr (fence - 9, 0, 79) != fence - 1) | |
770d76d7 PA |
75 | result |= 2; |
76 | if (memchr (fence - 1, 0, 3) != fence - 1) | |
77 | result |= 4; | |
f434ba03 | 78 | } |
c0c3707f CB |
79 | /* Test against bug on Android 4.3. */ |
80 | { | |
81 | char input[3]; | |
82 | input[0] = 'a'; | |
83 | input[1] = 'b'; | |
84 | input[2] = 'c'; | |
85 | if (memchr (input, 0x789abc00 | 'b', 3) != input + 1) | |
86 | result |= 8; | |
87 | } | |
770d76d7 | 88 | return result; |
c0c3707f CB |
89 | ]])], |
90 | [gl_cv_func_memchr_works=yes], | |
91 | [gl_cv_func_memchr_works=no], | |
92 | [case "$host_os" in | |
93 | # Guess no on Android. | |
94 | linux*-android*) gl_cv_func_memchr_works="guessing no" ;; | |
95 | # Guess yes on native Windows. | |
96 | mingw*) gl_cv_func_memchr_works="guessing yes" ;; | |
97 | # If we don't know, obey --enable-cross-guesses. | |
98 | *) gl_cv_func_memchr_works="$gl_cross_guess_normal" ;; | |
99 | esac | |
100 | ]) | |
101 | ]) | |
102 | case "$gl_cv_func_memchr_works" in | |
103 | *yes) ;; | |
104 | *) REPLACE_MEMCHR=1 ;; | |
105 | esac | |
f6ea5628 DJ |
106 | fi |
107 | ]) | |
108 | ||
109 | # Prerequisites of lib/memchr.c. | |
110 | AC_DEFUN([gl_PREREQ_MEMCHR], [ | |
f434ba03 | 111 | AC_CHECK_HEADERS([bp-sym.h]) |
f6ea5628 | 112 | ]) |