Add x86 __memset_chk assembler.
Change-Id: Ic6732f761ea8a41f70d5ff59d47ad38417d17cc2
diff --git a/libc/arch-x86/atom/string/sse2-memset-atom.S b/libc/arch-x86/atom/string/sse2-memset-atom.S
index e8ceee1..30fb3f1 100644
--- a/libc/arch-x86/atom/string/sse2-memset-atom.S
+++ b/libc/arch-x86/atom/string/sse2-memset-atom.S
@@ -28,6 +28,8 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <private/bionic_asm.h>
+
#include "cache.h"
#ifndef L
@@ -38,14 +40,6 @@
# define ALIGN(n) .p2align n
#endif
-#ifndef cfi_startproc
-# define cfi_startproc .cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc .cfi_endproc
-#endif
-
#ifndef cfi_rel_offset
# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
#endif
@@ -58,21 +52,6 @@
# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
#endif
-#ifndef ENTRY
-# define ENTRY(name) \
- .type name, @function; \
- .globl name; \
- .p2align 4; \
-name: \
- cfi_startproc
-#endif
-
-#ifndef END
-# define END(name) \
- cfi_endproc; \
- .size name, .-name
-#endif
-
#define CFI_PUSH(REG) \
cfi_adjust_cfa_offset (4); \
cfi_rel_offset (REG, 0)
@@ -84,10 +63,11 @@
#define PUSH(REG) pushl REG; CFI_PUSH (REG)
#define POP(REG) popl REG; CFI_POP (REG)
-#define DEST PARMS
-#define CHR DEST+4
+#define DST PARMS
+#define CHR DST+4
#define LEN CHR+4
-#define SETRTNVAL movl DEST(%esp), %eax
+#define CHK_DST_LEN (LEN+4)
+#define SETRTNVAL movl DST(%esp), %eax
#if (defined SHARED || defined __PIC__)
# define ENTRANCE PUSH (%ebx);
@@ -132,13 +112,17 @@
jmp *TABLE(,%ecx,4)
#endif
-#ifndef MEMSET
-# define MEMSET memset
-#endif
+ENTRY(__memset_chk)
+ movl LEN(%esp), %ecx
+ cmpl %ecx, CHK_DST_LEN(%esp)
+ jbe memset
+
+ jmp __memset_chk_fail
+END(__memset_chk)
.section .text.sse2,"ax",@progbits
ALIGN (4)
-ENTRY (MEMSET)
+ENTRY (memset)
ENTRANCE
movl LEN(%esp), %ecx
@@ -148,7 +132,7 @@
movl %eax, %edx
shl $16, %eax
or %edx, %eax
- movl DEST(%esp), %edx
+ movl DST(%esp), %edx
cmp $32, %ecx
jae L(32bytesormore)
@@ -903,4 +887,4 @@
SETRTNVAL
RETURN_END
-END (MEMSET)
+END (memset)
diff --git a/libc/arch-x86/silvermont/string/sse2-memset-slm.S b/libc/arch-x86/silvermont/string/sse2-memset-slm.S
index 489f64e..0718fa7 100644
--- a/libc/arch-x86/silvermont/string/sse2-memset-slm.S
+++ b/libc/arch-x86/silvermont/string/sse2-memset-slm.S
@@ -28,11 +28,9 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include "cache.h"
+#include <private/bionic_asm.h>
-#ifndef MEMSET
-# define MEMSET memset
-#endif
+#include "cache.h"
#ifndef L
# define L(label) .L##label
@@ -42,14 +40,6 @@
# define ALIGN(n) .p2align n
#endif
-#ifndef cfi_startproc
-# define cfi_startproc .cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc .cfi_endproc
-#endif
-
#ifndef cfi_rel_offset
# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
#endif
@@ -62,21 +52,6 @@
# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
#endif
-#ifndef ENTRY
-# define ENTRY(name) \
- .type name, @function; \
- .globl name; \
- .p2align 4; \
-name: \
- cfi_startproc
-#endif
-
-#ifndef END
-# define END(name) \
- cfi_endproc; \
- .size name, .-name
-#endif
-
#define CFI_PUSH(REG) \
cfi_adjust_cfa_offset (4); \
cfi_rel_offset (REG, 0)
@@ -88,10 +63,11 @@
#define PUSH(REG) pushl REG; CFI_PUSH (REG)
#define POP(REG) popl REG; CFI_POP (REG)
-#define DEST PARMS
-#define CHR DEST+4
+#define DST PARMS
+#define CHR DST+4
#define LEN CHR+4
-#define SETRTNVAL movl DEST(%esp), %eax
+#define CHK_DST_LEN (LEN+4)
+#define SETRTNVAL movl DST(%esp), %eax
#if (defined SHARED || defined __PIC__)
# define ENTRANCE PUSH (%ebx);
@@ -136,9 +112,17 @@
jmp *TABLE(,%ecx,4)
#endif
+ENTRY(__memset_chk)
+ movl LEN(%esp), %ecx
+ cmpl %ecx, CHK_DST_LEN(%esp)
+ jbe memset
+
+ jmp __memset_chk_fail
+END(__memset_chk)
+
.section .text.sse2,"ax",@progbits
ALIGN (4)
-ENTRY (MEMSET)
+ENTRY(memset)
ENTRANCE
movl LEN(%esp), %ecx
@@ -154,7 +138,7 @@
movl %eax, %edx
shl $16, %eax
or %edx, %eax
- movl DEST(%esp), %edx
+ movl DST(%esp), %edx
cmp $1, %ecx
je L(1byte)
cmp $16, %ecx
@@ -824,4 +808,4 @@
SETRTNVAL
RETURN_END
-END (MEMSET)
+END(memset)
diff --git a/libc/bionic/fortify.cpp b/libc/bionic/fortify.cpp
index ccbdec0..31a9e84 100644
--- a/libc/bionic/fortify.cpp
+++ b/libc/bionic/fortify.cpp
@@ -153,7 +153,7 @@
return memrchr(s, c, n);
}
-#if !defined(__aarch64__) && !defined(__arm__) && !defined(__x86_64__) // TODO: add optimized assembler for the others too.
+#if defined(__mips__) // TODO: add optimized assembler for mips too, and remove this.
// 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);