Skip to content

[libc] -Wcast-function-type in libc/src/signal/linux/signal_utils.h #74617

@nickdesaulniers

Description

@nickdesaulniers
Member
[270/361] Building CXX object projects/libc/src/signal/linux/CMakeFiles/libc.src.signal.linux.raise.dir/raise.cpp.o
In file included from /android0/llvm-project/libc/src/signal/linux/raise.cpp:10:
/android0/llvm-project/libc/src/signal/linux/signal_utils.h: In member function ‘__llvm_libc_18_0_0_git::KernelSigaction& __llvm_libc_18_0_0_git::KernelSigaction::operator=(const sigaction&)’:
/android0/llvm-project/libc/src/signal/linux/signal_utils.h:38:20: warning: cast between incompatible function types from ‘void (*)(int, siginfo_t*, void*)’ to ‘void (*)(int)’ [-Wcast-function-type]
   38 |       sa_handler = reinterpret_cast<HandlerType *>(sa.sa_sigaction);
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/android0/llvm-project/libc/src/signal/linux/signal_utils.h: In member function ‘__llvm_libc_18_0_0_git::KernelSigaction::operator sigaction() const’:
/android0/llvm-project/libc/src/signal/linux/signal_utils.h:51:25: warning: cast between incompatible function types from ‘void (*)(int)’ to ‘void (*)(int, siginfo_t*, void*)’ [-Wcast-function-type]
   51 |       sa.sa_sigaction = reinterpret_cast<SiginfoHandlerType *>(sa_handler);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm trying to get -Werror re-enabled in #74506; building with GCC flags the above warning.

Activity

llvmbot

llvmbot commented on Dec 6, 2023

@llvmbot
Member

@llvm/issue-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

``` [270/361] Building CXX object projects/libc/src/signal/linux/CMakeFiles/libc.src.signal.linux.raise.dir/raise.cpp.o In file included from /android0/llvm-project/libc/src/signal/linux/raise.cpp:10: /android0/llvm-project/libc/src/signal/linux/signal_utils.h: In member function ‘__llvm_libc_18_0_0_git::KernelSigaction& __llvm_libc_18_0_0_git::KernelSigaction::operator=(const sigaction&)’: /android0/llvm-project/libc/src/signal/linux/signal_utils.h:38:20: warning: cast between incompatible function types from ‘void (*)(int, siginfo_t*, void*)’ to ‘void (*)(int)’ [-Wcast-function-type] 38 | sa_handler = reinterpret_cast<HandlerType *>(sa.sa_sigaction); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /android0/llvm-project/libc/src/signal/linux/signal_utils.h: In member function ‘__llvm_libc_18_0_0_git::KernelSigaction::operator sigaction() const’: /android0/llvm-project/libc/src/signal/linux/signal_utils.h:51:25: warning: cast between incompatible function types from ‘void (*)(int)’ to ‘void (*)(int, siginfo_t*, void*)’ [-Wcast-function-type] 51 | sa.sa_sigaction = reinterpret_cast<SiginfoHandlerType *>(sa_handler); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` I'm trying to get -Werror re-enabled in https://github.com//pull/74506; building with GCC flags the above warning.
nickdesaulniers

nickdesaulniers commented on Dec 6, 2023

@nickdesaulniers
MemberAuthor

https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/bits/signal_types.h?autodive=0%2F%2F#60

alludes to the issue that is mentioned near the warning.

// The POSIX definition of struct sigaction and the sigaction data structure
// expected by the rt_sigaction syscall differ in their definition. So, we

SchrodingerZhu

SchrodingerZhu commented on Dec 6, 2023

@SchrodingerZhu
Contributor

Casting to an intermediate value should suppress the warning. See https://godbolt.org/z/PncMfvfz7

using T0 = void (*)(int, void*, void*);
using T1 = void (*)(int);

T0 test(T1 f) {
    return reinterpret_cast<T0>(f);
}

T0 test1(T1 f) {
    void * ptr = reinterpret_cast<void *>(f);
    return reinterpret_cast<T0>(ptr);
}
nickdesaulniers

nickdesaulniers commented on Dec 6, 2023

@nickdesaulniers
MemberAuthor

Sure, we can probably get by with laundering the type through a void*. Though the comment in bionic makes me think that our current implementation is wrong; or might need to be adjusted further in the future.

SchrodingerZhu

SchrodingerZhu commented on Dec 6, 2023

@SchrodingerZhu
Contributor

Would HandlerType = void (*) (int, ...) work then?

Casting around

void (*a)(int, ...);
void (*b)((int, void*, void*);

will just work without warning. Per psABI's view, it seems to be safe enough.

However, I suppose you are talking about separating them based on implementations rather than figuring out an generic type that just works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @nickdesaulniers@EugeneZelenko@SchrodingerZhu@llvmbot

      Issue actions

        [libc] -Wcast-function-type in libc/src/signal/linux/signal_utils.h · Issue #74617 · llvm/llvm-project