Merge "Sync with upstream openbsd."
diff --git a/libc/Android.bp b/libc/Android.bp
index e5a7454..c101f45 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -672,7 +672,6 @@
         },
         riscv64: {
             srcs: [
-                "arch-riscv64/string/memset_chk.c",
                 "upstream-freebsd/lib/libc/string/memcmp.c",
                 "upstream-freebsd/lib/libc/string/memcpy.c",
                 "upstream-freebsd/lib/libc/string/memmove.c",
@@ -825,6 +824,11 @@
                 "arch-arm64/string/__memset_chk.S",
             ],
         },
+        riscv64: {
+            srcs: [
+               "arch-riscv64/string/__memset_chk.S",
+            ],
+        },
     },
 }
 
diff --git a/libc/arch-riscv64/string/__memset_chk.S b/libc/arch-riscv64/string/__memset_chk.S
new file mode 100644
index 0000000..a5562cb
--- /dev/null
+++ b/libc/arch-riscv64/string/__memset_chk.S
@@ -0,0 +1,10 @@
+#include <private/bionic_asm.h>
+
+ENTRY(__memset_chk)
+    bleu    a2, a3, 1f
+    call    __memset_chk_fail
+
+1:
+    tail   memset
+END(__memset_chk)
+
diff --git a/libc/arch-riscv64/string/memset_chk.c b/libc/arch-riscv64/string/memset_chk.c
deleted file mode 100644
index bdd15a5..0000000
--- a/libc/arch-riscv64/string/memset_chk.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stdint.h>
-
-extern void* memset(void*, int, size_t);
-extern void* __memset_chk_fail(void*, int, size_t, size_t);
-
-void* __memset_chk(void* dst, int c, size_t n, size_t dst_len) {
-  if (dst_len < n) __memset_chk_fail(dst, c, n, dst_len);
-  return memset(dst, c, n);
-}
diff --git a/libc/include/inttypes.h b/libc/include/inttypes.h
index f8b7f7d..76aee38 100644
--- a/libc/include/inttypes.h
+++ b/libc/include/inttypes.h
@@ -85,6 +85,42 @@
 #define	PRIiPTR			__PRI_PTR_prefix"i"		/* intptr_t */
 
 /* fprintf macros for unsigned integers */
+#define	PRIb8			"b"		/* int8_t */
+#define	PRIb16			"b"		/* int16_t */
+#define	PRIb32			"b"		/* int32_t */
+#define	PRIb64			__PRI_64_prefix"b"		/* int64_t */
+
+#define	PRIbLEAST8		"b"		/* int_least8_t */
+#define	PRIbLEAST16		"b"		/* int_least16_t */
+#define	PRIbLEAST32		"b"		/* int_least32_t */
+#define	PRIbLEAST64		__PRI_64_prefix"b"		/* int_least64_t */
+
+#define	PRIbFAST8		"b"		/* int_fast8_t */
+#define	PRIbFAST16		__PRI_FAST_prefix"b"	/* int_fast16_t */
+#define	PRIbFAST32		__PRI_FAST_prefix"b"	/* int_fast32_t */
+#define	PRIbFAST64		__PRI_64_prefix"b"		/* int_fast64_t */
+
+#define	PRIbMAX			"jb"		/* intmax_t */
+#define	PRIbPTR			__PRI_PTR_prefix"b"		/* intptr_t */
+
+#define	PRIB8			"B"		/* int8_t */
+#define	PRIB16			"B"		/* int16_t */
+#define	PRIB32			"B"		/* int32_t */
+#define	PRIB64			__PRI_64_prefix"B"		/* int64_t */
+
+#define	PRIBLEAST8		"B"		/* int_least8_t */
+#define	PRIBLEAST16		"B"		/* int_least16_t */
+#define	PRIBLEAST32		"B"		/* int_least32_t */
+#define	PRIBLEAST64		__PRI_64_prefix"B"		/* int_least64_t */
+
+#define	PRIBFAST8		"B"		/* int_fast8_t */
+#define	PRIBFAST16		__PRI_FAST_prefix"B"	/* int_fast16_t */
+#define	PRIBFAST32		__PRI_FAST_prefix"B"	/* int_fast32_t */
+#define	PRIBFAST64		__PRI_64_prefix"B"		/* int_fast64_t */
+
+#define	PRIBMAX			"jB"		/* intmax_t */
+#define	PRIBPTR			__PRI_PTR_prefix"B"		/* intptr_t */
+
 #define	PRIo8			"o"		/* int8_t */
 #define	PRIo16			"o"		/* int16_t */
 #define	PRIo32			"o"		/* int32_t */
@@ -195,6 +231,42 @@
 #define	SCNiPTR			__PRI_PTR_prefix"i"		/* intptr_t */
 
 /* fscanf macros for unsigned integers */
