• XSS.stack #1 – первый литературный журнал от юзеров форума

ASSEMBLY SIGFPE ERROR

basileusapoleiaoff

HDD-drive
Пользователь
Регистрация
02.08.2023
Сообщения
40
Реакции
21
Hello forum,

I try to run my program and i came across this error:

Floating point exception (core dumped)
when I step in with pwndbg i came across this error:
Program terminated with signal SIGFPE, Arithmetic exception. (at the line divq %rdi)

I'm interested in this behavior, why the program crash here ?
If anybody has a fix also

Here is the source code
C:
#this program call odd_even write in ASM
int odd_even(int);
int main(){
    return odd_even(4);
}

Код:
#this program check if the number is odd or even then call a function

.globl odd_even
.type odd_even, @function
.section .text
odd_even:
    movq %rdi, %rax
    movq $2, %rdi
    divq %rdi #ERROR OCCUR HERE
    movq $0, %rdx
    cmpq $0, %rdx
    je even
    jmp odd
even:
    call factorial
    leave
    ret
odd:
    call exponent
    leave
    ret


Код:
.globl exponent
.type exponent, @function
.section .text
exponent:
    movq $0, %rax
    addq %rdi, %rax
    mulq %rdi
    mulq %rdi
    leave
    ret


Код:
.globl factorial
.type factorial, @function
.section .data
.equ OFFSET_FRAME, -8
.section .text
factorial:
    enter $16, $0
    movq $0, %rax
    addq %rdi, %rax
    movq %rdi, OFFSET_FRAME(%rbp)
mainloop:
    cmpq $0, OFFSET_FRAME(%rbp)
    je finish
    mulq OFFSET_FRAME(%rbp)
    decq OFFSET_FRAME(%rbp)
    jmp mainloop
finish:
    leave
    ret

I comple with gcc and i get this from gnu documentation

Macro: int SIGFPE
The SIGFPE signal reports a fatal arithmetic error. Although thename is derived from “floating-point exception”, this signal actuallycovers all arithmetic errors, including division by zero and overflow.If a program stores integer data in a location which is then used in afloating-point operation, this often causes an “invalid operation”exception, because the processor cannot recognize the data as afloating-point number.


Actual floating-point exceptions are a complicated subject because thereare many types of exceptions with subtly different meanings, and theSIGFPE signal doesn’t distinguish between them. The IEEEStandard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985and ANSI/IEEE Std 854-1987)defines various floating-point exceptions and requires conformingcomputer systems to report their occurrences. However, this standarddoes not specify how the exceptions are reported, or what kinds ofhandling and control the operating system can offer to the programmer.


BSD systems provide the SIGFPE handler with an extra argumentthat distinguishes various causes of the exception. In order to accessthis argument, you must define the handler to accept two arguments,which means you must cast it to a one-argument function type in order toestablish the handler. The GNU C Library does provide this extraargument, but the value is meaningful only on operating systems thatprovide the information (BSD systems and GNU systems).

FPE_INTOVF_TRAP
Integer overflow (impossible in a C program unless you enable overflowtrapping in a hardware-specific fashion).

FPE_INTDIV_TRAP
Integer division by zero.

FPE_SUBRNG_TRAP
Subscript-range (something that C programs never check for).

FPE_FLTOVF_TRAP
Floating overflow trap.

FPE_FLTDIV_TRAP
Floating/decimal division by zero.

FPE_FLTUND_TRAP
Floating underflow trap. (Trapping on floating underflow is notnormally enabled.)

FPE_DECOVF_TRAP
Decimal overflow trap. (Only a few machines have decimal arithmetic andC never uses it.)
 
Решение


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх