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

core: normalize malloc, realloc #7564

Closed
wants to merge 2 commits into from
Closed

Conversation

mhdawson
Copy link
Member

@mhdawson mhdawson commented Jul 6, 2016

  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

core

malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests. Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. build Issues and PRs related to build files or the CI. inspector Issues and PRs related to the V8 inspector protocol labels Jul 6, 2016
@mhdawson
Copy link
Member Author

mhdawson commented Jul 6, 2016

FYI those involved in the original discussion @bnoordhuis, @jasnell, @ChALkeR

@bnoordhuis
Copy link
Member

bnoordhuis commented Jul 6, 2016

Bleh, I hate this let's-add-more-macros approach. If you want to eradicate malloc(0), move everything over to new / new (std::nothrow). That avoids platform-specific macro magic and it cleans up our inconsistent memory management.

@mscdex mscdex added lib / src Issues and PRs related to general changes in the lib or src directory. and removed build Issues and PRs related to build files or the CI. inspector Issues and PRs related to the V8 inspector protocol labels Jul 6, 2016
@jasnell
Copy link
Member

jasnell commented Jul 6, 2016

LGTM with green CI but would prefer @bnoordhuis to be happy with it also.

@bnoordhuis
Copy link
Member

I'll work on moving everything over to new[] / delete[].

@mhdawson
Copy link
Member Author

mhdawson commented Jul 8, 2016

@bnoordhuis is that something you are working now ? Just want to get a feel for whether its the short term fix or not.

@bnoordhuis
Copy link
Member

Yes.

@mhdawson
Copy link
Member Author

mhdawson commented Aug 2, 2016

Just wondering how this is going, want to be sure we have a fix in before LTS is cut for 6.x (still a ways off I know).

@bnoordhuis
Copy link
Member

I did some work on it already (and most of that has landed) but fully moving over to new/delete isn't possible short term.

In some places, notably the node::Buffer API, we leak the implementation detail that memory is managed with malloc/free. Since mixing C-style and C++-style dynamic memory is undefined behavior, we're stuck with that for the foreseeable future.

@mhdawson
Copy link
Member Author

mhdawson commented Aug 3, 2016

Ok so does that mean we sill need to land this PR to resolve the current issues on AIX ?

@mhdawson
Copy link
Member Author

mhdawson commented Aug 4, 2016

@bnoordhuis you ok with this change given that the move away from malloc/free is a longer term effort or do you have some alternative suggestion ?

@bnoordhuis
Copy link
Member

I want to take a slightly different approach. I have some in-progress work that I can PR next week.

@mhdawson
Copy link
Member Author

mhdawson commented Aug 5, 2016

Ok, thanks.

@mhdawson
Copy link
Member Author

Any update ?

mhdawson added a commit to mhdawson/io.js that referenced this pull request Aug 11, 2016
We now have adequate AIX hardware to add AIX to
the regular regression runs.

However, there are a couple of failing tests even
though AIX was green at one point.  This PR
marks those tests as flaky so that we can add AIX
so that we can spot any new regressions without making
the builds RED

The tests are being worked under the following PRs

- being worked under nodejs#7564
test-async-wrap-post-did-throw
test-async-wrap-throw-from-callback
test-crypto-random

- being worked under nodejs#7973
test-stdio-closed

- covered by nodejs#3796
test-debug-signal-cluster
mhdawson added a commit that referenced this pull request Aug 11, 2016
We now have adequate AIX hardware to add AIX to
the regular regression runs.

However, there are a couple of failing tests even
though AIX was green at one point.  This PR
marks those tests as flaky so that we can add AIX
so that we can spot any new regressions without making
the builds RED

The tests are being worked under the following PRs

- being worked under #7564
test-async-wrap-post-did-throw
test-async-wrap-throw-from-callback
test-crypto-random

- being worked under #7973
test-stdio-closed

- covered by #3796
test-debug-signal-cluster

PR-URL: #8065
Reviewed-By: joaocgreis - João Reis <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
cjihrig pushed a commit that referenced this pull request Aug 11, 2016
We now have adequate AIX hardware to add AIX to
the regular regression runs.

However, there are a couple of failing tests even
though AIX was green at one point.  This PR
marks those tests as flaky so that we can add AIX
so that we can spot any new regressions without making
the builds RED

The tests are being worked under the following PRs

- being worked under #7564
test-async-wrap-post-did-throw
test-async-wrap-throw-from-callback
test-crypto-random

