Embedding args occasionally get trailing garbage on .NET (missing null terminator on a pooled buffer)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
Hướng nghiên cứu
Bắt đầu tại src/NodeApi/Runtime/Utf8StringArray.cs và tái hiện sự cố bằng ví dụ đầu độc ArrayPool được cung cấp. Thêm một bài kiểm thử hồi quy bao quát các bộ đệm từ pool trên nhánh .NET, sau đó chạy bộ kiểm thử liên quan. Được xem là hoàn tất khi các đối số embedding vẫn được kết thúc bằng null và không có dữ liệu cũ từ pool xuất hiện trong chuỗi tùy chọn native.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Passing Args to NodeEmbeddingPlatformSettings intermittently fails with
bad option: --disable-wasm-trap-handler<random leftover bytes>
The tail is different every time and looks like fragments of unrelated strings from elsewhere in the process (table names, event names, and so on). It works if you create the platform right at startup, but once the app has been running for a while it starts failing, which pointed at the option value picking up stale memory.
Versions: Microsoft.JavaScript.NodeApi 0.9.11 with Microsoft.JavaScript.LibNode 20.1800.215 (Node 20.18.0), on net8.0. Reproduced on both Windows x64 and Linux x64 — it is tied to the TFM, not the OS.
Cause
In src/NodeApi/Runtime/Utf8StringArray.cs:
#if NETFRAMEWORK || NETSTANDARD
_stringBuffer = new byte[byteLength]; // zeroed
#else
_stringBuffer = ArrayPool<byte>.Shared.Rent(byteLength); // NOT zeroed
#endif
...
offset += Encoding.UTF8.GetBytes(
src, strings[i].Length,
(byte*)(stringBufferPtr + offset), byteLength - offset)
+ 1; // +1 for the string Null-terminator.
The + 1 reserves a byte for the terminator, but nothing ever writes the \0. On the netfx/netstandard path this is harmless because new byte[] is zero-initialized, so the reserved byte is already 0. On the .NET path the buffer comes from ArrayPool<byte>.Shared, which returns it with its previous contents intact, so the terminator byte can hold any leftover value. Native node then reads each argv entry as a C string and reads past the end of the intended option into that stale data.
This also explains the timing. On a fresh process the pool is empty, so the first rent returns zeroed memory and everything works. After the app has used ArrayPool<byte>.Shared for anything else (JSON, IO, DB drivers, and so on), the buffer comes back dirty and the option gets a trailing tail.
Repro
The failure can be made deterministic by dirtying the relevant pool bucket first:
using System.Buffers;
using Microsoft.JavaScript.NodeApi.Runtime;
// byteLength for ["node","--disable-wasm-trap-handler"] is (4+1)+(27+1)=33 -> 64-byte bucket
var marker = System.Text.Encoding.ASCII.GetBytes("_LEAKED_MARKER_1234567890");
for (int i = 0; i < 64; i++)
{
var buf = ArrayPool<byte>.Shared.Rent(33);
for (int k = 0; k < buf.Length; k++) buf[k] = marker[k % marker.Length];
ArrayPool<byte>.Shared.Return(buf); // returned without clearing
}
var platform = new NodeEmbeddingPlatform(new NodeEmbeddingPlatformSettings
{
Args = new[] { "node", "--disable-wasm-trap-handler" }
});
produces:
bad option: --disable-wasm-trap-handler_MARKER_1234567890_LEAKED_MARKER
Fix
Write the terminator explicitly instead of relying on the buffer being zeroed:
int written = Encoding.UTF8.GetBytes(
src, strings[i].Length,
(byte*)(stringBufferPtr + offset), byteLength - offset);
((byte*)stringBufferPtr)[offset + written] = 0;
offset += written + 1;
Clearing the rented buffer would also work, but writing the terminator is cheaper. The pointer array (ArrayPool<nint>.Shared.Rent) is not affected, since it is passed with an explicit count.
This is hard to diagnose because it passes in isolation and only surfaces under load or after warm-up. I can open a PR with the terminator fix and a test that poisons the pool if that would help.
- Ngôn ngữ chính
- C#
- Star
- 783
- Fork
- 80
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/node-api-dotnet
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/node-api-dotnet#484 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
microsoft/node-api-dotnet#486 · 1 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
microsoft/node-api-dotnet#483 · 1 bình luận · 1 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
microsoft/node-api-dotnet#479 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
microsoft/node-api-dotnet#475 · 1 bình luận ·
Tất cả issue của microsoft/node-api-dotnet
Issue tương tự
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
sillsdev/languageforge-lexbox#2665 ·
-
bug documentation frontend
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
azurenoops/spin_agent#975 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
SubtitleEdit/subtitleedit#15108 · 1 bình luận ·