+#define	SCNb8			"hhb"		/* uint8_t */
+#define	SCNb16			"hb"		/* uint16_t */
+#define	SCNb32			"b"		/* uint32_t */
+#define	SCNb64			__PRI_64_prefix"b"		/* uint64_t */
+
+#define	SCNbLEAST8		"hhb"		/* uint_least8_t */
+#define	SCNbLEAST16		"hb"		/* uint_least16_t */
+#define	SCNbLEAST32		"b"		/* uint_least32_t */
+#define	SCNbLEAST64		__PRI_64_prefix"b"		/* uint_least64_t */
+
+#define	SCNbFAST8		"hhb"		/* uint_fast8_t */
+#define	SCNbFAST16		__PRI_FAST_prefix"b"	/* uint_fast16_t */
+#define	SCNbFAST32		__PRI_FAST_prefix"b"	/* uint_fast32_t */
+#define	SCNbFAST64		__PRI_64_prefix"b"		/* uint_fast64_t */
+
+#define	SCNbMAX			"jb"		/* uintmax_t */
+#define	SCNbPTR			__PRI_PTR_prefix"b"		/* uintptr_t */
+
+#define	SCNB8			"hhB"		/* uint8_t */
+#define	SCNB16			"hB"		/* uint16_t */
+#define	SCNB32			"B"		/* uint32_t */
+#define	SCNB64			__PRI_64_prefix"B"		/* uint64_t */
+
+#define	SCNBLEAST8		"hhB"		/* uint_least8_t */
+#define	SCNBLEAST16		"hB"		/* uint_least16_t */
+#define	SCNBLEAST32		"B"		/* uint_least32_t */
+#define	SCNBLEAST64		__PRI_64_prefix"B"		/* uint_least64_t */
+
+#define	SCNBFAST8		"hhB"		/* uint_fast8_t */
+#define	SCNBFAST16		__PRI_FAST_prefix"B"	/* uint_fast16_t */
+#define	SCNBFAST32		__PRI_FAST_prefix"B"	/* uint_fast32_t */
+#define	SCNBFAST64		__PRI_64_prefix"B"		/* uint_fast64_t */
+
+#define	SCNBMAX			"jB"		/* uintmax_t */
+#define	SCNBPTR			__PRI_PTR_prefix"B"		/* uintptr_t */
+
 #define	SCNo8			"hho"		/* uint8_t */
 #define	SCNo16			"ho"		/* uint16_t */
 #define	SCNo32			"o"		/* uint32_t */
diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp
index 4f12fd0..645aefa 100644
--- a/libc/stdio/stdio.cpp
+++ b/libc/stdio/stdio.cpp
@@ -884,7 +884,7 @@
 
 void perror(const char* msg) {
   if (msg == nullptr) msg = "";
-  fprintf(stderr, "%s%s%s\n", msg, (*msg == '\0') ? "" : ": ", strerror(errno));
+  fprintf(stderr, "%s%s%m\n", msg, (*msg == '\0') ? "" : ": ");
 }
 
 int printf(const char* fmt, ...) {
diff --git a/tests/gwp_asan_test.cpp b/tests/gwp_asan_test.cpp
index 38661c7..8b12bec 100644
--- a/tests/gwp_asan_test.cpp
+++ b/tests/gwp_asan_test.cpp
@@ -58,13 +58,10 @@
 // the torture mode is is generally 40,000, so that svelte devices don't
 // explode, as this uses ~163MiB RAM (4KiB per live allocation).
 TEST(gwp_asan_integration, malloc_tests_under_torture) {
-  if (running_with_hwasan()) {
-    // Skip the malloc.zeroed tests since they fail in this particular config.
-    // TODO(b/267386540): Need to fix this problem.
-    RunGwpAsanTest("malloc.*:-malloc.mallinfo*:malloc.zeroed*");
-  } else {
-    RunGwpAsanTest("malloc.*:-malloc.mallinfo*");
-  }
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
+  RunGwpAsanTest("malloc.*:-malloc.mallinfo*");
 }
 
 class SyspropRestorer {
@@ -153,6 +150,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_program_specific) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   std::string path = testing::internal::GetArgvs()[0];
@@ -167,6 +167,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_persist_program_specific) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   std::string path = testing::internal::GetArgvs()[0];
@@ -182,6 +185,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_system) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "1");
@@ -192,6 +198,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_persist_system) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   __system_property_set("persist.libc.debug.gwp_asan.sample_rate.system_default", "1");
@@ -202,6 +211,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_non_persist_overrides_persist) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "1");
@@ -216,6 +228,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_program_specific_overrides_default) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   std::string path = testing::internal::GetArgvs()[0];
@@ -235,6 +250,9 @@
 }
 
 TEST(gwp_asan_integration, sysprops_can_disable) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "0");
