Blame sysdeps/unix/sysv/linux/i386/____longjmp_chk.S

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <jmpbuf-offsets.h>
Packit 6c4009
#include <jmp_buf-ssp.h>
Packit 6c4009
#include <asm-syntax.h>
Packit 6c4009
#include <stap-probe.h>
Packit 6c4009
Packit 6c4009
/* Don't restore shadow stack register if shadow stack isn't enabled.  */
Packit 6c4009
#if !SHSTK_ENABLED
Packit 6c4009
# undef SHADOW_STACK_POINTER_OFFSET
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	.section .rodata.str1.1,"aMS",@progbits,1
Packit 6c4009
	.type	longjmp_msg,@object
Packit 6c4009
longjmp_msg:
Packit 6c4009
	.string "longjmp causes uninitialized stack frame"
Packit 6c4009
	.size	longjmp_msg, .-longjmp_msg
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef PIC
Packit 6c4009
# define CALL_FAIL	movl	%ebx, %ecx;				      \
Packit 6c4009
			cfi_register(%ebx,%ecx);			      \
Packit 6c4009
			LOAD_PIC_REG (bx);				      \
Packit 6c4009
			leal	longjmp_msg@GOTOFF(%ebx), %eax;		      \
Packit 6c4009
			movl	%eax, (%esp);				      \
Packit 6c4009
			call	HIDDEN_JUMPTARGET(__fortify_fail)
Packit 6c4009
#else
Packit 6c4009
# define CALL_FAIL	movl	$longjmp_msg, %eax;			      \
Packit 6c4009
			movl	%eax, (%esp);				      \
Packit 6c4009
			call	HIDDEN_JUMPTARGET(__fortify_fail)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (____longjmp_chk)
Packit 6c4009
	movl	4(%esp), %ecx	/* User's jmp_buf in %ecx.  */
Packit 6c4009
Packit 6c4009
#ifdef SHADOW_STACK_POINTER_OFFSET
Packit 6c4009
# if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
Packit 6c4009
	/* Check if Shadow Stack is enabled.  */
Packit 6c4009
	testl	$X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
Packit 6c4009
	jz	L(skip_ssp)
Packit 6c4009
# else
Packit 6c4009
	xorl	%edx, %edx
Packit 6c4009
# endif
Packit 6c4009
	/* Check and adjust the Shadow-Stack-Pointer.  */
Packit 6c4009
	rdsspd	%edx
Packit 6c4009
	/* And compare it with the saved ssp value.  */
Packit 6c4009
	subl	SHADOW_STACK_POINTER_OFFSET(%ecx), %edx
Packit 6c4009
	je	L(skip_ssp)
Packit 6c4009
	/* Count the number of frames to adjust and adjust it
Packit 6c4009
	   with incssp instruction.  The instruction can adjust
Packit 6c4009
	   the ssp by [0..255] value only thus use a loop if
Packit 6c4009
	   the number of frames is bigger than 255.  */
Packit 6c4009
	negl	%edx
Packit 6c4009
	shrl	$2, %edx
Packit 6c4009
	/* NB: We saved Shadow-Stack-Pointer of setjmp.  Since we are
Packit 6c4009
	       restoring Shadow-Stack-Pointer of setjmp's caller, we
Packit 6c4009
	       need to unwind shadow stack by one more frame.  */
Packit 6c4009
	addl	$1, %edx
Packit 6c4009
	movl	$255, %ebx
Packit 6c4009
L(loop):
Packit 6c4009
	cmpl	%ebx, %edx
Packit 6c4009
	cmovb	%edx, %ebx
Packit 6c4009
	incsspd	%ebx
Packit 6c4009
	subl	%ebx, %edx
Packit 6c4009
	ja	L(loop)
Packit 6c4009
L(skip_ssp):
Packit 6c4009
#endif
Packit 6c4009
	/* Save the return address now.  */
Packit 6c4009
	movl	(JB_PC*4)(%ecx), %edx
Packit 6c4009
	/* Get the stack pointer.  */
Packit 6c4009
	movl	(JB_SP*4)(%ecx), %edi
Packit 6c4009
	cfi_undefined(%edi)
Packit 6c4009
	PTR_DEMANGLE (%edx)
Packit 6c4009
	PTR_DEMANGLE (%edi)
Packit 6c4009
Packit 6c4009
	cmpl	%edi, %esp
Packit 6c4009
	jbe	.Lok
Packit 6c4009
Packit 6c4009
	subl	$12, %esp
Packit 6c4009
	cfi_adjust_cfa_offset(12)
Packit 6c4009
	xorl	%ebx, %ebx
Packit 6c4009
	movl	%esp, %ecx
Packit 6c4009
	movl	$__NR_sigaltstack, %eax
Packit 6c4009
	ENTER_KERNEL
Packit 6c4009
	/* Without working sigaltstack we cannot perform the test.  */
Packit 6c4009
	test	%eax, %eax
Packit 6c4009
	jne	.Lok2
Packit 6c4009
	testl	$1, 4(%esp)
Packit 6c4009
	jz	.Lfail
Packit 6c4009
Packit 6c4009
	movl	(%esp), %eax
Packit 6c4009
	addl	8(%esp), %eax
Packit 6c4009
	subl	%edi, %eax
Packit 6c4009
	cmpl	8(%esp), %eax
Packit 6c4009
	jae	.Lok2
Packit 6c4009
Packit 6c4009
.Lfail:	CALL_FAIL
Packit 6c4009
Packit 6c4009
.Lok2:	addl	$12, %esp
Packit 6c4009
	cfi_adjust_cfa_offset(-12)
Packit 6c4009
	movl	4(%esp), %ecx
Packit 6c4009
Packit 6c4009
.Lok:
Packit 6c4009
	LIBC_PROBE (longjmp, 3, 4@%ecx, -4@8(%esp), 4@%edx)
Packit 6c4009
	/* We add unwind information for the target here.  */
Packit 6c4009
	cfi_def_cfa(%ecx, 0)
Packit 6c4009
	cfi_register(%eip, %edx)
Packit 6c4009
	cfi_register(%esp, %edi)
Packit 6c4009
	cfi_offset(%ebx, JB_BX*4)
Packit 6c4009
	cfi_offset(%esi, JB_SI*4)
Packit 6c4009
	cfi_offset(%edi, JB_DI*4)
Packit 6c4009
	cfi_offset(%ebp, JB_BP*4)
Packit 6c4009
Packit 6c4009
	movl	8(%esp), %eax	/* Second argument is return value.  */
Packit 6c4009
	movl	%edi, %esp
Packit 6c4009
Packit 6c4009
	/* Restore registers.  */
Packit 6c4009
	movl	(JB_BX*4)(%ecx), %ebx
Packit 6c4009
	movl	(JB_SI*4)(%ecx), %esi
Packit 6c4009
	movl	(JB_DI*4)(%ecx), %edi
Packit 6c4009
	movl	(JB_BP*4)(%ecx), %ebp
Packit 6c4009
	cfi_restore(%ebx)
Packit 6c4009
	cfi_restore(%esi)
Packit 6c4009
	cfi_restore(%edi)
Packit 6c4009
	cfi_restore(%ebp)
Packit 6c4009
Packit 6c4009
	/* Jump to saved PC.  */
Packit 6c4009
	LIBC_PROBE (longjmp_target, 3, 4@%ecx, -4@%eax, 4@%edx)
Packit 6c4009
	jmp	*%edx
Packit 6c4009
END (____longjmp_chk)