Skip to content

Fix mapping of integer overflow in division to managed exception #4138

Closed

Description

@janvorli

Linux linux integer overflow during division to the same signal code as division by zero in some cases, the si_signo is SIGFPE and the si_code is FPE_INTDIV and so we generate division by zero exception instead of integer overflow.
It seems that we will need to check the failing instruction code, find the divisor value and check if it is zero. Based on that we would either return EXCEPTION_INT_DIVIDE_BY_ZERO or EXCEPTION_INT_OVERFLOW.

Activity

  1. self-assigned this
    on Apr 16, 2015
  2. janvorli commented on Apr 16, 2015

    @janvorli
    MemberAuthor

    We actually do the same thing for OSX here:
    https://github.com/dotnet/coreclr/blob/master/src/pal/src/exception/machexception.cpp#L647
    So we should be able to reuse the IsDivByZeroAnIntegerOverflow function.

  3. janvorli commented on Apr 16, 2015

    @janvorli
    MemberAuthor

    The code we had is not complete. It doesn't handle memory operands. We need to add support for those since two tests in System.Dynamic.Runtime.Tests are failing due to division overflow reported as division by zero for memory operands.

  4. mikedn commented on Apr 16, 2015

    @mikedn
    Contributor

    What happens if the contents of the memory operand is modified by another thread after the exception is thrown? Would IsDivByZeroAnIntegerOverflow check lead to the wrong exception being thrown?

  5. janvorli commented on Apr 16, 2015

    @janvorli
    MemberAuthor

    You are right, it would (if we extended the current verification to the memory operands).
    But there seems to be no other way to find out whether a division by zero or integer overflow happened.
    Another bad issue could happen in case the memory of the target operand got unmapped from the address space by another thread before we had a chance to check.
    I am still investigating if there is an alternative way to figure out the difference.

    An alternative approach to solve the issue would be to make jitter issue zero check for the divisor before the actual division and throw the div by zero in that case explicitly. That way, we could convert all hardware exceptions with code EXCEPTION_INT_DIVIDE_BY_ZERO to EXCEPTION_INT_OVERFLOW.

  6. janvorli commented on Apr 16, 2015

    @janvorli
    MemberAuthor

    Btw, Windows do the same thing as I was originally suggesting - decode the instruction, find the address of the operand and read its value. So it seems that unless we want to perform the check before every division, we don't have other option.

  7. mikedn commented on Apr 16, 2015

    @mikedn
    Contributor

    So it seems that unless we want to perform the check before every division, we don't have other option.

    It looks like the x86 JIT forces the divisor into a register if it's not a local, I wonder if that's just a coincidence or it was done on purpose. RyuJIT doesn't do it but I supposed it could be modified to do it. Though I'm not sure if it's worth the trouble, this situation should be rare.

  8. janvorli commented on Apr 18, 2015

    @janvorli
    MemberAuthor
  9. transferred this issue fromdotnet/coreclron Jan 30, 2020
  10. ghost locked as resolved and limited conversation to collaborators on Jan 6, 2021
  11. added
    in-pr
    on Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-PAL-coreclrbugin-pros-linux

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions