Skip to content

Commit 0483aa0

Browse files
r-Larchclaude
andauthored
Null-terminate PooledBuffer and Utf8StringArray UTF-8 strings (#510)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a16ae44 commit 0483aa0

5 files changed

Lines changed: 132 additions & 5 deletions

File tree

‎src/NodeApi/Runtime/NodejsRuntime.Embedding.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public override NodeEmbeddingStatus EmbeddingRuntimeConfigAddModule(
681681
node_embedding_data_release_callback release_init_module_data,
682682
int module_node_api_version)
683683
{
684-
PooledBuffer moduleNameBuffer = PooledBuffer.FromSpanUtf8(module_name);
684+
using PooledBuffer moduleNameBuffer = PooledBuffer.FromSpanUtf8(module_name);
685685
fixed (byte* moduleNamePtr = moduleNameBuffer)
686686
return Import(ref node_embedding_runtime_config_add_module)(
687687
runtime_config,

‎src/NodeApi/Runtime/PooledBuffer.cs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,42 @@ public void Dispose()
6868
[EditorBrowsable(EditorBrowsableState.Never)]
6969
public readonly ref byte GetPinnableReference() => ref Span.GetPinnableReference();
7070

71+
// Native callers may read these buffers as null-terminated C strings. A null string
72+
// produces an empty buffer (null pointer); an empty string produces just the terminator.
7173
public static unsafe PooledBuffer FromStringUtf8(string? value)
7274
{
73-
if (string.IsNullOrEmpty(value))
75+
if (value == null)
7476
{
7577
return Empty;
7678
}
7779

7880
int byteLength = Encoding.UTF8.GetByteCount(value);
7981
PooledBuffer buffer = new(byteLength, byteLength + 1);
80-
Encoding.UTF8.GetBytes(value, 0, value!.Length, buffer.Buffer, 0);
82+
Encoding.UTF8.GetBytes(value, 0, value.Length, buffer.Buffer, 0);
8183

84+
// Pooled arrays are not zeroed.
85+
buffer.Buffer[byteLength] = 0;
8286
return buffer;
8387
}
8488

8589
public static unsafe PooledBuffer FromSpanUtf8(ReadOnlySpan<char> value)
8690
{
8791
if (value.IsEmpty)
8892
{
89-
return Empty;
93+
PooledBuffer emptyString = new(0, 1);
94+
emptyString.Buffer[0] = 0;
95+
return emptyString;
9096
}
9197

9298
fixed (char* valuePtr = value)
9399
{
94100
int byteLength = Encoding.UTF8.GetByteCount(valuePtr, value.Length);
95101
PooledBuffer buffer = new(byteLength, byteLength + 1);
96102
fixed (byte* bufferPtr = buffer.Span)
97-
Encoding.UTF8.GetBytes(valuePtr, value.Length, bufferPtr, byteLength + 1);
103+
Encoding.UTF8.GetBytes(valuePtr, value.Length, bufferPtr, byteLength);
104+
105+
// Pooled arrays are not zeroed.
106+
buffer.Buffer[byteLength] = 0;
98107
return buffer;
99108
}
100109
}

‎test/NodejsEmbeddingTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,34 @@ public void ErrorPropagation()
260260
$"at {typeof(NodejsEmbeddingTests).FullName}.", StringComparison.Ordinal));
261261
}
262262

