Skip to content

Conversation

@TheShermanTanker
Copy link
Contributor

@TheShermanTanker TheShermanTanker commented Nov 12, 2024

JDK-8305590 removed -fcheck-new when building with gcc. It turns out Visual Studio has a similar option, though inverted in behavior and default.

It seems like /Zc:throwingNew- (the default) corresponds to gcc -fcheck-new, and /Zc:throwingNew corresponds to -fno-check-new (the default).

The Visual Studio documentation strongly recommends using /Zc:throwingNew if possible, as turning it off (the default) seriously bloats code and inhibits optimizations.
https://learn.microsoft.com/en-us/cpp/build/reference/zc-throwingnew-assume-operator-new-throws?view=msvc-170

As mentioned in JDK-8305590, the standard says that an allocation function can report allocation failure either by returning null (when it must have a nothrow exception specification), or by throwing std::bad_alloc (so obviously must not be declared as non-throwing). HotSpot allocation functions terminate the program instead of throwing on allocation failure, so similarly don't need the result checked for null.

The documentation for /Zc:throwingNew is somewhat vague and confusing, so some investigation is probably needed to verify it really has the desired effect for us.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8306579: Consider building with /Zc:throwingNew (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22039/head:pull/22039
$ git checkout pull/22039

Update a local copy of the PR:
$ git checkout pull/22039
$ git pull https://git.openjdk.org/jdk.git pull/22039/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 22039

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22039.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 12, 2024

👋 Welcome back jwaters! 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
Copy link

openjdk bot commented Nov 12, 2024

@TheShermanTanker 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:

8306579: Consider building with /Zc:throwingNew

Reviewed-by: ihse, kbarrett

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 570 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 changed the title 8306579 8306579: Consider building with /Zc:throwingNew Nov 12, 2024
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 12, 2024
@openjdk
Copy link

openjdk bot commented Nov 12, 2024

@TheShermanTanker The following labels will be automatically applied to this pull request:

  • build
  • client

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

@openjdk openjdk bot added build build-dev@openjdk.org client client-libs-dev@openjdk.org labels Nov 12, 2024
@mlbridge
Copy link

mlbridge bot commented Nov 12, 2024

Webrevs

@TheShermanTanker
Copy link
Contributor Author

I don't plan on integrating this yet. My intention is to let it run in Actions while I also test it locally. This is also dependent on some cleanup that has to be done in ADLC, where an operator new is currently causing issues with experimental enabling of C++17

return safe_Malloc(size);
}
#endif

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see you commenting on this which is a "huge" deal as it seems like it changes memory allocation for a lot of the AWT Windows code.
This needs careful and analysis and explanation - from you - so reviewers can ponder it.
Also you need to run a lot of tests to verify it.

Copy link

@kimbarrett kimbarrett Nov 17, 2024

Choose a reason for hiding this comment

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

This is a "replacement function" for global operator new. As the comment
says, it exists to provide the semantics specified by the standard.
Specifically, throwing std::bad_alloc when the allocation fails, because old
versions of the MSVC-provided default implementation didn't do that. That's no
longer true; the default implementation has thrown on allocation failure for a
long time (at least since VS 2015).

https://learn.microsoft.com/en-us/cpp/cpp/new-and-delete-operators?view=msvc-170
https://learn.microsoft.com/en-us/cpp/cpp/new-and-delete-operators?view=msvc-140

VS documentation discusses replacing that behavior by linking in non-throwing
operator new, but we're not doing that.
https://learn.microsoft.com/en-us/cpp/c-runtime-library/link-options?view=msvc-170
see nothrownew.obj

This was also never a correct implementation, since there isn't a
corresponding replacement for operator delete. This implementation just
calls malloc and checks the result. Calling the default operator delete on
the result of malloc (or anything else that isn't the result of calling the
default operator new) is UB; C++14 3.7.4.2/3. Presumably it's been working,
but that's presumably by accident of the MSVC implementation.

The effect of removing this definition is that the default operator new will
be used instead. Doing that instead of calling malloc is potentially somewhat
of a change of behavior. Whether it matters is hard for me to guess.

Either this replacement definiton should be removed, or a corresponding
operator delete must be added.

Also, can user code be linked into the application using this? If so, it is
generally wrong for a library to provide a replacement definition; the
application might be providing its own.

Choose a reason for hiding this comment

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

There also isn't a corresponding operator new[]. Because of that, the
various places that are allocating arrays are already using the default array
allocation function, e.g. the C++ allocator, rather than directly using
malloc. That also argues for the removal proposed here.

Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

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

Looks good.

@openjdk
Copy link

openjdk bot commented Nov 17, 2024

@TheShermanTanker this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout patch-15
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Nov 17, 2024
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Nov 18, 2024
@TheShermanTanker
Copy link
Contributor Author

One thing I have a query on: Does this need the problem with the ADLC operator new to be fixed before it can be integrated?

@kimbarrett
Copy link

