Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RISCV: handle divide by 0 #6508

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sleigh-InSPECtor
Copy link
Contributor

As apart of a research project testing the accuracy of the sleigh specifications compared to real hardware, we observed unexpected behaviour in the divide and remainder instructions for RISCV. These include the div, divu, rem, remu, divw, divuw, remw, remuw instructions. According to section 7.2 of the riscv-spec-20191213 specification the result of dividing by zero is all 1's, and the result of the remainder of zero is the dividend. The current behaviour does not check if the divisor is zero which causes a division exception on these cases. Handling this case does add an if statement which shows in decompilation, but is how it is handled in both AARCH64 and ARM with the SDIV and UDIV instructions, and MIPS16 with the div and divu instructions. There is also an edge case for overflowing division which has not been handled.

Decompilation without fix

int division(int param_1,int param_2)
{
  gp = &__global_pointer$;
  return param_1 / param_2;
}

Decompilation with fix

int division(int param_1,int param_2)
{
  gp = &__global_pointer$;
  if (param_2 == 0) {
    param_1 = -1;
  }
  else {
    param_1 = param_1 / param_2;
  }
  return param_1;
}

@Sleigh-InSPECtor Sleigh-InSPECtor changed the title riscv: handle divideby0 RISCV: handle divide by 0 May 15, 2024
@GhidorahRex GhidorahRex self-assigned this May 15, 2024
@GhidorahRex GhidorahRex added Type: Bug Something isn't working Feature: Processor/RISC-V Status: Triage Information is being gathered labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Processor/RISC-V Status: Triage Information is being gathered Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants