ncnn icon indicating copy to clipboard operation
ncnn copied to clipboard

Heap-buffer-overflow in Concat::forward (AVX and generic x86 kernel) with multi-scale YOLO export

Open sergshadrap opened this issue 2 months ago • 5 comments

error log | 日志或报错信息 | ログ

AVX build: ==16564==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x632000045844 WRITE of size 73728 at 0x632000045844 thread T0 #0 ... (libasan) #1 ncnn::Concat_x86_avx::forward(std::vector<ncnn::Mat, std::allocatorncnn::Mat > const&, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const #2 ncnn::NetPrivate::do_forward_layer(...) #3 ncnn::NetPrivate::forward_layer(...) [recurses through the graph] ... #45 ncnn::Extractor::extract(int, ncnn::Mat&, int)

0x632000045844 is located 0 bytes to the right of 86084-byte region [0x632000030800,0x632000045844) allocated by thread T0 here: #0 posix_memalign (libasan) #1 ncnn::PoolAllocator::fastMalloc(unsigned long)

Non-AVX build (rebuilt with -DNCNN_AVX2=OFF -DNCNN_AVX=OFF), identical size/offset: ==4778==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x63200023d844 WRITE of size 73728 at 0x63200023d844 thread T0 #0 ... (libasan) #1 ncnn::Concat_x86::forward(std::vector<ncnn::Mat, std::allocatorncnn::Mat > const&, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const #2 ncnn::NetPrivate::do_forward_layer(...) ... 0x63200023d844 is located 0 bytes to the right of 86084-byte region [0x632000228800,0x63200023d844) allocated by thread T0 here: #0 posix_memalign (libasan) #1 ncnn::PoolAllocator::fastMalloc(unsigned long)

context | 编译/运行环境 | バックグラウンド

ncnn: reproduced on tag 20260526 (commit e54f7b1) and on current master as of Sept 2026 Platform: x86_64, Ubuntu on WSL2 Compiler: gcc, built with -fsanitize=address -O0 -g Reproduced both with default build and with -DNCNN_AVX2=OFF -DNCNN_AVX=OFF (same overflow, same size/offset, in Concat_x86_avx and Concat_x86 respectively)

Model: yolo26n.pt, exported via Ultralytics: yolo export model=yolo26n.pt format=ncnn quantize=16 imgsz=384,224 Input blob: in0, output blob: out0 Input tensor: 384x224 RGB

how to reproduce | 复现步骤 | 再現方法

1 Export a YOLO26n model to ncnn fp16 as above (or use attached .param/.bin). 2 Build a minimal test harness against ncnn with -fsanitize=address, load the model, create one ncnn::Extractor, feed a 384x224 RGB in0, and call ex.extract("out0", out). 3 Run under ASan — crashes on the very first inference call.

more | 其他 | その他

I originally found this while investigating what looked like a steady memory leak across repeated inference calls (free memory dropped every call, never recovered even after manually clearing my PoolAllocators). Bisecting by extracting different intermediate blobs eventually pointed at a Concat layer, and ASan confirmed a real heap-buffer-overflow rather than a leak — my best guess is that in normal (non-ASan) runs this usually lands in slack space inside a reused pool allocator block, so it goes unnoticed, but it likely still corrupts adjacent allocator bookkeeping over many calls, which would explain the apparent "leak."

The graph has several Concat layers joining the three YOLO detection scales, which have different anchor counts (1344/336/84) — I suspect the output size calculation for one of these is off for this particular combination of input shapes, but haven't pinned down the exact layer name yet. Happy to attach the .param/.bin files and/or a minimal repro .cpp if that would help narrow it down.

yolo26n_ncnn_model.zip — the exact .param/.bin used in both traces above (fp16 export, 384x224 input, in0/out0 blob names). mpptest.zip — the actual test harness used to find/confirm this. Build with: g++ -O0 -g -fsanitize=address -I/src -I/build/src -o mpptest_asan mpptest.cpp /build/src/libncnn.a -lpthread -fopenmp -lm then run: ./mpptest_asan yolo26n-fp16.param yolo26n-fp16.bin Note: this harness pulls a frame from a live MJPEG stream (fetch_one_frame(), reading the URL from mppnet.conf or defaulting to http://127.0.0.1/mjpeg) — it needs a reachable MJPEG source to get past frame capture before it reaches the crash. If you don't have one handy, the crash itself doesn't depend on real frame content — any 384x224x3 buffer fed into ncnn::Mat::from_pixels_resize and then ex.extract("out0", out) should reproduce it; happy to strip the networking part out into a minimal version if that's easier for you to run. yolo26n_ncnn_model.zip

mpptest.zip

sergshadrap avatar Sep 25 '26 13:09 sergshadrap

Reproduced with the attached model (ASan build of current master, x86_64) - same signature: WRITE of size 73728, 86084-byte region, Concat_x86_avx::forward.

Two separate problems.

  1. The input is transposed relative to the export. imgsz=384,224 is (h, w), and the graph is baked for h=384, w=224: Reshape reshape_191 0=7 1=12 2=128 is torch (128, 12, 7) (pnnx takes ncnn w/h from torch W/H), i.e. H=12 W=7 at stride 32. Feeding Mat(384, 224, 3) gives (7, 12) against the baked (12, 7); the element count is the same, so nothing complains. (Can you confirm the target_width/target_height you pass to from_pixels_resize?)

  2. ncnn then corrupts memory:

  • BinaryOp add_9 receives (w=12 h=7 c=128) + (w=7 h=12 c=128) and broadcasts to (12, 12, 128);
  • cat_7 (Concat, default axis 0) therefore gets inputs whose h differs. The dims == 3 && axis == 0 branch of concat_x86.cpp takes w/h/d from bottom_blobs[0] for the output (12x7x256 = 86016 B) but copies each input with its own bottom_blob.total(): the second input is 73728 B at channel 128 (offset 43008), i.e. 30720 bytes past the end. Same pattern in concat_x86.cpp:674 (non-AVX), the arm/mips/loongarch/riscv copies and concat.cpp:151; no Concat backend validates the non-concat dims.

Feeding Mat(224, 384, 3) (target_width=224, target_height=384) makes the whole graph consistent - all 382 blobs run clean here - so it should also give you correct detections.

I guess your "leak" is the same write landing in pool allocator slack: nothing gets reported, but the bookkeeping is corrupted, ncnn prints FATAL ERROR! pool allocator destroyed too early ... still in use at exit, and the chunks show up as leaked.

Edwardssss avatar Sep 25 '26 15:09 Edwardssss

Thanks! Confirmed — that's exactly the bug. I had target_width=384, target_height=224 — backwards from what the export expects, exactly as you diagnosed from imgsz=384,224 being (h, w). Swapping to target_width=224, target_height=384 fixes it on my end too. Thanks! I didn't run Asan again , but checked on ARM target.

sergshadrap avatar Sep 25 '26 18:09 sergshadrap

Opened a fix for the Concat non-concat-dim overflow path: https://github.com/Tencent/ncnn/pull/7029 — shared check_shape() + arch forwards + regression test for the 12x7x128 vs 7x12x128 case.

ktz03 avatar Sep 29 '26 07:09 ktz03

Thanks! Retested #7029 with the same original transposed input. The Concat overflow no longer reproduces there — good. But it now crashes further downstream with a SEGV in Convolution_x86::forward/convolution_im2col_gemm, instead. Looks like the same bad shape is still propagating past the now-hardened Concat and hitting an unguarded assumption in Convolution's im2col sizing. Full ASan log attached. Let me know if this belongs here or as a separate issue.

After model load free: 5977308 KB (free+buffers: 5977308 KB) | proc heap in-use: 0 KB Warming up... warm-up: after create_extractor free: 5977260 KB (free+buffers: 5977260 KB) | proc heap in-use: 0 KB warm-up: after input free: 5977256 KB (free+buffers: 5977256 KB) | proc heap in-use: 0 KB ASAN:DEADLYSIGNAL

================================================================= ==26393==ERROR: AddressSanitizer: SEGV on unknown address 0x7f778d773000 (pc 0x7f77942f6222 bp 0x61a0000254f0 sp 0x7ffff9eb2040 T0) ==26393==The signal is caused by a READ memory access. #0 0x7f77942f6221 in ncnn::convolution_im2col_gemm(ncnn::Mat const&, ncnn::Mat&, ncnn::Mat const&, ncnn::Mat const&, int, int, int, int, int, int, int, ncnn::Option const&) [clone ._omp_fn.15] (/home/serg/mpptest_asan+0xf6221) #1 0x7f779209dede in GOMP_parallel (/usr/lib/x86_64-linux-gnu/libgomp.so.1+0xdede) #2 0x7f7794339996 in ncnn::Convolution_x86::forward(ncnn::Mat const&, ncnn::Mat&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x139996) #3 0x7f77942926bd in ncnn::NetPrivate::do_forward_layer(ncnn::Layer const*, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x926bd) #4 0x7f7794294aa7 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94aa7) #5 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #6 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #7 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #8 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #9 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #10 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #11 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #12 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #13 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #14 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #15 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #16 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #17 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #18 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #19 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #20 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #21 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #22 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #23 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #24 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #25 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #26 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #27 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #28 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #29 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #30 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #31 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #32 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #33 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #34 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #35 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #36 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #37 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #38 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #39 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #40 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #41 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #42 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #43 0x7f7794294a80 in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocatorncnn::Mat >&, ncnn::Option const&) const (/home/serg/mpptest_asan+0x94a80) #44 0x7f7794294c6c in ncnn::Extractor::extract(int, ncnn::Mat&, int) (/home/serg/mpptest_asan+0x94c6c) #45 0x7f7794270320 in main /home/serg/mpptest.cpp:292 #46 0x7f7791a91bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6) #47 0x7f7794232369 in _start (/home/serg/mpptest_asan+0x32369)

AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV (/home/serg/mpptest_asan+0xf6221) in ncnn::convolution_im2col_gemm(ncnn::Mat const&, ncnn::Mat&, ncnn::Mat const&, ncnn::Mat const&, int, int, int, int, int, int, int, ncnn::Option const&) [clone ._omp_fn.15] ==26393==ABORTING

sergshadrap avatar Sep 29 '26 08:09 sergshadrap

Thanks for retesting with #7029 — good confirmation that the Concat HBOF is gone.

The new SEGV in convolution_im2col_gemm is expected to be a separate failure mode of the same transposed input, not a regression in the Concat guard:

  1. With the wrong imgsz / from_pixels_resize orientation, BinaryOp still broadcasts mismatched spatial shapes (as Edwardssss diagnosed).
  2. Net evaluates producers before Concat. Those poisoned tensors can hit a Convolution before cat_7 runs, so im2col OOBs even though Concat would now return -1 cleanly if reached.
  3. NetPrivate::do_forward_layer already aborts when Concat returns non-zero; the SEGV means Convolution was on the producer path and does not yet validate spatial size vs the baked graph.

So #7029 is doing its job (Concat no longer memory-corrupts). The remaining crash belongs either to fixing the input orientation (your confirmed workaround: target_width=224, target_height=384) or a follow-up issue/PR for Convolution/BinaryOp shape guards — happy to track that separately if you open one with the ASan log.

I would not block #7029 on hardening every downstream layer for deliberately inconsistent input shapes.

ktz03 avatar Sep 29 '26 08:09 ktz03