Merge "bionic: Do not use PAGE_SIZE to define PTHREAD_STACK_MIN" into main
diff --git a/libc/bionic/bionic_call_ifunc_resolver.cpp b/libc/bionic/bionic_call_ifunc_resolver.cpp
index 0b12088..d5a812c 100644
--- a/libc/bionic/bionic_call_ifunc_resolver.cpp
+++ b/libc/bionic/bionic_call_ifunc_resolver.cpp
@@ -28,6 +28,7 @@
 
 #include "private/bionic_call_ifunc_resolver.h"
 #include <sys/auxv.h>
+#include <sys/hwprobe.h>
 #include <sys/ifunc.h>
 
 #include "bionic/macros.h"
@@ -51,20 +52,15 @@
   return reinterpret_cast<ifunc_resolver_t>(resolver_addr)(arg._hwcap | _IFUNC_ARG_HWCAP, &arg);
 #elif defined(__arm__)
   typedef ElfW(Addr) (*ifunc_resolver_t)(unsigned long);
-  static unsigned long hwcap;
-  static bool initialized = false;
-  if (!initialized) {
-    initialized = true;
-    hwcap = getauxval(AT_HWCAP);
-  }
+  static unsigned long hwcap = getauxval(AT_HWCAP);
   return reinterpret_cast<ifunc_resolver_t>(resolver_addr)(hwcap);
 #elif defined(__riscv)
-  // The pointer argument is currently unused, but reserved for future
+  // The third argument is currently unused, but reserved for future
   // expansion. If we pass nullptr from the beginning, it'll be easier
   // to recognize if/when we pass actual data (and matches glibc).
-  typedef ElfW(Addr) (*ifunc_resolver_t)(uint64_t, void*);
+  typedef ElfW(Addr) (*ifunc_resolver_t)(uint64_t, __riscv_hwprobe_t, void*);
   static uint64_t hwcap = getauxval(AT_HWCAP);
-  return reinterpret_cast<ifunc_resolver_t>(resolver_addr)(hwcap, nullptr);
+  return reinterpret_cast<ifunc_resolver_t>(resolver_addr)(hwcap, __riscv_hwprobe, nullptr);
 #else
   typedef ElfW(Addr) (*ifunc_resolver_t)(void);
   return reinterpret_cast<ifunc_resolver_t>(resolver_addr)();
diff --git a/libc/include/bits/tcphdr.h b/libc/include/bits/tcphdr.h
new file mode 100644
index 0000000..a9b6fe0
--- /dev/null
+++ b/libc/include/bits/tcphdr.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <sys/cdefs.h>
+#include <stdint.h>
+
+__BEGIN_DECLS
+
+struct tcphdr {
+  __extension__ union {
+    struct {
+      uint16_t th_sport;
+      uint16_t th_dport;
+      uint32_t th_seq;
+      uint32_t th_ack;
+      uint8_t th_x2:4;
+      uint8_t th_off:4;
+      uint8_t th_flags;
+      uint16_t th_win;
+      uint16_t th_sum;
+      uint16_t th_urp;
+    };
+    struct {
+      uint16_t source;
+      uint16_t dest;
+      uint32_t seq;
+      uint32_t ack_seq;
+      uint16_t res1:4;
+      uint16_t doff:4;
+      uint16_t fin:1;
+      uint16_t syn:1;
+      uint16_t rst:1;
+      uint16_t psh:1;
+      uint16_t ack:1;
+      uint16_t urg:1;
+      uint16_t res2:2;
+      uint16_t window;
+      uint16_t check;
+      uint16_t urg_ptr;
+    };
+  };
+};
+
+__END_DECLS
diff --git a/libc/include/netinet/tcp.h b/libc/include/netinet/tcp.h
index 147f6f7..d1c657b 100644
--- a/libc/include/netinet/tcp.h
+++ b/libc/include/netinet/tcp.h
@@ -32,47 +32,10 @@
 #include <sys/cdefs.h>
 #include <stdint.h>
 
-#define tcphdr __kernel_tcphdr
 #include <linux/tcp.h>
-#undef tcphdr
 
 __BEGIN_DECLS
 
-struct tcphdr {
-  __extension__ union {
-    struct {
-      uint16_t th_sport;
-      uint16_t th_dport;
-      uint32_t th_seq;
-      uint32_t th_ack;
-      uint8_t th_x2:4;
-      uint8_t th_off:4;
-      uint8_t th_flags;
-      uint16_t th_win;
-      uint16_t th_sum;
-      uint16_t th_urp;
-    };
-    struct {
-      uint16_t source;
-      uint16_t dest;
-      uint32_t seq;
-      uint32_t ack_seq;
-      uint16_t res1:4;
-      uint16_t doff:4;
-      uint16_t fin:1;
-      uint16_t syn:1;
-      uint16_t rst:1;
-      uint16_t psh:1;
-      uint16_t ack:1;
-      uint16_t urg:1;
-      uint16_t res2:2;
-      uint16_t window;
-      uint16_t check;
-      uint16_t urg_ptr;
-    };
-  };
-};
-
 #define TH_FIN 0x01
 #define TH_SYN 0x02
 #define TH_RST 0x04