@@ -245,6 +263,9 @@
 }
 
 TEST(gwp_asan_integration, env_overrides_sysprop) {
+  // Do not override HWASan with GWP ASan.
+  SKIP_WITH_HWASAN;
+
   SyspropRestorer restorer;
 
   __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "0");
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 907a35c..06a0f3d 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -41,6 +41,7 @@
 #include <android-base/scopeguard.h>
 #include <android-base/silent_death_test.h>
 #include <android-base/strings.h>
+#include <android-base/test_utils.h>
 
 #include "private/bionic_constants.h"
 #include "SignalUtils.h"
@@ -184,6 +185,30 @@
   ASSERT_EQ(0, pthread_key_delete(key));
 }
 
+static void* FnWithStackFrame(void*) {
+  int x;
+  *const_cast<volatile int*>(&x) = 1;
+  return nullptr;
+}
+
+TEST(pthread, pthread_heap_allocated_stack) {
+  SKIP_WITH_HWASAN; // TODO(b/148982147): Re-enable when fixed.
+
+  size_t stack_size = 640 * 1024;
+  std::vector<char> stack_vec(stack_size, '\xff');
+  void* stack = stack_vec.data();
+
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+  ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, &attr, FnWithStackFrame, nullptr));
+
+  void* result;
+  ASSERT_EQ(0, pthread_join(t, &result));
+}
+
 TEST(pthread, static_pthread_key_used_before_creation) {
 #if defined(__BIONIC__)
   // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index acc7ccd..a8db1b0 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -2975,9 +2975,6 @@
 }
 
 TEST(STDIO_TEST, snprintf_b) {
-  // Our clang doesn't know about %b/%B yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   char buf[BUFSIZ];
   EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%b>", 5));
   EXPECT_STREQ("<101>", buf);
@@ -2989,13 +2986,9 @@
   EXPECT_STREQ("<0b10101010101010101010101010101010>", buf);
   EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#b>", 0));
   EXPECT_STREQ("<0>", buf);
-#pragma clang diagnostic pop
 }
 
 TEST(STDIO_TEST, snprintf_B) {
-  // Our clang doesn't know about %b/%B yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   char buf[BUFSIZ];
   EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%B>", 5));
   EXPECT_STREQ("<101>", buf);
@@ -3007,13 +3000,9 @@
   EXPECT_STREQ("<0B10101010101010101010101010101010>", buf);
   EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#B>", 0));
   EXPECT_STREQ("<0>", buf);
-#pragma clang diagnostic pop
 }
 
 TEST(STDIO_TEST, swprintf_b) {
-  // Our clang doesn't know about %b/%B yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   wchar_t buf[BUFSIZ];
   EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%b>", 5));
   EXPECT_EQ(std::wstring(L"<101>"), buf);
@@ -3025,13 +3014,9 @@
   EXPECT_EQ(std::wstring(L"<0b10101010101010101010101010101010>"), buf);
   EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#b>", 0));
   EXPECT_EQ(std::wstring(L"<0>"), buf);
-#pragma clang diagnostic pop
 }
 
 TEST(STDIO_TEST, swprintf_B) {
-  // Our clang doesn't know about %b/%B yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   wchar_t buf[BUFSIZ];
   EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%B>", 5));
   EXPECT_EQ(std::wstring(L"<101>"), buf);
@@ -3043,7 +3028,6 @@
   EXPECT_EQ(std::wstring(L"<0B10101010101010101010101010101010>"), buf);
   EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#B>", 0));
   EXPECT_EQ(std::wstring(L"<0>"), buf);
-#pragma clang diagnostic pop
 }
 
 TEST(STDIO_TEST, scanf_i_decimal) {
@@ -3143,10 +3127,6 @@
 }
 
 TEST(STDIO_TEST, scanf_b) {
-  // Our clang doesn't know about %b yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat"
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   int i;
   char ch;
   EXPECT_EQ(2, sscanf("<1012>", "<%b%c>", &i, &ch));
@@ -3159,14 +3139,9 @@
   EXPECT_EQ(2, sscanf("-0b", "%i%c", &i, &ch));
   EXPECT_EQ(0, i);
   EXPECT_EQ('b', ch);
-#pragma clang diagnostic pop
 }
 
 TEST(STDIO_TEST, swscanf_b) {
-  // Our clang doesn't know about %b yet.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat"
-#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
   int i;
   char ch;
   EXPECT_EQ(2, swscanf(L"<1012>", L"<%b%c>", &i, &ch));
@@ -3179,5 +3154,4 @@
   EXPECT_EQ(2, swscanf(L"-0b", L"%i%c", &i, &ch));
   EXPECT_EQ(0, i);
   EXPECT_EQ('b', ch);
-#pragma clang diagnostic pop
 }