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

Terminate immediately when allocation fails #3193

Merged
merged 4 commits into from
May 1, 2018

Conversation

str4d
Copy link
Contributor

@str4d str4d commented Apr 19, 2018

Backport of bitcoin/bitcoin#9856

Closes #1498.

@str4d str4d added C-upstream-port Category: Changes that are ported from the Bitcoin Core codebase. memory management I-error-handling Problems and improvements related to error handling community database corruption labels Apr 19, 2018
@str4d str4d added this to the v1.1.1 milestone Apr 19, 2018
@str4d str4d added this to Implemented — Waiting for ACKs in Arborist Team Apr 19, 2018
daira
daira previously requested changes Apr 21, 2018
src/prevector.h Outdated
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
assert(_union.indirect);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would prefer:

if (!_union.indirect) { new_handler_terminate(); }

(and change the comment appropriately).

Copy link
Contributor

Choose a reason for hiding this comment

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

... except that new_handler_terminate is static in init.cpp, but that can easily be fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not particularly keen on this, because currently prevector.h has no dependency on any other local repository source file. Looking at upstream, the only local include they have introduced is on compat.h, which is a helper include that has no other local dependencies. If we wanted to re-use new_handler_terminate we'd at a minimum need to include util.h, which then means depending on libbitcoin_util.a anywhere prevector is used. That appears to be the case currently though...

Copy link
Contributor

@daira daira Apr 23, 2018

Choose a reason for hiding this comment

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

I don't see any problem with moving new_handler_terminate into util.cpp/h.

src/prevector.h Outdated
_union.capacity = new_capacity;
} else {
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
assert(new_indirect);
Copy link
Contributor

Choose a reason for hiding this comment

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

if (!new_indirect) { new_handler_terminate(); }

src/init.cpp Outdated
// immediately to (try to) avoid chain corruption.
// Since LogPrintf may itself allocate memory, set the handler directly
// to terminate first.
std::set_new_handler(std::terminate);
Copy link
Contributor

@daira daira Apr 21, 2018

Choose a reason for hiding this comment

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

Take @kallewoof's suggestion to do

fputs(stderr, "Error: Out of memory. Terminating.\n");

here (just before the LogPrintf).

@str4d str4d dismissed daira’s stale review April 26, 2018 10:32

Addressed comments.

Copy link
Contributor

@mdr0id mdr0id left a comment

Choose a reason for hiding this comment

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

Tested ACK:

Sanity checked calling sequence via

char* new_indirect  = NULL;
➜  src git:(b9856-badalloc-terminate) ✗ ./zcashd                
Error: Out of memory. Terminating.
terminate called without an active exception
[1]    60500 abort (core dumped)  ./zcashd

Otherwise performs as expected with suggested implementation

@bitcartel
Copy link
Contributor

ACK. Ran same test as above. Results:

Error: Out of memory. Terminating.
terminate called without an active exception
Aborted (core dumped)

@bitcartel
Copy link
Contributor

@zkbot r+

@zkbot
Copy link
Contributor

zkbot commented May 1, 2018

📌 Commit aeb089e has been approved by bitcartel

@zkbot
Copy link
Contributor

zkbot commented May 1, 2018

⌛ Testing commit aeb089e with merge 1fd3207b9e71e56de641cc4d48d8a794726162b4...

@zkbot
Copy link
Contributor

zkbot commented May 1, 2018

💔 Test failed - pr-merge

@str4d
Copy link
Contributor Author

str4d commented May 1, 2018 via email

@zkbot
Copy link
Contributor

zkbot commented May 1, 2018

⌛ Testing commit aeb089e with merge 23f8b30...

zkbot added a commit that referenced this pull request May 1, 2018
Terminate immediately when allocation fails

Backport of bitcoin/bitcoin#9856

Closes #1498.
@str4d str4d removed the request for review from arcalinea May 1, 2018 12:09
@zkbot
Copy link
Contributor

zkbot commented May 1, 2018

☀️ Test successful - pr-merge
Approved by: bitcartel
Pushing 23f8b30 to master...

Arborist Team automation moved this from In Review to Released (Merged in Master) May 1, 2018
@zkbot zkbot merged commit aeb089e into zcash:master May 1, 2018
@zkbot zkbot mentioned this pull request May 1, 2018
@str4d str4d deleted the b9856-badalloc-terminate branch December 17, 2020 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-upstream-port Category: Changes that are ported from the Bitcoin Core codebase. community database corruption I-error-handling Problems and improvements related to error handling memory management
Projects
Arborist Team
  
Released (Merged in Master)
Development

Successfully merging this pull request may close these issues.

None yet

6 participants