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