Merge "Mandate optimized __memset_chk for arm and arm64."
diff --git a/libc/Android.mk b/libc/Android.mk
index 3983a77..828a227 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -232,7 +232,6 @@
 
 libc_bionic_src_files += \
     bionic/__memcpy_chk.cpp \
-    bionic/__memset_chk.cpp \
     bionic/__strcat_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/strchr.cpp \
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 76f465e..6c11169 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -10,7 +10,6 @@
 
 libc_bionic_src_files_exclude_arm += \
     bionic/__memcpy_chk.cpp \
-    bionic/__memset_chk.cpp \
 
 libc_openbsd_src_files_exclude_arm += \
     upstream-openbsd/lib/libc/string/strcpy.c \
diff --git a/libc/arch-arm64/denver64/bionic/memset.S b/libc/arch-arm64/denver64/bionic/memset.S
index 9127d89..bea5b26 100644
--- a/libc/arch-arm64/denver64/bionic/memset.S
+++ b/libc/arch-arm64/denver64/bionic/memset.S
@@ -48,6 +48,7 @@
 #define dstin		x0
 #define val		w1
 #define count		x2
+#define dst_count x3 /* for __memset_chk */
 #define tmp1		x3
 #define tmp1w		w3
 #define tmp2		x4
@@ -63,6 +64,19 @@
 
 #define QA_l		q0
 
+ENTRY(__memset_chk)
+  cmp count, dst_count
+  bls memset
+
+  // Preserve for accurate backtrace.
+  stp x29, x30, [sp, -16]!
+  .cfi_def_cfa_offset 16
+  .cfi_rel_offset x29, 0
+  .cfi_rel_offset x30, 8
+
+  bl __memset_chk_fail
+END(__memset_chk)
+
 ENTRY(memset)
 
 	mov	dst, dstin		/* Preserve return value.  */
diff --git a/libc/arch-arm64/generic/bionic/memset.S b/libc/arch-arm64/generic/bionic/memset.S
index 7c204b4..9626c0b 100644
--- a/libc/arch-arm64/generic/bionic/memset.S
+++ b/libc/arch-arm64/generic/bionic/memset.S
@@ -45,12 +45,9 @@
    values rather than re-reading them each call.  */
 
 #define dstin		x0
-#ifdef BZERO
-#define count		x1
-#else
-#define count		x2
-#endif
 #define val		w1
+#define count		x2
+#define dst_count x3 /* for __memset_chk */
 #define tmp1		x3
 #define tmp1w		w3
 #define tmp2		x4
@@ -64,16 +61,22 @@
 #define dst		x8
 #define tmp3w		w9
 
-#ifdef BZERO
-ENTRY(bzero)
-#else
+ENTRY(__memset_chk)
+  cmp count, dst_count
+  bls memset
+
+  // Preserve for accurate backtrace.
+  stp x29, x30, [sp, -16]!
+  .cfi_def_cfa_offset 16
+  .cfi_rel_offset x29, 0
+  .cfi_rel_offset x30, 8
+
+  bl __memset_chk_fail
+END(__memset_chk)
+
 ENTRY(memset)
-#endif
 
 	mov	dst, dstin		/* Preserve return value.  */
-#ifdef BZERO
-	b	.Lzero_mem
-#endif
 	ands	A_lw, val, #255
 	b.eq	.Lzero_mem
 	orr	A_lw, A_lw, A_lw, lsl #8
@@ -233,11 +236,7 @@
 	ands	count, count, zva_bits_x
 	b.ne	.Ltail_maybe_long
 	ret
-#ifdef BZERO
-END(bzero)
-#else
 END(memset)
-#endif
 
 #ifdef MAYBE_VIRT
 	.bss
diff --git a/libc/bionic/__memset_chk.cpp b/libc/bionic/__memset_chk.cpp
deleted file mode 100644
index 9e7b940..0000000
--- a/libc/bionic/__memset_chk.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-#undef _FORTIFY_SOURCE
-
-#include <string.h>
-
-#include "private/bionic_fortify.h"
-
-// Runtime implementation of __builtin___memset_chk (used directly by compiler, not in headers).
-extern "C" void* __memset_chk(void* dst, int byte, size_t count, size_t dst_len) {
-  __check_count("memset", "count", count);
-  __check_buffer_access("memset", "write into", count, dst_len);
-  return memset(dst, byte, count);
-}
diff --git a/libc/bionic/fortify.cpp b/libc/bionic/fortify.cpp
index ad7aa04..a1db2a4 100644
--- a/libc/bionic/fortify.cpp
+++ b/libc/bionic/fortify.cpp
@@ -153,6 +153,15 @@
   return memrchr(s, c, n);
 }
 
+#if !defined(__aarch64__) && !defined(__arm__) // TODO: add optimized assembler for the others too.
+// Runtime implementation of __builtin___memset_chk (used directly by compiler, not in headers).
+extern "C" void* __memset_chk(void* dst, int byte, size_t count, size_t dst_len) {
+  __check_count("memset", "count", count);
+  __check_buffer_access("memset", "write into", count, dst_len);
+  return memset(dst, byte, count);
+}
+#endif
+
 // memset is performance-critical enough that we have assembler __memset_chk implementations.
 // This function is used to give better diagnostics than we can easily do from assembler.
 extern "C" void* __memset_chk_fail(void* /*dst*/, int /*byte*/, size_t count, size_t dst_len) {