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

8277299: STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits #6473

Closed
wants to merge 5 commits into from

Conversation

azuev-java
Copy link
Member

@azuev-java azuev-java commented Nov 19, 2021

Made colorBits and maskBits arrays dynamic so they are allocated on heap instead of stack.
Added regression test.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8277299: STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6473/head:pull/6473
$ git checkout pull/6473

Update a local copy of the PR:
$ git checkout pull/6473
$ git pull https://git.openjdk.java.net/jdk pull/6473/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6473

View PR using the GUI difftool:
$ git pr show -t 6473

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6473.diff

…onBits

Made colorBits and maskBits arraus dynamic so they allocated on heap instead of stack.
Added regression test.
@bridgekeeper
Copy link

bridgekeeper bot commented Nov 19, 2021

👋 Welcome back kizune! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 19, 2021
@openjdk
Copy link

openjdk bot commented Nov 19, 2021

@azuev-java The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Nov 19, 2021
@mlbridge
Copy link

mlbridge bot commented Nov 19, 2021

Webrevs

@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits
bmi.bmiHeader.biCompression = BI_RGB;
// Extract the color bitmap
int nBits = iconSize * iconSize;
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE];
long * colorBits;
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.
+Probably it will be better to merge assigning into one line.

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.

I can't see any try-catch.
Is it better to use malloc and check for NULL?

Probably it will be better to merge assigning into one line.

I agree. The line doesn't get too long.

Copy link
Member

Choose a reason for hiding this comment

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

Probably TRY/CATCH_BAD_ALLOC_RET(NULL); could be used. It depends on how we will clean the stuff on exception.

@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits
bmi.bmiHeader.biCompression = BI_RGB;
// Extract the color bitmap
int nBits = iconSize * iconSize;
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE];
long * colorBits;
Copy link
Member

Choose a reason for hiding this comment

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

Pointer declarations aren't consistent in the file, the function parameter is declared as JNIEnv* env. However, in the majority of cases the asterisk is near the variable name as in const char *pLibName. In either case, there's only one space:

long *maskBits;

or

long* maskBits;

@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits
bmi.bmiHeader.biCompression = BI_RGB;
// Extract the color bitmap
int nBits = iconSize * iconSize;
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE];
long * colorBits;
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.

I can't see any try-catch.
Is it better to use malloc and check for NULL?

Probably it will be better to merge assigning into one line.

I agree. The line doesn't get too long.

* @test
* @bug 8277299
* @requires (os.family == "windows")
* @summary STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this could be spelt with regular case rather than caps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this could be spelt with regular case rather than caps?

Well, it is just copy-paste from the bug synopsis which is copy-paste from the error message of the VM crash.

Fixed insets
Declaration and assignment are now joined
free(colorBits);
}

CATCH_BAD_ALLOC_RET(NULL);
Copy link
Member

Choose a reason for hiding this comment

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

I believe we leak dc as well as iconInfo.hbmColor and iconInfo.hbmMask if std::bad_alloc is thrown.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. Now we will release these resources on exit in case of bad_alloc.

Comment on lines 1061 to 1068
try {
entry_point();
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));
} catch(std::bad_alloc&) {
handle_bad_alloc();
}

if (colorBits != NULL) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this way defeats the use of exception to handle allocation error. You could use:

long *colorBits = (long*)malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));
if (colorBits != NULL) {

to achieve the same effect, which is shorter and clearer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but then handle_bad_alloc() will not be called which will not raise the OutOfMemoryError on the java side.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but then handle_bad_alloc() will not be called which will not raise the OutOfMemoryError on the java side.

Can't we call it explicitly in the case where colorBits == NULL?

Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can't we call it explicitly in the case where colorBits == NULL?

I would use the default handlers for bad_alloc since they can be changed in feature and keeping code that is intended to be used in two places is usually not a good idea.

Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.

That might require a lot of code reformatting and the benefits are unclear.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.

That might require a lot of code reformatting and the benefits are unclear.

I tried it, it doesn't, see this diff. It's close to your latest code, however, there's only one catch that handles both allocations and there are no if's inside the try-block.

Copy link
Member

Choose a reason for hiding this comment

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

What about this approach?
Compared to master
Compared to your latest code

I think it's cleaner in the use of exceptions. Each try-block allocates memory that it uses and frees it before the catch-block. There's no need for if's: if exception is thrown, the memory wasn't allocated, therefore the memory is freed only if no exception is thrown.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the singular try/catch block approach better so i did that in the latest commit.

Comment on lines 1101 to 1102
// Release DC
ReleaseDC(NULL, dc);
Copy link
Member

Choose a reason for hiding this comment

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

The DC has to be released even if colorBits allocation failed, so this needs to be after if (colorBits != NULL).

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Probably, you hasn't pushed the change for releasing the DC.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed.

Copy link
Member

@aivanov-jdk aivanov-jdk left a comment

Choose a reason for hiding this comment

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

I admit I prefer the 2nd version with two try-catch blocks better because it avoids using if's completely.

Anyway, this version looks good to me. Thanks.

Comment on lines +1079 to +1083
// Extract the mask bitmap

maskBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));

GetDIBits(dc, iconInfo.hbmMask, 0, iconSize, maskBits, &bmi, DIB_RGB_COLORS);
Copy link
Member

Choose a reason for hiding this comment

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

Probably the empty lines here aren't necessary.

@openjdk
Copy link

openjdk bot commented Dec 10, 2021

@azuev-java This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8277299: STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits

Reviewed-by: aivanov

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 345 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 10, 2021
@azuev-java azuev-java closed this Dec 11, 2021
@azuev-java azuev-java deleted the JDK-8277299 branch January 31, 2022 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org ready Pull request is ready to be integrated rfr Pull request is ready for review
3 participants