Skip to content

Fix invalid function pointer cast in cpuinfo.c #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

davidben
Copy link
Contributor

While casting function pointers is allowed in C, the function must ultimately be called through a pointer with the same type signature as the function itself. Type signature mismatches, even decaying T* to void* is undefined behavior.

UBSan flags this with -fsanitize=function. The easiest way I found to repro this was:

CC=clang-18 CXX=clang++-18 \
CFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
CXXFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
cmake -GNinja -B build -DCPUINFO_BUILD_BENCHMARKS=OFF

ninja -C build

./build/cpu-info

That gives the following error:

[...]/src/linux/multiline.c:85:11: runtime error: call to function parse_line through pointer to incorrect function type 'bool (*)(const char *, const char *, void *, unsigned long)'
cpuinfo.c: note: parse_line defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior [...]/src/linux/multiline.c:85:11

The fix is fairly straightforward: just keep the function at the type signature the expected, and cast void* instead the function instead.

While casting function pointers is allowed in C, the function must
ultimately be called through a pointer with the same type signature as
the function itself. Type signature mismatches, even decaying T* to
void* is undefined behavior.

UBSan flags this with -fsanitize=function. The easiest way I found to
repro this was:

    CC=clang-18 CXX=clang++-18 \
    CFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
    CXXFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
    cmake -GNinja -B build -DCPUINFO_BUILD_BENCHMARKS=OFF

    ninja -C build

    ./build/cpu-info

That gives the following error:

    [...]/src/linux/multiline.c:85:11: runtime error: call to function parse_line through pointer to incorrect function type 'bool (*)(const char *, const char *, void *, unsigned long)'
    cpuinfo.c: note: parse_line defined here
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior [...]/src/linux/multiline.c:85:11

The fix is fairly straightforward: just keep the function at the type
signature the expected, and cast void* instead the function instead.
@davidben
Copy link
Contributor Author

davidben commented Mar 5, 2025

friendly ping

Copy link
Contributor

@digantdesai digantdesai left a comment

Choose a reason for hiding this comment

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

I see cpuinfo_line_callback has void*. LGTM. Thanks.

@digantdesai digantdesai merged commit 1a02246 into pytorch:main Mar 21, 2025
1 check passed
@davidben davidben deleted the func-cast branch March 21, 2025 22:04
@malfet
Copy link
Contributor

malfet commented May 22, 2025

This broke clang-format, @digantdesai why did you merge it before requesting fixes to the format?

@malfet malfet mentioned this pull request May 22, 2025
malfet added a commit that referenced this pull request May 22, 2025
Regressions introduced by #275 and #287
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants