76b6d9
commit df648905e7d8340bb3e78813fd25e2077b9685d9
76b6d9
Author: Joseph Myers <joseph@codesourcery.com>
76b6d9
Date:   Mon Dec 17 18:29:36 2018 +0000
76b6d9
76b6d9
    Add test that MAP_* constants agree with kernel.
76b6d9
    
76b6d9
    Continuing the process of building up and using Python infrastructure
76b6d9
    for extracting and using values in headers, this patch adds a test
76b6d9
    that MAP_* constants from sys/mman.h agree with those in the Linux
76b6d9
    kernel headers.  (Other sys/mman.h constants could be added to the
76b6d9
    test separately.)
76b6d9
    
76b6d9
    This set of constants has grown over time, so the generic code is
76b6d9
    enhanced to allow saying extra constants are OK on either side of the
76b6d9
    comparison (where the caller sets those parameters based on the Linux
76b6d9
    kernel headers version, compared with the version the headers were
76b6d9
    last updated from).  Although the test is a custom Python file, my
76b6d9
    intention is to move in future to a single Python script for such
76b6d9
    tests and text files it takes as inputs, once there are enough
76b6d9
    examples to provide a guide to the common cases in such tests (I'd
76b6d9
    like to end up with most or all such sets of constants copied from
76b6d9
    kernel headers having such tests, and likewise for structure layouts
76b6d9
    from the kernel).
76b6d9
    
76b6d9
    The Makefile code is essentially the same as for tst-signal-numbers,
76b6d9
    but I didn't try to find an object file to depend on to represent the
76b6d9
    dependency on the headers used by the test (the conform/ tests don't
76b6d9
    try to represent such header dependencies at all, for example).
76b6d9
    
76b6d9
    Tested with build-many-glibcs.py, and also for x86_64 with older
76b6d9
    kernel headers.
76b6d9
    
76b6d9
            * scripts/glibcextract.py (compare_macro_consts): Take parameters
76b6d9
            to allow extra macros from first or second sources.
76b6d9
            * sysdeps/unix/sysv/linux/tst-mman-consts.py: New file.
76b6d9
            * sysdeps/unix/sysv/linux/Makefile [$(subdir) = misc]
76b6d9
            (tests-special): Add $(objpfx)tst-mman-consts.out.
76b6d9
            ($(objpfx)tst-mman-consts.out): New makefile target.
76b6d9
76b6d9
diff --git a/scripts/glibcextract.py b/scripts/glibcextract.py
76b6d9
index ecc4d5b6cc387c7d..06f712ad115e0f9e 100644
76b6d9
--- a/scripts/glibcextract.py
76b6d9
+++ b/scripts/glibcextract.py
76b6d9
@@ -136,12 +136,19 @@ def compute_macro_consts(source_text, cc, macro_re, exclude_re=None):
76b6d9
     return compute_c_consts(sym_data, cc)
76b6d9
 
76b6d9
 
76b6d9
-def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None):
76b6d9
+def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None,
76b6d9
+                         allow_extra_1=False, allow_extra_2=False):
76b6d9
     """Compare the values of macros defined by two different sources.
76b6d9
 
76b6d9
     The sources would typically be includes of a glibc header and a
76b6d9
-    kernel header.  Return 1 if there were any differences, 0 if the
76b6d9
-    macro values were the same.
76b6d9
+    kernel header.  If allow_extra_1, the first source may define
76b6d9
+    extra macros (typically if the kernel headers are older than the
76b6d9
+    version glibc has taken definitions from); if allow_extra_2, the
76b6d9
+    second source may define extra macros (typically if the kernel
76b6d9
+    headers are newer than the version glibc has taken definitions
76b6d9
+    from).  Return 1 if there were any differences other than those
76b6d9
+    allowed, 0 if the macro values were the same apart from any
76b6d9
+    allowed differences.
76b6d9
 
76b6d9
     """
76b6d9
     macros_1 = compute_macro_consts(source_1, cc, macro_re, exclude_re)
76b6d9
@@ -150,13 +157,19 @@ def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None):
76b6d9
         return 0
76b6d9
     print('First source:\n%s\n' % source_1)
76b6d9
     print('Second source:\n%s\n' % source_2)
76b6d9
+    ret = 0
76b6d9
     for name, value in sorted(macros_1.items()):
76b6d9
         if name not in macros_2:
76b6d9
             print('Only in first source: %s' % name)
76b6d9
+            if not allow_extra_1:
76b6d9
+                ret = 1
76b6d9
         elif macros_1[name] != macros_2[name]:
76b6d9
             print('Different values for %s: %s != %s'
76b6d9
                   % (name, macros_1[name], macros_2[name]))
76b6d9
+            ret = 1
76b6d9
     for name in sorted(macros_2.keys()):
76b6d9
         if name not in macros_1:
76b6d9
             print('Only in second source: %s' % name)
76b6d9
-    return 1
76b6d9
+            if not allow_extra_2:
76b6d9
+                ret = 1
76b6d9
+    return ret
76b6d9
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
76b6d9
index 9c10ee53b26e1b1b..863ed80c2a2713d3 100644
76b6d9
--- a/sysdeps/unix/sysv/linux/Makefile
76b6d9
+++ b/sysdeps/unix/sysv/linux/Makefile
76b6d9
@@ -98,6 +98,15 @@ $(objpfx)tst-sysconf-iov_max: $(objpfx)tst-sysconf-iov_max-uapi.o
76b6d9
 
