Skip to content
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

[libc][sys/wait][linux] add missing and clean up existing macros #125572

Merged
merged 4 commits into from
Feb 5, 2025

Conversation

nickdesaulniers
Copy link
Member

This patch does a few things:

  • replace macro definitions with an inclusion of the linux/wait.h kernel
    header.
    • WNOHANG
    • WUNTRACED
    • WEXITED
    • WCONTINUED
    • WSTOPPED
    • P_ALL
    • P_PID
    • P_PGID
    • P_PIDFD
  • Add missing macro definitions mandated by POSIX. Some are needed to build
    LLVM.
    • WCOREDUMP
    • WIFCONTINUED
    • WIFSIGNALELD
    • WIFSTOPPED
    • WSTOPSIG
  • Remove glibc style __W* macros. Users should stick with the POSIX macros. We
    can re-add them if necessary.
    • __WEXITSTATUS
    • __WTERMSIG
    • __WIFEXITED
    • __WIFSIGNALED
    • __WIFSTOPPED
    • __WIFCONTINUED
    • __WCOREDUMP
    • __W_EXITCODE
    • __W_STOPCODE
    • __W_CONTINUED
    • __WCOREFLAG

Fixes: #124944

This patch does a few things:
- replace macro definitions with an inclusion of the linux/wait.h kernel
  header.
  - WNOHANG
  - WUNTRACED
  - WEXITED
  - WCONTINUED
  - WSTOPPED
  - P_ALL
  - P_PID
  - P_PGID
  - P_PIDFD
- Add missing macro definitions mandated by POSIX. Some are needed to build
  LLVM.
  - WCOREDUMP
  - WIFCONTINUED
  - WIFSIGNALELD
  - WIFSTOPPED
  - WSTOPSIG
- Remove glibc style __W* macros.  Users should stick with the POSIX macros. We
  can re-add them if necessary.
  - __WEXITSTATUS
  - __WTERMSIG
  - __WIFEXITED
  - __WIFSIGNALED
  - __WIFSTOPPED
  - __WIFCONTINUED
  - __WCOREDUMP
  - __W_EXITCODE
  - __W_STOPCODE
  - __W_CONTINUED
  - __WCOREFLAG

Fixes: llvm#124944
@llvmbot llvmbot added the libc label Feb 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2025

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

This patch does a few things:

  • replace macro definitions with an inclusion of the linux/wait.h kernel
    header.
    • WNOHANG
    • WUNTRACED
    • WEXITED
    • WCONTINUED
    • WSTOPPED
    • P_ALL
    • P_PID
    • P_PGID
    • P_PIDFD
  • Add missing macro definitions mandated by POSIX. Some are needed to build
    LLVM.
    • WCOREDUMP
    • WIFCONTINUED
    • WIFSIGNALELD
    • WIFSTOPPED
    • WSTOPSIG
  • Remove glibc style __W* macros. Users should stick with the POSIX macros. We
    can re-add them if necessary.
    • __WEXITSTATUS
    • __WTERMSIG
    • __WIFEXITED
    • __WIFSIGNALED
    • __WIFSTOPPED
    • __WIFCONTINUED
    • __WCOREDUMP
    • __W_EXITCODE
    • __W_STOPCODE
    • __W_CONTINUED
    • __WCOREFLAG

Fixes: #124944


Full diff: https://github.com/llvm/llvm-project/pull/125572.diff

1 Files Affected:

  • (modified) libc/include/llvm-libc-macros/linux/sys-wait-macros.h (+14-31)
diff --git a/libc/include/llvm-libc-macros/linux/sys-wait-macros.h b/libc/include/llvm-libc-macros/linux/sys-wait-macros.h
index c101638fdae340..d01cfa71ba3906 100644
--- a/libc/include/llvm-libc-macros/linux/sys-wait-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-wait-macros.h
@@ -9,36 +9,19 @@
 #ifndef LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H
 #define LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H
 
-// Wait flags
-#define WNOHANG 1    // Do not block
-#define WUNTRACED 2  // Report is a child has stopped even if untraced
-#define WEXITED 4    // Report dead child
-#define WCONTINUED 8 // Report if a stopped child has been resumed by SIGCONT
-#define WSTOPPED WUNTRACED
-
-// Wait status info macros
-#define __WEXITSTATUS(status) (((status)&0xff00) >> 8)
-#define __WTERMSIG(status) ((status)&0x7f)
-#define __WIFEXITED(status) (__WTERMSIG(status) == 0)
-
-// Macros for constructing status values.
-#define __W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
-#define __W_STOPCODE(sig) ((sig) << 8 | 0x7f)
-#define __W_CONTINUED 0xffff
-#define __WCOREFLAG 0x80
-
-#define WEXITSTATUS(status) __WEXITSTATUS(status)
-#define WTERMSIG(status) __WTERMSIG(status)
-#define WIFEXITED(status) __WIFEXITED(status)
-
-#define WCOREFLAG __WCOREFLAG
-#define W_EXITCODE(ret, sig) __W_EXITCODE(ret, sig)
-#define W_STOPCODE(sig) __W_STOPCODE(sig)
-
-// First argument to waitid:
-#define P_ALL 0
-#define P_PID 1
-#define P_PGID 2
-#define P_PIDFD 3
+#include <linux/wait.h>
+
+#define WCOREDUMP(status) ((status) & WCOREFLAG)
+#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
+#define WIFCONTINUED(status) ((status) == 0xffff)
+#define WIFEXITED(status) (WTERMSIG(status) == 0)
+#define WIFSIGNALED(status) ((WTERMSIG(status) + 1) >= 2)
+#define WIFSTOPPED(status) (WTERMSIG(status) == 0x7f)
+#define WSTOPSIG(status) WEXITSTATUS(status)
+#define WTERMSIG(status) ((status) & 0x7f)
+
+#define WCOREFLAG 0x80
+#define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
+#define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
 
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H


#define WCOREFLAG 0x80
#define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
#define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
Copy link
Member Author

Choose a reason for hiding this comment

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

These three aren't specified by POSIX. Are they glibc extensions? Perhaps doesn't matter for this PR, but I did choose to retain them in case they're in widespread use.

Copy link
Contributor

Choose a reason for hiding this comment

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

they're in BSD too.

the commit message on the change that added them to bionic was "Fixes for building gdbserver (and gdb) out of the box.".

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thanks for the context. Maybe ACSRG can standardize them at some point. Will leave them for now.

@nickdesaulniers
Copy link
Member Author

ping for review

@enh-google
Copy link
Contributor

(seemed plausible to me, but i'm assuming you want an llvm-libc committer's review...)

@nickdesaulniers
Copy link
Member Author

nickdesaulniers commented Feb 5, 2025

(seemed plausible to me, but i'm assuming you want an llvm-libc committer's review...)

I want you to be an llvm-libc committer and reviewer 😉 . If you expect me to be for bionic, then it's only reciprocal.

@nickdesaulniers nickdesaulniers merged commit 57614a3 into llvm:main Feb 5, 2025
16 checks passed
@nickdesaulniers nickdesaulniers deleted the sys_wait branch February 5, 2025 18:01
@llvm-ci
Copy link
Collaborator

llvm-ci commented Feb 5, 2025

LLVM Buildbot has detected a new failure on builder libc-riscv32-qemu-yocto-fullbuild-dbg running on rv32gc-qemu-system while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/196/builds/4589

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
-- Using getrandom for hashtable randomness
-- Integration test for hdrgen added.
-- check-runtimes does nothing.
-- Configuring done (3.5s)
-- Generating done (8.9s)
-- Build files have been written to: /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build
@@@BUILD_STEP build libc@@@
Running: ninja libc
[1/5] Generating sys-wait-macros.h
[2/5] Building CXX object libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait4.dir/wait4.cpp.o
FAILED: libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait4.dir/wait4.cpp.o 
/usr/local/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_20_0_0_git -D_DEBUG -I/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc -isystem /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/libc/include -mabi=ilp32d -march=rv32imafdc --target=riscv32-unknown-linux-gnu --sysroot=/opt/riscv/sysroot --gcc-toolchain=/opt/riscv -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -std=gnu++17 --target=riscv32-unknown-linux-gnu -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS -fpie -ffreestanding -DLIBC_FULL_BUILD -nostdlibinc -idirafter=/usr/include -ffixed-point -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wdeprecated -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -DLIBC_COPT_PUBLIC_PACKAGING -MD -MT libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait4.dir/wait4.cpp.o -MF libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait4.dir/wait4.cpp.o.d -o libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait4.dir/wait4.cpp.o -c /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/wait4.cpp
In file included from /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/wait4.cpp:14:
/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/wait4Impl.h:68:22: error: use of undeclared identifier '__W_CONTINUED'
   68 |       *wait_status = __W_CONTINUED;
      |                      ^
1 error generated.
[3/5] Building CXX object libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.waitpid.dir/waitpid.cpp.o
FAILED: libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.waitpid.dir/waitpid.cpp.o 
/usr/local/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_20_0_0_git -D_DEBUG -I/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc -isystem /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/libc/include -mabi=ilp32d -march=rv32imafdc --target=riscv32-unknown-linux-gnu --sysroot=/opt/riscv/sysroot --gcc-toolchain=/opt/riscv -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -std=gnu++17 --target=riscv32-unknown-linux-gnu -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS -fpie -ffreestanding -DLIBC_FULL_BUILD -nostdlibinc -idirafter=/usr/include -ffixed-point -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wdeprecated -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -DLIBC_COPT_PUBLIC_PACKAGING -MD -MT libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.waitpid.dir/waitpid.cpp.o -MF libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.waitpid.dir/waitpid.cpp.o.d -o libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.waitpid.dir/waitpid.cpp.o -c /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/waitpid.cpp
In file included from /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/waitpid.cpp:13:
/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/wait4Impl.h:68:22: error: use of undeclared identifier '__W_CONTINUED'
   68 |       *wait_status = __W_CONTINUED;
      |                      ^
1 error generated.
[4/5] Building CXX object libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait.dir/wait.cpp.o
FAILED: libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait.dir/wait.cpp.o 
/usr/local/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_20_0_0_git -D_DEBUG -I/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc -isystem /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/libc/include -mabi=ilp32d -march=rv32imafdc --target=riscv32-unknown-linux-gnu --sysroot=/opt/riscv/sysroot --gcc-toolchain=/opt/riscv -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -std=gnu++17 --target=riscv32-unknown-linux-gnu -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS -fpie -ffreestanding -DLIBC_FULL_BUILD -nostdlibinc -idirafter=/usr/include -ffixed-point -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wdeprecated -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -DLIBC_COPT_PUBLIC_PACKAGING -MD -MT libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait.dir/wait.cpp.o -MF libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait.dir/wait.cpp.o.d -o libc/src/sys/wait/linux/CMakeFiles/libc.src.sys.wait.linux.wait.dir/wait.cpp.o -c /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/wait.cpp
In file included from /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/wait.cpp:14:
/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/wait4Impl.h:68:22: error: use of undeclared identifier '__W_CONTINUED'
   68 |       *wait_status = __W_CONTINUED;
      |                      ^
1 error generated.
ninja: build stopped: subcommand failed.
['ninja', 'libc'] exited with return code 1.
The build step threw an exception...
Traceback (most recent call last):
  File "/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 164, in step
    yield
  File "/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 126, in main
    run_command(['ninja', 'libc'])
  File "/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 179, in run_command
    util.report_run_cmd(cmd, cwd=directory)
  File "/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-zorg/zorg/buildbot/builders/annotated/util.py", line 49, in report_run_cmd
    subprocess.check_call(cmd, shell=shell, *args, **kwargs)
  File "/usr/lib/python3.12/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ninja', 'libc']' returned non-zero exit status 1.
@@@STEP_FAILURE@@@

@nickdesaulniers
Copy link
Member Author

ack, looking:

In file included from /home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/linux/waitpid.cpp:13:
/home/libcrv32buildbot/bbroot/libc-riscv32-qemu-yocto-fullbuild-dbg/llvm-project/libc/src/sys/wait/wait4Impl.h:68:22: error: use of undeclared identifier '__W_CONTINUED'
   68 |       *wait_status = __W_CONTINUED;
      |                      ^

nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Feb 5, 2025
riscv32 currently doesn't have SYS_wait4, so wait4 is implemented via fallback
to SYS_waitid. In llvm#125572, I missed that we had one use of the removed
__W_CONTINUED value. Hard code it here.

Fixes: llvm#125572
nickdesaulniers added a commit that referenced this pull request Feb 5, 2025
riscv32 currently doesn't have SYS_wait4, so wait4 is implemented via fallback
to SYS_waitid. In #125572, I missed that we had one use of the removed
__W_CONTINUED value. Hard code it here.

Fixes: #125572
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…m#125572)

This patch does a few things:
- replace macro definitions with an inclusion of the linux/wait.h kernel
  header.
  - WNOHANG
  - WUNTRACED
  - WEXITED
  - WCONTINUED
  - WSTOPPED
  - P_ALL
  - P_PID
  - P_PGID
  - P_PIDFD
- Add missing macro definitions mandated by POSIX. Some are needed to
  build LLVM.
  - WCOREDUMP
  - WIFCONTINUED
  - WIFSIGNALELD
  - WIFSTOPPED
  - WSTOPSIG
- Remove glibc style __W* macros. Users should stick with the POSIX
  macros. We can re-add them if necessary.
  - __WEXITSTATUS
  - __WTERMSIG
  - __WIFEXITED
  - __WIFSIGNALED
  - __WIFSTOPPED
  - __WIFCONTINUED
  - __WCOREDUMP
  - __W_EXITCODE
  - __W_STOPCODE
  - __W_CONTINUED
  - __WCOREFLAG

Fixes: llvm#124944
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…5929)

riscv32 currently doesn't have SYS_wait4, so wait4 is implemented via fallback
to SYS_waitid. In llvm#125572, I missed that we had one use of the removed
__W_CONTINUED value. Hard code it here.

Fixes: llvm#125572
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc][POSIX] provide WIFSIGNALED from <sys/wait.h>
5 participants