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

src: return static buffer on malloc(0) #8658

Closed
wants to merge 1 commit into from

Conversation

mscdex
Copy link
Contributor

@mscdex mscdex commented Sep 20, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)
  • src
Description of change

This is an alternate approach to #8572 that reuses a static buffer for malloc(0) requests to restore pre-node v6.6.0 behavior. This requires the addition of a new node::Free() for all buffers created via node::Malloc()/node::Calloc().

Fixes: #8571

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Sep 20, 2016
@mscdex mscdex added crypto Issues and PRs related to the crypto subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. labels Sep 20, 2016
@mscdex
Copy link
Contributor Author

mscdex commented Sep 20, 2016

Copy link
Contributor

@yorkie yorkie left a comment

Choose a reason for hiding this comment

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

LGTM

@bnoordhuis
Copy link
Member

It might put people on the wrong foot because it deviates from what normal malloc does. malloc(0) is allowed to either return nullptr or a unique pointer but not a fixed pointer.

If you think performance is at stake, then it's arguably better to teach callers to handle zero-sized allocations explicitly.

@bnoordhuis
Copy link
Member

Oh, another thing: there are at least one or two places in the source code where we pass a heap-allocated buffer to an external library (openssl), with the library responsible for freeing the memory again. I don't think we normally pass zero-sized buffers but it would fail badly if we did.

@mscdex
Copy link
Contributor Author

mscdex commented Sep 20, 2016

@bnoordhuis Handling the nullptr in node code is fine, but there are some external APIs (e.g. OpenSSL) that actually check the pointer first and may return early for example. That would break compatibility with node v6 pre-v6.6.0, because then we would start returning errors instead of a result.

Allowing a nullptr to be passed to OpenSSL is probably the right thing to do going forward though (albeit in node v7 or later).

// a nullptr.
// nullptr for zero-sized allocation requests. Normalize by always using
// a static buffer.
static void* fake_mem = malloc(1);
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this works as it is? Having fake_mem as a static variable means that every object file is going to end up with its own instance of that, meaning that e.g. the Free() from node_crypto.cc can’t handle Malloc()ed buffers from node_buffer.cc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Does it look better now?

Copy link
Member

Choose a reason for hiding this comment

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

@mscdex Yeah, that should work, but it may come with a bit of overhead and it’s not technically thread-safe.

Something that might be a bit faster would be using a compile-time static buffer, i.e. extern char fake_mem[1]; in util-inl.h and char fake_mem[1]; in util.cc, because that should enable the compiler to inline the memory address (which may become an actual constant).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@mscdex
Copy link
Contributor Author

mscdex commented Sep 20, 2016

CI again after recent changes: https://ci.nodejs.org/job/node-test-pull-request/4150/

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM

@bnoordhuis
Copy link
Member

I'm going to go out on a limb and say this does not LGTM. The potential for subtle bugs outweighs any benefits, IMO.

@jasnell
Copy link
Member

jasnell commented Sep 20, 2016

That's fair. Is this a "don't do this at all" or a "there's a better way" type of thing?

@addaleax
Copy link
Member

I think the “better way” would basically be #8572? I’m reopening that because it seems like a solution that can get to consensus pretty easily, and anything else can be discussed later.

@mscdex mscdex closed this Sep 20, 2016
@mscdex mscdex deleted the crypto-pbkdf2-fix branch December 14, 2016 07:13
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++. crypto Issues and PRs related to the crypto subsystem. 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.

6 participants