One thing I have a query on: Does this need the problem with the ADLC operator new to be fixed before it can be integrated?

For those following along, the bug being referred to is this:
https://bugs.openjdk.org/browse/JDK-8342639
Global operator new in adlc has wrong exception spec

I think that's not a blocker for this change. With this change, new
expressions that call that function won't implicitly check for null. That
would be a problem if that function could return null. But it calls the normal
global operator new(size_t), which doesn't return null, instead throwing on
failure. And if that call failed, that throw would hit the nothrow exception
specification of the adlc-supplied definition, and call std::terminate. I'm
pretty sure that's where it would end up anyway, in the absence of the nothrow
exception specification, since I don't think there's any exception handling in
adlc.

@TheShermanTanker
Copy link
Contributor Author

TheShermanTanker commented Nov 21, 2024

One thing I have a query on: Does this need the problem with the ADLC operator new to be fixed before it can be integrated?

For those following along, the bug being referred to is this: https://bugs.openjdk.org/browse/JDK-8342639 Global operator new in adlc has wrong exception spec

I think that's not a blocker for this change. With this change, new expressions that call that function won't implicitly check for null. That would be a problem if that function could return null. But it calls the normal global operator new(size_t), which doesn't return null, instead throwing on failure. And if that call failed, that throw would hit the nothrow exception specification of the adlc-supplied definition, and call std::terminate. I'm pretty sure that's where it would end up anyway, in the absence of the nothrow exception specification, since I don't think there's any exception handling in adlc.

Ah, alright. For exceptions ADLC is a strange case. ADLC has no exceptions on Linux, but does on every other platform. I couldn't tell you why this is the case

EDIT: Nevermind, I think I misunderstood "I don't think there's any exception handling in adlc", my bad

@kimbarrett
Copy link

kimbarrett commented Nov 21, 2024

One thing I have a query on: Does this need the problem with the ADLC operator new to be fixed before it can be integrated?

For those following along, the bug being referred to is this: https://bugs.openjdk.org/browse/JDK-8342639 Global operator new in adlc has wrong exception spec
I think that's not a blocker for this change. [...[

Ah, alright. For exceptions ADLC is a strange case. ADLC has no exceptions on Linux, but does on every other platform. I couldn't tell you why this is the case

EDIT: Nevermind, I think I misunderstood "I don't think there's any exception handling in adlc", my bad

Right, there's no uses of try/catch or anything related.

I thought adlc had exceptions enabled, but you say not on linux, only on
everything else? (That's weird. It also would seriously uglify any exception
handling.) What does the "throwing" default global operator new do when
exceptions are disabled and allocation fails? I hope it terminates, rather
than returning null.

But I'm pretty sure it doesn't matter, because I think that function
definition is unused anywhere other than (possibly) Windows, and probably not
even there. (See discussion in the bug.)

Copy link
Member

@magicus magicus left a comment

Choose a reason for hiding this comment

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

Build changes look fine. I also believe the AWT change is sound, based on Kim's reasoning.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 2, 2024
@bridgekeeper
Copy link

bridgekeeper bot commented Dec 30, 2024

@TheShermanTanker This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@TheShermanTanker
Copy link
Contributor Author

Keep open please

@magicus
Copy link
Member

magicus commented Jan 13, 2025

@TheShermanTanker What is holding you back from integrating? I think you are good to go at this point.

@TheShermanTanker
Copy link
Contributor Author

Sorry, was bogged down on my end. I'm free enough to integrate this at the moment, so I'll do that

@TheShermanTanker
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jan 13, 2025

Going to push as commit a289bcf.
Since your change was applied there have been 771 commits pushed to the master branch:

  • cede304: 8347482: Remove unused field in ParkEvent
  • fa5ff82: 8342062: Reformat keytool and jarsigner output for keys with a named parameter set
  • cc19897: 8293123: Fix various include file ordering
  • 6e43f48: 8346929: runtime/ClassUnload/DictionaryDependsTest.java fails with "Test failed: should be unloaded"
  • c885e59: 8346377: Properly support static builds for Windows
  • 0612636: 8347373: HTTP/2 flow control checks may count unprocessed data twice
  • 450636a: 8347274: Gatherers.mapConcurrent exhibits undesired behavior under variable delays, interruption, and finishing
  • 82e2a79: 8347006: LoadRangeNode floats above array guard in arraycopy intrinsic
  • 85ed78c: 8345185: Update jpackage to not include service bindings by default
  • 3b9732e: 8345471: Clean up compiler/intrinsics/sha/cli tests
  • ... and 761 more: https://git.openjdk.org/jdk/compare/a47d9ba98a1498425970613415ecb830f805a3be...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 13, 2025
@openjdk openjdk bot closed this Jan 13, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jan 13, 2025
@openjdk
Copy link

openjdk bot commented Jan 13, 2025

@TheShermanTanker Pushed as commit a289bcf.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@TheShermanTanker TheShermanTanker deleted the patch-15 branch January 13, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build build-dev@openjdk.org client client-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants