Skip to content

Commit

Permalink
test: add buffer alignment regression tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and ofrobots committed Mar 19, 2016
1 parent 73fc440 commit be97db9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 8 additions & 1 deletion test/addons/buffer-free-callback/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 alignment = args[1]->IntegerValue();
uintptr_t offset = args[2]->IntegerValue();

uintptr_t static_offset = reinterpret_cast<uintptr_t>(buf) % alignment;
char* aligned = buf + (alignment - static_offset) + offset;

args.GetReturnValue().Set(node::Buffer::New(
isolate,
buf,
aligned,
args[0]->IntegerValue(),
FreeCallback,
nullptr).ToLocalChecked());
Expand Down
21 changes: 17 additions & 4 deletions test/addons/buffer-free-callback/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
require('../../common');
var binding = require('./build/Release/binding');

function check(size) {
var buf = binding.alloc(size);
function check(size, alignment, offset) {
var buf = binding.alloc(size, alignment, offset);
var slice = buf.slice(size >>> 1);

buf = null;
Expand All @@ -16,7 +16,20 @@ function check(size) {
gc();
}

check(64);
check(64, 1, 0);

// Buffers can have weird sizes.
check(97, 1, 0);

// Buffers can be unaligned
check(64, 8, 0);
check(64, 16, 0);
check(64, 8, 1);
check(64, 16, 1);
check(97, 8, 1);
check(97, 16, 1);
check(97, 8, 3);
check(97, 16, 3);

// Empty ArrayBuffer does not allocate data, worth checking
check(0);
check(0, 1, 0);

0 comments on commit be97db9

Please sign in to comment.