Skip to content

ggml-cpu : disable PCH and fix CACHE_LINE_SIZE ambiguity - #28882

Merged
ggerganov merged 1 commit into
masterfrom
gg/ggml-cpu-disable-pch
Sep 14, 2026
Merged

ggerganov merged 1 commit into
masterfrom
gg/ggml-cpu-disable-pch

Conversation

@ggerganov

@ggerganov ggerganov commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Overview

fix #28858
cont #28091

Fix heap corruption on macOS arm64 caused by the ggml-cpu precompiled header.

The PCH added in #28091 force-includes ggml-impl.h before ops.h. ggml-impl.h pulls in <new> via <array>/<vector>, which defines __cpp_lib_hardware_interference_size. This makes CACHE_LINE_SIZE in ops.h resolve to std::hardware_destructive_interference_size (256 on Apple Silicon) in the C++ kernels, while the C work-buffer sizing code in ggml-cpu.c always uses the fallback value (64). The mismatch undersizes the rope work buffer, causing a heap-buffer-overflow that corrupts the heap and later crashes in ggml_compute_forward_rope_flt.

This PR disables the ggml-cpu PCH (restoring the natural include order so ops.h is processed before <new>) and removes the std::hardware_destructive_interference_size branch from CACHE_LINE_SIZE so the value is deterministic and include-order independent.

Additional information

Reproduce on master using AddressSanitizer with test-rope:

cmake -B build-asan \
  -DGGML_METAL=OFF \
  -DCMAKE_BUILD_TYPE=Debug \
  -DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer" \
  -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer" \
  -DLLAMA_BUILD_SERVER=OFF \
  -DLLAMA_BUILD_APP=OFF \
  -DLLAMA_BUILD_TESTS=ON \
  -DLLAMA_CURL=OFF

cmake --build build-asan -j $(sysctl -n hw.logicalcpu) --target test-rope

./build-asan/bin/test-rope

Before the fix, test-rope aborts with a heap-buffer-overflow:

==ERROR: AddressSanitizer: heap-buffer-overflow on address ...
WRITE of size 4 ... in rope_yarn ... ops.cpp
#2 in ggml_compute_forward_rope_flt<float> ... ops.cpp
...
0x... is located 0 bytes after 4352-byte region ...
==ABORTING

After the fix, test-rope runs to completion with exit code 0 and no ASan errors.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label Sep 14, 2026
…corruption

Disable the ggml-cpu precompiled header and remove the
std::hardware_destructive_interference_size branch from CACHE_LINE_SIZE.

The PCH force-includes ggml-impl.h before ops.h, which pulls in <new>
via <array>/<vector> and defines __cpp_lib_hardware_interference_size.
This makes the C++ kernels use CACHE_LINE_SIZE = 256 (hardware
destructive interference size) while the C work-buffer sizing code in
ggml-cpu.c always uses the fallback 64. The mismatch undersizes the
rope work buffer by (CACHE_LINE_SIZE/4 - 16) * n_threads * 4 bytes,
causing a heap-buffer-overflow that corrupts the heap and later crashes
in ggml_compute_forward_rope_flt.

Disabling the ggml-cpu PCH restores the natural include order so
ops.h is processed before <new>, keeping CACHE_LINE_SIZE consistent.
Removing the std::hardware_destructive_interference_size branch makes
the value deterministic and include-order independent.

ref: #28858

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp
@ggerganov
ggerganov force-pushed the gg/ggml-cpu-disable-pch branch from f0714a1 to 64d8a73 Compare September 14, 2026 05:53
@ggerganov ggerganov changed the title ggml-cpu : disable PCH and fix CACHE_LINE_SIZE ambiguity to fix heap corruption ggml-cpu : disable PCH and fix CACHE_LINE_SIZE ambiguity Sep 14, 2026
@ggerganov
ggerganov marked this pull request as ready for review September 14, 2026 07:02
@ggerganov
ggerganov requested a review from danbev September 14, 2026 07:31

@deadprogram deadprogram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

@yomaytk

yomaytk commented Sep 14, 2026

Copy link
Copy Markdown
Member

I confirmed that this PR resolves the error of CI (webgpu) macos in master branch. Thanks!
https://github.com/yomaytk/llama.cpp/actions/runs/34824704274

@ggerganov

Copy link
Copy Markdown
Member Author

@danbev I think we should take step back on the precompiled headers approach. It's tricky to get the PCH right and there might be other such unexpected effects, now and in the future. Would be difficult to guarantee that the codebase is compatible with PCH.

The unity builds alone are good improvement already. It reduces most of the src compilation time which is nice: https://github.com/ggml-org/llama.cpp-dev/blob/master/README-compile-times.md

@ggerganov
ggerganov merged commit 2f53959 into master Sep 14, 2026
27 of 29 checks passed
@ggerganov
ggerganov deleted the gg/ggml-cpu-disable-pch branch September 14, 2026 10:03
@danbev

danbev commented Sep 14, 2026

Copy link
Copy Markdown
Member

I think we should take step back on the precompiled headers approach. It's tricky to get the PCH right and there might be other such unexpected effects, now and in the future. Would be difficult to guarantee that the codebase is compatible with PCH.

Yeah, I agree. It looked promising when working on it but in hindsight it was a bad idea.
I'll open a PR to revert it and just keep the unity builds.

@ggerganov

Copy link
Copy Markdown
Member Author

In any case, the usage of the std::hardware_destructive_interference_size was an actual problem - with or without PCH. I've added a TODO to resolve this.

@BrewTestBot BrewTestBot mentioned this pull request Sep 14, 2026
1 task done
danbev added a commit that referenced this pull request Sep 14, 2026
This commit removes the precompiled headers that I added in Commit
3bcfeb7  ("cmake : add PCH and unity build to improve build times
(#28091)").

The motivation for this is that this looked good when developing this
but has caused multiple issues that I had taken into consideration and
we have decided to remove it and only keep the unity builds from the
above commit.

Refs: #28882 (comment)
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
…corruption (ggml-org#28882)

Disable the ggml-cpu precompiled header and remove the
std::hardware_destructive_interference_size branch from CACHE_LINE_SIZE.

The PCH force-includes ggml-impl.h before ops.h, which pulls in <new>
via <array>/<vector> and defines __cpp_lib_hardware_interference_size.
This makes the C++ kernels use CACHE_LINE_SIZE = 256 (hardware
destructive interference size) while the C work-buffer sizing code in
ggml-cpu.c always uses the fallback 64. The mismatch undersizes the
rope work buffer by (CACHE_LINE_SIZE/4 - 16) * n_threads * 4 bytes,
causing a heap-buffer-overflow that corrupts the heap and later crashes
in ggml_compute_forward_rope_flt.

Disabling the ggml-cpu PCH restores the natural include order so
ops.h is processed before <new>, keeping CACHE_LINE_SIZE consistent.
Removing the std::hardware_destructive_interference_size branch makes
the value deterministic and include-order independent.

ref: ggml-org#28858

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
This commit removes the precompiled headers that I added in Commit
3bcfeb7  ("cmake : add PCH and unity build to improve build times
(ggml-org#28091)").

The motivation for this is that this looked good when developing this
but has caused multiple issues that I had taken into consideration and
we have decided to remove it and only keep the unity builds from the
above commit.

Refs: ggml-org#28882 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: macOS arm64: heap corruption due to ggml-cpu precompiled header

5 participants