Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Ensure we differentiate between division by zero and overflow - #737

Merged
jkotas merged 1 commit into
dotnet:masterfrom
janvorli:fix-div-overflow-exception
Apr 18, 2015
Merged

jkotas merged 1 commit into
dotnet:masterfrom
janvorli:fix-div-overflow-exception

Conversation

@janvorli

Copy link
Copy Markdown
Member

This change implements parsing of the IDIV instruction operand so that
we can find whether an integer division error was a division by zero or
an integer overflow.
It replaces the previous functionality that was implemented for OSX only
and was incomplete (it didn't handle memory operands) and partially incorrect
(not handling R8..R15 properly).
I have reused code from https://github.com/dotnet/coreclr/blob/master/src/debug/ee/amd64/amd64walker.cpp#L27,
but I have enhanced it to be complete w.r.t. the multiple possible operand sizes. The original code was created for parsing call / jmp operand where only the 64 bit operand was possible. In the idiv, 8, 16, 32 or 64 bit operands are possible.

@janvorli

Copy link
Copy Markdown
Member Author

@jkotas Can you take a look please?

Comment thread src/pal/src/exception/seh.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not look right that this is unsigned - could you please double check it?

Here is a small repro to try:

using System;
using System.Runtime.CompilerServices;

unsafe class My {
    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    static int foo(int *p) {
        return 5/ *(p-1);
    }
    static void Main() {
        int[] a = new int[2];
            fixed(int * p = a)
        foo(p);
    }
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it looks strange. Let me take a look. Btw, this is what we have in the amd64walker.cpp, I haven't functionaly modified this piece.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is extremely rare to have negative offset for indirect call target, so if would not be surprising if the amd64walker had a bug. We should fix there too if there is one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As expected, it is really incorrect. Just from looking at few compiled instructions:

idiv dword ptr [rax+1] -> f7 78 01
idiv dword ptr [rax-1] -> f7 78 ff
idiv dword ptr [rax-512] -> f7 b8 00 fe ff ff
idiv dword ptr [rax+512] -> f7 b8 00 02 00 00

@jkotas

jkotas commented Apr 17, 2015

Copy link
Copy Markdown
Member

Looks good to me otherwise.

@janvorli
janvorli force-pushed the fix-div-overflow-exception branch from 1d64ab4 to d6d5e98 Compare April 18, 2015 00:26
@janvorli

Copy link
Copy Markdown
Member Author

I've fixed the wrong unsigned offsets (also in the amd64walker.cpp) and moved the detection code to exceptionhandling.cpp.
I've amended the previous commit since it would be disjunct with this one anyways.

@janvorli

Copy link
Copy Markdown
Member Author

Oh, the VS copy and paste screwed the formatting of the function comment headers. Let me fix it.

This change implements parsing of the IDIV instruction operand so that
we can find whether an integer division error was a division by zero or
an integer overflow.
It replaces the previous functionality that was implemented for OSX only
and was incomplete (it didn't handle memory operands) and partially incorrect
(not handling R8..R15 properly).
@janvorli
janvorli force-pushed the fix-div-overflow-exception branch from d6d5e98 to ac0ce45 Compare April 18, 2015 00:32
@janvorli

Copy link
Copy Markdown
Member Author

Fixed

@kangaroo

Copy link
Copy Markdown

Build failure doesn't seem related

@kangaroo

Copy link
Copy Markdown

@dotnet-bot test this please

@jkotas

jkotas commented Apr 18, 2015

Copy link
Copy Markdown
Member

LGTM

jkotas added a commit that referenced this pull request Apr 18, 2015
Ensure we differentiate between division by zero and overflow
@jkotas
jkotas merged commit 1987adb into dotnet:master Apr 18, 2015
@janvorli
janvorli deleted the fix-div-overflow-exception branch April 18, 2015 08:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants