Snap for 6450912 from 0e1fbbfc81bfc41d9366b17e267b56e5d06f92d8 to mainline-release

Change-Id: I61d453779e0e3f7831d007177df594f6098ed6c3
diff --git a/Android.bp b/Android.bp
index 591acd4..15cc1a8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -38,10 +38,6 @@
         "-DHAVE_CONFIG_H",
         "-Wall",
         "-Werror",
-        // TODO(http://b/153760240): Remove this once we have
-        // patched sources to leave a large stack array uninitialized
-        // (stack_frames_vector from pcre2_match.c).
-        "-ftrivial-auto-var-init=uninitialized",
     ],
     tidy_checks: [
         "-google-build-using-namespace",
diff --git a/dist2/CMakeLists.txt b/dist2/CMakeLists.txt
index 4737687..a7ef67e 100644
--- a/dist2/CMakeLists.txt
+++ b/dist2/CMakeLists.txt
@@ -107,8 +107,9 @@
 
 # Configuration checks
 
-INCLUDE(CheckIncludeFile)
+INCLUDE(CheckCSourceCompiles)
 INCLUDE(CheckFunctionExists)
+INCLUDE(CheckIncludeFile)
 INCLUDE(CheckTypeSize)
 
 CHECK_INCLUDE_FILE(dirent.h     HAVE_DIRENT_H)
@@ -123,6 +124,14 @@
 CHECK_FUNCTION_EXISTS(memmove   HAVE_MEMMOVE)
 CHECK_FUNCTION_EXISTS(strerror  HAVE_STRERROR)
 
+set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
+set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror")
+CHECK_C_SOURCE_COMPILES(
+  "int main() { char buf[128] __attribute__((uninitialized)); (void)buf; return 0; }"
+  HAVE_ATTRIBUTE_UNINITIALIZED
+)
+set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
+
 # User-configurable options
 #
 # Note: CMakeSetup displays these in alphabetical order, regardless of
diff --git a/dist2/config-cmake.h.in b/dist2/config-cmake.h.in
index 529b009..44d51ae 100644
--- a/dist2/config-cmake.h.in
+++ b/dist2/config-cmake.h.in
@@ -1,5 +1,6 @@
 /* config.h for CMake builds */
 
+#cmakedefine HAVE_ATTRIBUTE_UNINITIALIZED 1
 #cmakedefine HAVE_DIRENT_H 1
 #cmakedefine HAVE_INTTYPES_H 1    
 #cmakedefine HAVE_STDINT_H 1                                                   
diff --git a/dist2/configure.ac b/dist2/configure.ac
index 93c2b53..f3086f1 100644
--- a/dist2/configure.ac
+++ b/dist2/configure.ac
@@ -72,6 +72,24 @@
 
 PCRE2_VISIBILITY
 
+# Check for Clang __attribute__((uninitialized)) feature
+
+AC_MSG_CHECKING([for __attribute__((uninitialized))])
+AC_LANG_PUSH([C])
+tmp_CFLAGS=$CFLAGS
+CFLAGS="$CFLAGS -Werror"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,
+                   [[char buf[128] __attribute__((uninitialized));(void)buf]])],
+                   [pcre2_cc_cv_attribute_uninitialized=yes],
+                   [pcre2_cc_cv_attribute_uninitialized=no])
+AC_MSG_RESULT([$pcre2_cc_cv_attribute_uninitialized])
+if test "$pcre2_cc_cv_attribute_uninitialized" = yes; then
+  AC_DEFINE([HAVE_ATTRIBUTE_UNINITIALIZED], 1, [Define this if your compiler
+             supports __attribute__((uninitialized))])
+fi
+CFLAGS=$tmp_CFLAGS
+AC_LANG_POP([C])
+
 # Versioning
 
 PCRE2_MAJOR="pcre2_major"
diff --git a/dist2/src/config.h.in b/dist2/src/config.h.in
index 6b8eb7e..5406da0 100644
--- a/dist2/src/config.h.in
+++ b/dist2/src/config.h.in
@@ -52,6 +52,9 @@
    LF does in an ASCII/Unicode environment. */
 #undef EBCDIC_NL25
 
+/* Define this if your compiler supports __attribute__((uninitialized)) */
+#undef HAVE_ATTRIBUTE_UNINITIALIZED
+
 /* Define to 1 if you have the `bcopy' function. */
 #undef HAVE_BCOPY
 
diff --git a/dist2/src/pcre2_internal.h b/dist2/src/pcre2_internal.h
index 814d91b..aa7ce88 100644
--- a/dist2/src/pcre2_internal.h
+++ b/dist2/src/pcre2_internal.h
@@ -76,6 +76,17 @@
 #include <valgrind/memcheck.h>
 #endif
 
+/* -ftrivial-auto-var-init support supports initializing all local variables
+to avoid some classes of bug, but this can cause an unacceptable slowdown
+for large on-stack arrays in hot functions. This macro lets us annotate
+such arrays. */
+
+#ifdef HAVE_ATTRIBUTE_UNINITIALIZED
+#define PCRE2_KEEP_UNINITIALIZED __attribute__((uninitialized))
+#else
+#define PCRE2_KEEP_UNINITIALIZED
+#endif
+
 /* Older versions of MSVC lack snprintf(). This define allows for
 warning/error-free compilation and testing with MSVC compilers back to at least
 MSVC 10/2010. Except for VC6 (which is missing some fundamentals and fails). */
diff --git a/dist2/src/pcre2_match.c b/dist2/src/pcre2_match.c
index 419561f..e5cf2c4 100644
--- a/dist2/src/pcre2_match.c
+++ b/dist2/src/pcre2_match.c
@@ -6048,7 +6048,8 @@
 vector of the size required that is aligned for pointers, allocate it as a
 vector of pointers. */
 
-PCRE2_SPTR stack_frames_vector[START_FRAMES_SIZE/sizeof(PCRE2_SPTR)];
+PCRE2_SPTR stack_frames_vector[START_FRAMES_SIZE/sizeof(PCRE2_SPTR)]
+    PCRE2_KEEP_UNINITIALIZED;
 mb->stack_frames = (heapframe *)stack_frames_vector;
 
 /* A length equal to PCRE2_ZERO_TERMINATED implies a zero-terminated
diff --git a/include_internal/config.h b/include_internal/config.h
index a699a13..10c77e5 100644
--- a/include_internal/config.h
+++ b/include_internal/config.h
@@ -53,6 +53,9 @@
    LF does in an ASCII/Unicode environment. */
 /* #undef EBCDIC_NL25 */
 
+/* Define this if your compiler supports __attribute__((uninitialized)) */
+#define HAVE_ATTRIBUTE_UNINITIALIZED 1
+
 /* Define to 1 if you have the `bcopy' function. */
 #define HAVE_BCOPY 1