263+
[Theory]
264+
[InlineData("x")]
265+
[InlineData("")]
266+
public void ThrowErrorFromCallback(string message)
267+
{
268+
using NodeEmbeddingThreadRuntime nodejs = CreateNodeEmbeddingThreadRuntime();
269+
270+
nodejs.Run(() =>
271+
{
272+
JSValue.Global.SetProperty("throwError", JSValue.CreateFunction("throwError", (args) =>
273+
{
274+
// Rented buffers that are not zeroed must not leak into the error message.
275+
FillArrayPoolWithGarbage();
276+
JSError.ThrowError((string)args[0]);
277+
return JSValue.Undefined;
278+
}));
279+
JSValue.Global.SetProperty("message", message);
280+
281+
JSValue result = JSValue.RunScript("""
282+
(() => {
283+
try { throwError(message); } catch (e) { return e.message; }
284+
return 'not thrown';
285+
})()
286+
""");
287+
Assert.Equal(message, (string)result);
288+
});
289+
}
290+
263291
[Fact]
264292
public async Task WorkerIsMainThread()
265293
{

‎test/PooledBufferTests.cs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma warning disable CA1822 // Mark members as static
5+
6+
using System;
7+
using System.Text;
8+
using Microsoft.JavaScript.NodeApi.Runtime;
9+
using Xunit;
10+
using static Microsoft.JavaScript.NodeApi.Test.TestUtils;
11+
12+
namespace Microsoft.JavaScript.NodeApi.Test;
13+
14+
public class PooledBufferTests
15+
{
16+
public static TheoryData<string> Strings { get; } = new()
17+
{
18+
"",
19+
"x",
20+
"module-name",
21+
"\u00e4\u00f6\u00fc \u4e2d\u6587 \U0001F600",
22+
};
23+
24+
[Theory]
25+
[MemberData(nameof(Strings))]
26+
public void FromStringUtf8IsNullTerminated(string value)
27+
{
28+
FillArrayPoolWithGarbage();
29+
using PooledBuffer buffer = PooledBuffer.FromStringUtf8(value);
30+
AssertNullTerminatedUtf8(value, buffer);
31+
}
32+
33+
[Theory]
34+
[MemberData(nameof(Strings))]
35+
public void FromSpanUtf8IsNullTerminated(string value)
36+
{
37+
FillArrayPoolWithGarbage();
38+
using PooledBuffer buffer = PooledBuffer.FromSpanUtf8(value.AsSpan());
39+
AssertNullTerminatedUtf8(value, buffer);
40+
}
41+
42+
[Fact]
43+
public void FromStringUtf8NullIsEmpty()
44+
{
45+
// A null string must still produce a null pointer when pinned.
46+
using PooledBuffer buffer = PooledBuffer.FromStringUtf8(null);
47+
Assert.Equal(0, buffer.Length);
48+
Assert.True(buffer.Span.IsEmpty);
49+
}
50+
51+
private static void AssertNullTerminatedUtf8(string expected, PooledBuffer buffer)
52+
{
53+
int byteLength = Encoding.UTF8.GetByteCount(expected);
54+
Assert.Equal(byteLength, buffer.Length);
55+
56+
// The buffer must hold the terminator so that pinning it never yields a null pointer.
57+
Assert.True(buffer.Span.Length > byteLength);
58+
Assert.Equal(0, buffer.Span[byteLength]);
59+
Assert.Equal(expected, Encoding.UTF8.GetString(buffer.Buffer, 0, byteLength));
60+
}
61+
}

‎test/TestUtils.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
#if !NETFRAMEWORK
6+
using System.Buffers;
7+
#endif
8+
using System.Collections.Generic;
59
using System.Diagnostics;
610
using System.IO;
711
using System.Runtime.InteropServices;
@@ -142,6 +146,31 @@ public static void CopyIfNewer(string sourceFilePath, string targetFilePath)
142146
}
143147
}
144148

149+
/// <summary>
150+
/// Fills arrays in the shared byte array pool (as seen by the calling thread) with non-zero
151+
/// bytes, so that code relying on rented buffers being zeroed fails deterministically.
152+
/// </summary>
153+
public static void FillArrayPoolWithGarbage()
154+
{
155+
#if !NETFRAMEWORK
156+
List<byte[]> rented = new();
157+
for (int size = 1; size <= 1024; size *= 2)
158+
{
159+
for (int i = 0; i < 8; i++)
160+
{
161+
byte[] array = ArrayPool<byte>.Shared.Rent(size);
162+
array.AsSpan().Fill(0xAA);
163+
rented.Add(array);
164+
}
165+
}
166+
167+
foreach (byte[] array in rented)
168+
{
169+
ArrayPool<byte>.Shared.Return(array);
170+
}
171+
#endif
172+
}
173+
145174
public static Task RunInThread(Action action)
146175
{
147176
TaskCompletionSource<bool> threadCompletion = new();

0 commit comments

Comments
 (0)