76b6d9
 $(objpfx)tst-pkey: $(shared-thread-library)
76b6d9
 
76b6d9
+tests-special += $(objpfx)tst-mman-consts.out
76b6d9
+$(objpfx)tst-mman-consts.out: ../sysdeps/unix/sysv/linux/tst-mman-consts.py
76b6d9
+	PYTHONPATH=../scripts \
76b6d9
+	$(PYTHON) ../sysdeps/unix/sysv/linux/tst-mman-consts.py \
76b6d9
+		   --cc="$(CC) $(patsubst -DMODULE_NAME=%, \
76b6d9
+					  -DMODULE_NAME=testsuite, \
76b6d9
+					  $(CPPFLAGS))" \
76b6d9
+	< /dev/null > $@ 2>&1; $(evaluate-test)
76b6d9
+
76b6d9
 endif # $(subdir) == misc
76b6d9
 
76b6d9
 ifeq ($(subdir),time)
76b6d9
diff --git a/sysdeps/unix/sysv/linux/tst-mman-consts.py b/sysdeps/unix/sysv/linux/tst-mman-consts.py
76b6d9
new file mode 100644
76b6d9
index 0000000000000000..1a613beec0da16fb
76b6d9
--- /dev/null
76b6d9
+++ b/sysdeps/unix/sysv/linux/tst-mman-consts.py
76b6d9
@@ -0,0 +1,65 @@
76b6d9
+#!/usr/bin/python3
76b6d9
+# Test that glibc's sys/mman.h constants match the kernel's.
76b6d9
+# Copyright (C) 2018 Free Software Foundation, Inc.
76b6d9
+# This file is part of the GNU C Library.
76b6d9
+#
76b6d9
+# The GNU C Library is free software; you can redistribute it and/or
76b6d9
+# modify it under the terms of the GNU Lesser General Public
76b6d9
+# License as published by the Free Software Foundation; either
76b6d9
+# version 2.1 of the License, or (at your option) any later version.
76b6d9
+#
76b6d9
+# The GNU C Library is distributed in the hope that it will be useful,
76b6d9
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
76b6d9
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
76b6d9
+# Lesser General Public License for more details.
76b6d9
+#
76b6d9
+# You should have received a copy of the GNU Lesser General Public
76b6d9
+# License along with the GNU C Library; if not, see
76b6d9
+# <http://www.gnu.org/licenses/>.
76b6d9
+
76b6d9
+import argparse
76b6d9
+import sys
76b6d9
+
76b6d9
+import glibcextract
76b6d9
+
76b6d9
+
76b6d9
+def linux_kernel_version(cc):
76b6d9
+    """Return the (major, minor) version of the Linux kernel headers."""
76b6d9
+    sym_data = ['#include <linux/version.h>', 'START',
76b6d9
+                ('LINUX_VERSION_CODE', 'LINUX_VERSION_CODE')]
76b6d9
+    val = glibcextract.compute_c_consts(sym_data, cc)['LINUX_VERSION_CODE']
76b6d9
+    val = int(val)
76b6d9
+    return ((val & 0xff0000) >> 16, (val & 0xff00) >> 8)
76b6d9
+
76b6d9
+
76b6d9
+def main():
76b6d9
+    """The main entry point."""
76b6d9
+    parser = argparse.ArgumentParser(
76b6d9
+        description="Test that glibc's sys/mman.h constants "
76b6d9
+        "match the kernel's.")
76b6d9
+    parser.add_argument('--cc', metavar='CC',
76b6d9
+                        help='C compiler (including options) to use')
76b6d9
+    args = parser.parse_args()
76b6d9
+    linux_version_headers = linux_kernel_version(args.cc)
76b6d9
+    linux_version_glibc = (4, 19)
76b6d9
+    sys.exit(glibcextract.compare_macro_consts(
76b6d9
+        '#define _GNU_SOURCE 1\n'
76b6d9
+        '#include <sys/mman.h>\n',
76b6d9
+        '#define _GNU_SOURCE 1\n'
76b6d9
+        '#include <linux/mman.h>\n',
76b6d9
+        args.cc,
76b6d9
+        'MAP_.*',
76b6d9
+        # A series of MAP_HUGE_<size> macros are defined by the kernel
76b6d9
+        # but not by glibc.  MAP_UNINITIALIZED is kernel-only.
76b6d9
+        # MAP_FAILED is not a MAP_* flag and is glibc-only, as is the
76b6d9
+        # MAP_ANON alias for MAP_ANONYMOUS.  MAP_RENAME, MAP_AUTOGROW,
76b6d9
+        # MAP_LOCAL and MAP_AUTORSRV are in the kernel header for
76b6d9
+        # MIPS, marked as "not used by linux"; SPARC has MAP_INHERIT
76b6d9
+        # in the kernel header, but does not use it.
76b6d9
+        'MAP_HUGE_[0-9].*|MAP_UNINITIALIZED|MAP_FAILED|MAP_ANON'
76b6d9
+        '|MAP_RENAME|MAP_AUTOGROW|MAP_LOCAL|MAP_AUTORSRV|MAP_INHERIT',
76b6d9
+        linux_version_glibc > linux_version_headers,
76b6d9
+        linux_version_headers > linux_version_glibc))
76b6d9
+
76b6d9
+if __name__ == '__main__':
76b6d9
+    main()