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

buffer: fix buffer alignment restriction #5752

Closed

Conversation

ofrobots
Copy link
Contributor

Pull Request check-list

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to CONTRIBUTING.md?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?

Affected core subsystem(s)

buffer

Description of change

@addaleax recently reported that recent phantom weakness changes (ebbbc5a) to buffer break node-ffi and possibly other modules that use create unaligned buffers.

It turns out there is a simpler solution possible that doesn't introduce an alignment restriction. This also eliminates the need for Node-core to reserve the first internal field on buffers.

Ref: #5204
R=@bnoordhuis, @trevnorris

Marking with dont-land-on-* as the pre-requisite change is also marked likewise.

Recent phantom weakness API changes to buffer, ebbbc5a, ending up
introducing an alignment restriction on the native buffer pointers.
It turns out that there are uses in the modules ecosystem that rely
on the ability to create buffers with unaligned pointers (e.g.
node-ffi).

It turns out there is a simpler solution possible here. As a side
effect this also removes the need to have to reserve the first
internal field on buffers.
@ofrobots ofrobots added buffer Issues and PRs related to the buffer subsystem. v8 engine Issues and PRs related to the V8 dependency. dont-land-on-v5.x labels Mar 17, 2016
ofrobots referenced this pull request Mar 17, 2016
Old style SetWeak is now deprecated, and weakness now works like
phantom references. This means we no longer have a reference to the
object in the weak callback. We use a kInternalFields style weak
callback which provides us with the contents of 2 internal fields
where we can squirrel away the native buffer pointer.

We can no longer neuter the buffer in the weak callback, but that
should be unnecessary as the object is going to be GC'd during the
current gc cycle.

PR-URL: #5204
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: indutny - Fedor Indutny <fedor.indutny@gmail.com>
@addaleax
Copy link
Member

@ofrobots If you don’t happen to be working on it – mind if I try and write a test for this?

@jasnell
Copy link
Member

jasnell commented Mar 17, 2016

I understand it may be difficult, but is there a way to add a test on this?
(@addaleax.. jinx!)

@addaleax
Copy link
Member

I guess test/addons/buffer-free-callback would be a good place for that? One could have something like binding.alloc(size, alignment, offset) which returns a Buffer based on a pointer that’s offset bytes from a alignment boundary and try that for a few combinations.

@ofrobots
Copy link
Contributor Author

@addaleax @jasnell Added a test already 😄.

@addaleax
Copy link
Member

@ofrobots Your test doesn’t check that the alignment can be off, only the size. The buf constant in the binding.cc is always aligned the same.

@addaleax
Copy link
Member

(Not that testing the size too would be a bad idea or anything!)

@ofrobots
Copy link
Contributor Author

@addaleax you're right. I will fix tomorrow, or if you're inclined, I would be happy if you want to contribute the test.

@addaleax
Copy link
Member

sure, shouldn’t take too long :)

@addaleax
Copy link
Member

What do you think of addaleax/node@63649a5a59fec3cd8ce?

@ofrobots ofrobots force-pushed the fix-buffer-alignment-restriction branch 2 times, most recently from a74951a to ecb3a97 Compare March 17, 2016 04:13
@ofrobots
Copy link
Contributor Author

@addaleax Thanks for the test! I have added your commit to this PR (with a slightly modified commit message.)

@addaleax
Copy link
Member

@ofrobots Sure – I assumed everything here ends up in a single commit anyway, but I guess you can assess that better than I do 😄

@ofrobots
Copy link
Contributor Author

@addaleax These commits don't need to be squashed because they can pass testing individually. If this PR lands, the test commit will be correctly attributed as being your contribution. Thanks again for contributing the test, and for raising the issue!

@addaleax
Copy link
Member

@ofrobots Ah, that makes sense then. Good to know, I’ll think of that next time!

@@ -13,9 +13,16 @@ static void FreeCallback(char* data, void* hint) {
void Alloc(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
alive++;

uintptr_t wantAlignment = args[1]->IntegerValue();
uintptr_t wantOffset = args[2]->IntegerValue();
Copy link
Member

Choose a reason for hiding this comment

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

Style nits: don't align the RHS and use snake_case for locals.

Copy link
Member

Choose a reason for hiding this comment

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

Also, maybe drop the want prefix, it suggests the variable is a boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@ofrobots ofrobots force-pushed the fix-buffer-alignment-restriction branch from ecb3a97 to fc24288 Compare March 18, 2016 13:12
Buffers instances can have arbitrary alignment. `node-ffi` depends on
this. Add some regression tests to ensure we don't break this in the
future.

PR-URL: nodejs#5752
@ofrobots ofrobots force-pushed the fix-buffer-alignment-restriction branch from fc24288 to 662dad4 Compare March 18, 2016 13:14
@ofrobots
Copy link
Contributor Author

@trevnorris
Copy link
Contributor

LGTM

1 similar comment
@bnoordhuis
Copy link
Member

LGTM

ofrobots added a commit that referenced this pull request Mar 19, 2016
Recent phantom weakness API changes to buffer, ebbbc5a, ending up
introducing an alignment restriction on the native buffer pointers.
It turns out that there are uses in the modules ecosystem that rely
on the ability to create buffers with unaligned pointers (e.g.
node-ffi).

It turns out there is a simpler solution possible here. As a side
effect this also removes the need to have to reserve the first
internal field on buffers.

PR-URL: #5752
Reviewed-By: trevnorris - Trevor Norris <trev.norris@gmail.com>
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
ofrobots pushed a commit that referenced this pull request Mar 19, 2016
Buffers instances can have arbitrary alignment. `node-ffi` depends on
this. Add some regression tests to ensure we don't break this in the
future.

PR-URL: #5752
Reviewed-By: trevnorris - Trevor Norris <trev.norris@gmail.com>
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
@ofrobots
Copy link
Contributor Author

Thanks. Landed as 73fc440, be97db9.

@ofrobots ofrobots closed this Mar 19, 2016
@ofrobots ofrobots deleted the fix-buffer-alignment-restriction branch March 19, 2016 14:14
@addaleax
Copy link
Member

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants