Backport LLVM fix for mallinfo deprecation

This backports the following LLVM commit with a few
SwiftShader specific modifications, like testing for
__GLIBC__ because SwiftShader is used within Chrome
and built via GN bypassing cmake and not doing any
build time autodetection.

Bug: b/192875071
Test: Local builds on ChromeOS with glibc 2.32 & 2.33

Original commit message:

Fix deprecated usage of `mallinfo`

glibc deprecates `mallinfo` in the latest version of 2.33. This patch replaces the usage of `mallinfo` with the new `mallinfo2` when it's available.

Reviewed By: lattner

Differential Revision: https://reviews.llvm.org/D96359

Change-Id: Ib6253231279a2995acb685cf0a1f606dbe9b2f2b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/55828
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Commit-Queue: Adrian Ratiu <adrian.ratiu@collabora.corp-partner.google.com>
Tested-by: Adrian Ratiu <adrian.ratiu@collabora.corp-partner.google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a59a76..7bf5787 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -105,6 +105,12 @@
 # Host libraries
 ###########################################################
 
+include(CheckSymbolExists)
+if(LINUX)
+    check_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
+    check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
+endif()
+
 find_library(X11 X11)
 find_library(XCB xcb)
 if(SWIFTSHADER_BUILD_WSI_WAYLAND)
diff --git a/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h b/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
index ac4b3e3..24bdc67 100644
--- a/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
+++ b/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
@@ -4,6 +4,9 @@
 /* Exported configuration */
 #include "llvm/Config/llvm-config.h"
 
+/* For detecting __GLIBC__ usage */
+#include <features.h>
+
 /* Bug report URL. */
 #define BUG_REPORT_URL "https://bugs.llvm.org/"
 
@@ -127,6 +130,12 @@
 /* Define to 1 if you have the `mallinfo' function. */
 #define HAVE_MALLINFO 1
 
+/* Some projects using SwiftShader bypass cmake (eg Chromium via gn) */
+/* so we need to check glibc version for the new API to be safe */
+#if defined(__GLIBC__) && __GLIBC_MINOR__ >= 33
+#define HAVE_MALLINFO2 1
+#endif
+
 /* Define to 1 if you have the <malloc/malloc.h> header file. */
 /* #undef HAVE_MALLOC_MALLOC_H */
 
diff --git a/third_party/llvm-10.0/llvm/include/llvm/Config/config.h.cmake b/third_party/llvm-10.0/llvm/include/llvm/Config/config.h.cmake
index 1a38bc1..10aa51f 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/Config/config.h.cmake
+++ b/third_party/llvm-10.0/llvm/include/llvm/Config/config.h.cmake
@@ -127,6 +127,9 @@
 /* Define to 1 if you have the `mallinfo' function. */
 #cmakedefine HAVE_MALLINFO ${HAVE_MALLINFO}
 
+/* Define to 1 if you have the `mallinfo2' function. */
+#cmakedefine HAVE_MALLINFO2 ${HAVE_MALLINFO2}
+
 /* Define to 1 if you have the <malloc/malloc.h> header file. */
 #cmakedefine HAVE_MALLOC_MALLOC_H ${HAVE_MALLOC_MALLOC_H}
 
diff --git a/third_party/llvm-10.0/llvm/lib/Support/Unix/Process.inc b/third_party/llvm-10.0/llvm/lib/Support/Unix/Process.inc
index dfe81d7..1159a13 100644
--- a/third_party/llvm-10.0/llvm/lib/Support/Unix/Process.inc
+++ b/third_party/llvm-10.0/llvm/lib/Support/Unix/Process.inc
@@ -31,7 +31,7 @@
 #if HAVE_SIGNAL_H
 #include <signal.h>
 #endif
-#if defined(HAVE_MALLINFO)
+#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2)
 #include <malloc.h>
 #endif
 #if defined(HAVE_MALLCTL)
@@ -83,7 +83,11 @@
 }
 
 size_t Process::GetMallocUsage() {
-#if defined(HAVE_MALLINFO)
+#if defined(HAVE_MALLINFO2)
+  struct mallinfo2 mi;
+  mi = ::mallinfo2();
+  return mi.uordblks;
+#elif defined(HAVE_MALLINFO)
   struct mallinfo mi;
   mi = ::mallinfo();
   return mi.uordblks;
diff --git a/third_party/llvm-subzero/build/Linux/include/llvm/Config/config.h b/third_party/llvm-subzero/build/Linux/include/llvm/Config/config.h
index 2f860e1..0ffd1bf 100644
--- a/third_party/llvm-subzero/build/Linux/include/llvm/Config/config.h
+++ b/third_party/llvm-subzero/build/Linux/include/llvm/Config/config.h
@@ -4,6 +4,9 @@
 /* Exported configuration */
 #include "llvm/Config/llvm-config.h"
 
+/* For detecting __GLIBC__ usage */
+#include <features.h>
+
 /* Bug report URL. */
 #define BUG_REPORT_URL "http://llvm.org/bugs/"
 
@@ -132,6 +135,12 @@
 /* Define to 1 if you have the `mallinfo' function. */
 #define HAVE_MALLINFO 1
 
+/* Some projects using SwiftShader bypass cmake (eg Chromium via gn) */
+/* so we need to check glibc version for the new API to be safe */
+#if defined(__GLIBC__) && __GLIBC_MINOR__ >= 33
+#define HAVE_MALLINFO2 1
+#endif
+
 /* Define to 1 if you have the <malloc.h> header file. */
 #define HAVE_MALLOC_H 1
 
diff --git a/third_party/llvm-subzero/lib/Support/Unix/Process.inc b/third_party/llvm-subzero/lib/Support/Unix/Process.inc
index b239046..1a767bc 100644
--- a/third_party/llvm-subzero/lib/Support/Unix/Process.inc
+++ b/third_party/llvm-subzero/lib/Support/Unix/Process.inc
@@ -86,7 +86,11 @@
 }
 
 size_t Process::GetMallocUsage() {
-#if defined(HAVE_MALLINFO)
+#if defined(HAVE_MALLINFO2)
+  struct mallinfo2 mi;
+  mi = ::mallinfo2();
+  return mi.uordblks;
+#elif defined(HAVE_MALLINFO)
   struct mallinfo mi;
   mi = ::mallinfo();
   return mi.uordblks;