Skip to content

Commit 662dad4

Browse files
addaleaxofrobots
authored andcommitted
test: add buffer alignment regression tests
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
1 parent 3bc8a0b commit 662dad4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

test/addons/buffer-free-callback/binding.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ static void FreeCallback(char* data, void* hint) {
1313
void Alloc(const v8::FunctionCallbackInfo<v8::Value>& args) {
1414
v8::Isolate* isolate = args.GetIsolate();
1515
alive++;
16+
17+
uintptr_t alignment = args[1]->IntegerValue();
18+
uintptr_t offset = args[2]->IntegerValue();
19+
20+
uintptr_t static_offset = reinterpret_cast<uintptr_t>(buf) % alignment;
21+
char* aligned = buf + (alignment - static_offset) + offset;
22+
1623
args.GetReturnValue().Set(node::Buffer::New(
1724
isolate,
18-
buf,
25+
aligned,
1926
args[0]->IntegerValue(),
2027
FreeCallback,
2128
nullptr).ToLocalChecked());

test/addons/buffer-free-callback/test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
require('../../common');
55
var binding = require('./build/Release/binding');
66

7-
function check(size) {
8-
var buf = binding.alloc(size);
7+
function check(size, alignment, offset) {
8+
var buf = binding.alloc(size, alignment, offset);
99
var slice = buf.slice(size >>> 1);
1010

1111
buf = null;
@@ -16,7 +16,20 @@ function check(size) {
1616
gc();
1717
}
1818

19-
check(64);
19+
check(64, 1, 0);
20+
21+
// Buffers can have weird sizes.
22+
check(97, 1, 0);
23+
24+
// Buffers can be unaligned
25+
check(64, 8, 0);
26+
check(64, 16, 0);
27+
check(64, 8, 1);
28+
check(64, 16, 1);
29+
check(97, 8, 1);
30+
check(97, 16, 1);
31+
check(97, 8, 3);
32+
check(97, 16, 3);
2033

2134
// Empty ArrayBuffer does not allocate data, worth checking
22-
check(0);
35+
check(0, 1, 0);

0 commit comments

Comments
 (0)