diff --git a/libc/include/sys/hwprobe.h b/libc/include/sys/hwprobe.h
index b1a8400..8e69e8a 100644
--- a/libc/include/sys/hwprobe.h
+++ b/libc/include/sys/hwprobe.h
@@ -53,6 +53,14 @@
  */
 int __riscv_hwprobe(struct riscv_hwprobe* _Nonnull __pairs, size_t __pair_count, size_t __cpu_count, unsigned long* _Nullable __cpus, unsigned __flags);
 
+/**
+ * The type of the second argument passed to riscv64 ifunc resolvers.
+ * This argument allows riscv64 ifunc resolvers to call __riscv_hwprobe()
+ * without worrying about whether that relocation is resolved before
+ * the ifunc resolver is called.
+ */
+typedef int (*__riscv_hwprobe_t)(struct riscv_hwprobe* _Nonnull __pairs, size_t __pair_count, size_t __cpu_count, unsigned long* _Nullable __cpus, unsigned __flags);
+
 __END_DECLS
 
 #endif
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index 5340216..9b357f1 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -43,6 +43,7 @@
     "in_addr": False,
     "ip_mreq_source": False,
     "ip_msfilter": False,
+    "tcphdr": False,
     "timespec": False,
     }
 
diff --git a/libc/kernel/uapi/linux/tcp.h b/libc/kernel/uapi/linux/tcp.h
index 843b6ef..20fbcec 100644
--- a/libc/kernel/uapi/linux/tcp.h
+++ b/libc/kernel/uapi/linux/tcp.h
@@ -6,25 +6,10 @@
  */
 #ifndef _UAPI_LINUX_TCP_H
 #define _UAPI_LINUX_TCP_H
+#include <bits/tcphdr.h>
 #include <linux/types.h>
 #include <asm/byteorder.h>
 #include <linux/socket.h>
-struct tcphdr {
-  __be16 source;
-  __be16 dest;
-  __be32 seq;
-  __be32 ack_seq;
-#ifdef __LITTLE_ENDIAN_BITFIELD
-  __u16 res1 : 4, doff : 4, fin : 1, syn : 1, rst : 1, psh : 1, ack : 1, urg : 1, ece : 1, cwr : 1;
-#elif defined(__BIG_ENDIAN_BITFIELD)
-  __u16 doff : 4, res1 : 4, cwr : 1, ece : 1, urg : 1, ack : 1, psh : 1, rst : 1, syn : 1, fin : 1;
-#else
-#error "Adjust your <asm/byteorder.h> defines"
-#endif
-  __be16 window;
-  __sum16 check;
-  __be16 urg_ptr;
-};
 union tcp_word_hdr {
   struct tcphdr hdr;
   __be32 words[5];
diff --git a/tests/ifunc_test.cpp b/tests/ifunc_test.cpp
index 1fdbf1a..09d987d 100644
--- a/tests/ifunc_test.cpp
+++ b/tests/ifunc_test.cpp
@@ -65,18 +65,18 @@
 #include <sys/hwprobe.h>
 
 static uint64_t g_hwcap;
+static __riscv_hwprobe_t g_hwprobe_ptr;
+static void* g_null;
 
 static riscv_hwprobe g_hwprobes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
 
-extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, void* null) {
-  // Check hwcap like arm32/arm64.
+extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __riscv_hwprobe_t hwprobe_ptr, void* null) {
   g_hwcap = hwcap;
-
-  // For now, the pointer argument is reserved for future expansion.
-  if (null != NULL) abort();
+  g_hwprobe_ptr = hwprobe_ptr;
+  g_null = null;
 
   // Ensure that __riscv_hwprobe() can be called from an ifunc.
-  if (__riscv_hwprobe(g_hwprobes, 1, 0, nullptr, 0) != 0) return nullptr;
+  if ((*hwprobe_ptr)(g_hwprobes, 1, 0, nullptr, 0) != 0) return nullptr;
   return ret42;
 }
 
@@ -102,7 +102,11 @@
 #elif defined(__arm__)
   EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
 #elif defined(__riscv)
+  printf("hwcap=%lx hwprobe_ptr=%p (__riscv_hwprobe=%p) null=%p\n", g_hwcap, g_hwprobe_ptr,
+         __riscv_hwprobe, g_null);
+
   EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
+  EXPECT_EQ(nullptr, g_null);
 
   riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
   ASSERT_EQ(0, __riscv_hwprobe(probes, 1, 0, nullptr, 0));
diff --git a/tests/memtag_globals_test.cpp b/tests/memtag_globals_test.cpp
index 4482c8a..3736d4c 100644
--- a/tests/memtag_globals_test.cpp
+++ b/tests/memtag_globals_test.cpp
@@ -34,13 +34,11 @@
 #include "utils.h"
 #endif
 
-#include <android-base/test_utils.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string>
 
 TEST(MemtagGlobalsTest, test) {
-    SKIP_WITH_HWASAN << "b/313613493";
 #if defined(__BIONIC__) && defined(__aarch64__)
   std::string binary = GetPrebuiltElfDir() + "/memtag_globals_binary.so";
   chmod(binary.c_str(), 0755);