- being worked under #7973
test-stdio-closed

- covered by #3796
test-debug-signal-cluster

PR-URL: #8065
Reviewed-By: joaocgreis - João Reis <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@mhdawson
Copy link
Member Author

@bnoordhuis how close are you to a fix. At this point I think we either need it this week or we land the original PR and then revert once you have a better fix.

Trott added a commit to Trott/io.js that referenced this pull request Aug 31, 2016
`malloc(0)` may return NULL on some platforms. Do not report
out-of-memory error unless `malloc` was passed a number greater than
`0`.

Fixes: nodejs#7564
PR-URL: nodejs#8352
@mhdawson
Copy link
Member Author

mhdawson commented Sep 2, 2016

@Trott do you prefer to land your PR or that I land your commit as part of this one ? The later is probably easier for me but its up to you.

@Trott
Copy link
Member

Trott commented Sep 2, 2016

@mhdawson No preference, so GO FOR IT! (And ping me if you change your mind and want me to do it for whatever reason.)

@addaleax
Copy link
Member

addaleax commented Sep 2, 2016

LGTM, maybe use “src:” as the subsystem prefix for the first commit?

@addaleax
Copy link
Member

addaleax commented Sep 2, 2016

Also, I don’t want to step on @bnoordhuis’ toes or anything, but I like the general idea here… doing all memory management through node:: methods seems nice to me, it seems like it would make it easier to do things like call isolate->LowMemoryNotification() and retry in case malloc() fails?

@mhdawson
Copy link
Member Author

mhdawson commented Sep 2, 2016

I don't want to land this today since its probably one that should get is 72hrs and am out on Monday for the Canadian long weekend. I'll plan to land on Tuesday when I'm back.

mhdawson added a commit that referenced this pull request Sep 6, 2016
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests.  Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

Fixes: #7549
PR-URL: #7564
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@mhdawson
Copy link
Member Author

mhdawson commented Sep 6, 2016

landed as a00ccb0

@mhdawson mhdawson closed this Sep 6, 2016
@Fishrock123 Fishrock123 mentioned this pull request Sep 6, 2016
@Fishrock123
Copy link
Contributor

@mhdawson if you want this on v6.x you'll need to backport unfortunately. I segfaulted when I tried.

@addaleax
Copy link
Member

addaleax commented Sep 9, 2016

@Fishrock123 This needs to land together with ed640ae, master was broken for one commit there. :/ If you experienced problems even with that included, we’re probably in trouble and, while I know that you’re busy, it might be really good to have some debugging info if you can provide any.

@Fishrock123
Copy link
Contributor

Hmmm, ok will retry.

@Fishrock123
Copy link
Contributor

Looks good with both commits. Thanks!

Fishrock123 pushed a commit that referenced this pull request Sep 9, 2016
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests.  Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

Fixes: #7549
PR-URL: #7564
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>

 Conflicts:
	src/node.cc
Fishrock123 pushed a commit that referenced this pull request Sep 9, 2016
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests.  Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

Fixes: #7549
PR-URL: #7564
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>

 Conflicts:
	src/node.cc
@mhdawson
Copy link
Member Author

mhdawson commented Sep 9, 2016

Great to hear :)

@MylesBorins
Copy link
Contributor

@mhdawson should this be backported to v4?

@addaleax
Copy link
Member

addaleax commented Oct 7, 2016

I don’t think this has to be backported, but if it is, it needs to be backported together with #8572 (and maybe, but not necessarily, #8482).

@mhdawson
Copy link
Member Author

mhdawson commented Oct 7, 2016

I'm going to say no. We did not see issues in V4.x on AIX as they were introduced with node-inspector (as a side effect) and while this addresses the problem more generally I'd leave 4.X as is until we run into a similar issue in which case we can re-evaluate.

addaleax pushed a commit to addaleax/node that referenced this pull request Nov 22, 2016
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests.  Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

Fixes: nodejs#7549
PR-URL: nodejs#7564
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
MylesBorins pushed a commit that referenced this pull request Nov 22, 2016
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests.  Normalize by always using
a nullptr.

- Introduce node::malloc, node::realloc and node::calloc that should
  be used throught our source.
- Update all existing node source files to use the new functions
  instead of the native allocation functions.

Fixes: #7549
PR-URL: #7564
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Nov 22, 2016
@mhdawson mhdawson deleted the aix3 branch March 15, 2017 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants