Skip to content
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1a2fe13
feat: add proxy header for `cpu_set_t`
krishna2803 Feb 7, 2025
ba8c535
fix: replace <sched.h> with proxy headers
krishna2803 Feb 7, 2025
c6a14a1
fix: rename `__mask` to `__bits` for compatibility with glibc cpu_set_t
krishna2803 Feb 11, 2025
822bdb0
fix: change to proxy headers for `cpu_set_t`, `size_t` and `pid_t` types
krishna2803 Feb 11, 2025
00b5943
fix: update CMakeLists for proxy header dependencies
krishna2803 Feb 11, 2025
8c990d2
style: clang-format
krishna2803 Feb 11, 2025
926702c
fix: add proxy headers for unittests
krishna2803 Feb 11, 2025
02b5e6c
fix: add proxy header for `struct_sched_param`
krishna2803 Feb 11, 2025
99ee89d
fix: add proxy header for `sched_macros`
krishna2803 Feb 11, 2025
5a91cde
fix: tests for sched functions
krishna2803 Feb 11, 2025
ece948f
style: clang-format
krishna2803 Feb 11, 2025
c2ccccf
fix: remove unused dependencies from CMakeLists
krishna2803 Feb 12, 2025
fd0ec52
fix: remove unused header and comply llvm coding standard
krishna2803 Feb 12, 2025
32f9a0a
fix: add missing header for struct_timespec
krishna2803 Feb 12, 2025
88fd93f
Merge branch 'main' into fix-cpuset-includes
krishna2803 Feb 13, 2025
b34baef
fix: add `libc.include.llvm-libc-macros.sched_macros` for full build …
krishna2803 Feb 14, 2025
de1eead
fix: attempt fix for `struct_timespec`
krishna2803 Feb 15, 2025
55849ca
fix: attempt fix for `sched`
krishna2803 Feb 15, 2025
7b48a0c
add: sched.h in scudo CMakeListsts
krishna2803 Feb 16, 2025
9a42b90
revert: "add: sched.h in scudo CMakeListsts"
krishna2803 Feb 16, 2025
8baed07
merge: branch 'main' into fix-cpuset-includes
krishna2803 Feb 23, 2025
9228a3b
revert: "fix: attempt fix for `struct_timespec`"
krishna2803 Feb 23, 2025
0811fb1
revert: "fix: attempt fix for `sched`"
krishna2803 Feb 23, 2025
23f30e9
merge: branch 'main' into fix-cpuset-includes
krishna2803 Feb 27, 2025
cbcaf85
merge: branch 'main' into fix-cpuset-includes
krishna2803 Mar 5, 2025
3f67c42
fix: attempt removing sched.h
krishna2803 Mar 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,14 @@ add_proxy_header_library(
libc.include.wchar
)

add_proxy_header_library(
sched_macros
HDRS
sched_macros.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.sched_macros # for full build mode
libc.include.sched
)

add_subdirectory(types)
add_subdirectory(func)
27 changes: 27 additions & 0 deletions libc/hdr/sched_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Definition of macros from sched.h ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_SCHED_MACROS_H
#define LLVM_LIBC_HDR_SCHED_MACROS_H

#ifdef LIBC_FULL_BUILD

#ifndef _SCHED_H
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be in here. You are using this to suppress the system/glibc sched.h definitions. We should investigate and make sure the header inclusions are separated in full build mode instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

If for some reason, the system's usr/include/sched.h and libc/hdr/sched_macros.h are included in the same translation unit, we need to do one of the following options:

  1. Fulfilling our sched.h related headers to be complete enough so that usr/include/sched.h is not needed anymore in full build mode
  2. Update our implementations/tests so that usr/include/sched.h is not needed anymore in full build mode

Let me know if you need help tracing back to how all of those inclusion conflicts are happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

guess i'll just have to work on this using the github workflow failures only lol. I tried:

cmake -S ../runtimes -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES=libc -DLLVM_USE_LINKER=lld LLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_RUNTIMES="libc;compiler-rt" -DCOMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED=OFF -DCOMPILER_RT_BUILD_GWP_ASAN=OFF -DLLVM_LIBC_INCLUDE_SCUDO=ON -DCOMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC=ON -DLLVM_LIBC_FULL_BUILD=ON

followed by:

ninja check-libc

and i'm not able to reproduce these errors (on X86) so these are specific to aarch64 and i have no other way of testing for that architecture.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The source compiler-rt/lib/scudo/standalone/linux.cpp that is causing the errors uses sched.h:

in the function getNumberOfCPUs:

u32 getNumberOfCPUs() {
cpu_set_t CPUs;
// sched_getaffinity can fail for a variety of legitimate reasons (lack of
// CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
return 0;
return static_cast<u32>(CPU_COUNT(&CPUs));
}

which is declared in common.h

// Returns 0 if the number of CPUs could not be determined.
u32 getNumberOfCPUs();

we already have definitions for cpu_set_t and sched_getaffinity but not for CPU_COUNT. I planned on first implementing the proxy headers for sched.h related definitions and then work on #124642 for the implementation of those macros but it seems there's a cyclic dependency within these issues. Since sched.h is not too big of a header, I think we should go with the 1st approach:

  1. Fulfilling our sched.h related headers to be complete enough so that usr/include/sched.h is not needed anymore in full build mode

@lntue How should I proceed? Should I complete #124642 prior to this, or implement CPU_COUNT within this PR only, which should complete the requirements for Scudo for now?

Copy link
Contributor Author

@krishna2803 krishna2803 Mar 13, 2025

Choose a reason for hiding this comment

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

Oh, I missed it. we do have a definition for CPU_COUNT here:

#define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set)
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)

that would mean we do have a complete enough sched.h implementation so the bug shouldn't be because of some missing implementation (at least for now) which then means that the bug is due to the order of includes only?

#define _SCHED_H 1

#include "include/llvm-libc-macros/sched-macros.h"

#endif

#else // Overlay mode

#include <sched.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_SCHED_MACROS_H
16 changes: 16 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ add_proxy_header_library(
libc.include.stdio
)

add_proxy_header_library(
cpu_set_t
HDRS
cpu_set_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.cpu_set_t
)

add_proxy_header_library(
locale_t
HDRS
Expand Down Expand Up @@ -349,3 +357,11 @@ add_proxy_header_library(
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_pollfd
)

add_proxy_header_library(
struct_sched_param
HDRS
struct_sched_param.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_sched_param
)
22 changes: 22 additions & 0 deletions libc/hdr/types/cpu_set_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Proxy for cpu_set_t -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
#define LLVM_LIBC_HDR_TYPES_CPU_SET_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/cpu_set_t.h"

#else // Overlay mode

#include <sched.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
22 changes: 22 additions & 0 deletions libc/hdr/types/struct_sched_param.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Proxy for struct sched_param --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_SCHED_PARAM_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_SCHED_PARAM_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_sched_param.h"

#else // Overlay mode

#include <sched.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SCHED_PARAM_H
2 changes: 1 addition & 1 deletion libc/include/llvm-libc-types/cpu_set_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
typedef struct {
// If a processor with more than 1024 CPUs is to be supported in future,
// we need to adjust the size of this array.
unsigned long __mask[128 / sizeof(unsigned long)];
unsigned long __bits[128 / sizeof(unsigned long)];
Copy link
Member

Choose a reason for hiding this comment

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

What's up with this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so the glibc implementation of cpu_set_t uses __bits instead of __mask and when we're in overlay mode, this would include sched.h from the system's libc and that might cause bugs if glibc is being used in the system

ref: https://elixir.bootlin.com/glibc/glibc-2.41/source/posix/bits/cpu-set.h#L38-L43

Copy link
Member

Choose a reason for hiding this comment

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

when we're in overlay mode, this would include sched.h from the system's libc

Right, and libc/include/llvm-libc-types/cpu_set_t.h should only be included in fullbuild mode, not overlay mode.

} cpu_set_t;

#endif // LLVM_LIBC_TYPES_CPU_SET_T_H
3 changes: 3 additions & 0 deletions libc/include/llvm-libc-types/struct_timespec.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

#include "time_t.h"

#ifndef _STRUCT_TIMESPEC
#define _STRUCT_TIMESPEC 1
struct timespec {
time_t tv_sec; /* Seconds. */
/* TODO: BIG_ENDIAN may require padding. */
long tv_nsec; /* Nanoseconds. */
};
#endif // _STRUCT_TIMESPEC

#endif // LLVM_LIBC_TYPES_STRUCT_TIMESPEC_H
36 changes: 17 additions & 19 deletions libc/src/sched/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ add_entrypoint_object(
HDRS
../sched_getaffinity.h
DEPENDS
libc.include.sched
libc.hdr.types.cpu_set_t
libc.hdr.types.pid_t
libc.hdr.types.size_t
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -17,7 +19,9 @@ add_entrypoint_object(
HDRS
../sched_setaffinity.h
DEPENDS
libc.include.sched
libc.hdr.types.cpu_set_t
libc.hdr.types.pid_t
libc.hdr.types.size_t
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -29,7 +33,8 @@ add_entrypoint_object(
HDRS
../sched_getcpucount.h
DEPENDS
libc.include.sched
libc.hdr.types.cpu_set_t
libc.hdr.types.size_t
)

add_entrypoint_object(
Expand All @@ -39,7 +44,6 @@ add_entrypoint_object(
HDRS
../sched_yield.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -51,9 +55,8 @@ add_entrypoint_object(
HDRS
../sched_setparam.h
DEPENDS
libc.include.sys_syscall
libc.include.time
libc.include.sched
libc.hdr.types.pid_t
libc.hdr.types.struct_sched_param
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -65,9 +68,8 @@ add_entrypoint_object(
HDRS
../sched_getparam.h
DEPENDS
libc.include.sys_syscall
libc.include.time
libc.include.sched
libc.hdr.types.pid_t
libc.hdr.types.struct_sched_param
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -79,9 +81,8 @@ add_entrypoint_object(
HDRS
../sched_setscheduler.h
DEPENDS
libc.include.sys_syscall
libc.include.time
libc.include.sched
libc.hdr.types.pid_t
libc.hdr.types.struct_sched_param
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -93,8 +94,7 @@ add_entrypoint_object(
HDRS
../sched_getscheduler.h
DEPENDS
libc.include.sched
libc.include.sys_syscall
libc.hdr.types.pid_t
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -106,7 +106,6 @@ add_entrypoint_object(
HDRS
../sched_get_priority_min.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -118,7 +117,6 @@ add_entrypoint_object(
HDRS
../sched_get_priority_max.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Expand All @@ -130,8 +128,8 @@ add_entrypoint_object(
HDRS
../sched_rr_get_interval.h
DEPENDS
libc.include.sys_syscall
libc.include.sched
libc.hdr.types.pid_t
libc.hdr.types.struct_timespec
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
9 changes: 6 additions & 3 deletions libc/src/sched/linux/sched_getaffinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#include "src/sched/sched_getaffinity.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include <sched.h>
#include "hdr/types/cpu_set_t.h"
#include "hdr/types/pid_t.h"
#include "hdr/types/size_t.h"

#include <stdint.h>
#include <sys/syscall.h> // For syscall numbers.

Expand Down
14 changes: 7 additions & 7 deletions libc/src/sched/linux/sched_getcpucount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

#include "src/sched/sched_getcpucount.h"

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

#include <sched.h>
#include <stddef.h>
#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, __sched_getcpucount,
(size_t cpuset_size, const cpu_set_t *mask)) {
int result = 0;
for (size_t i = 0; i < cpuset_size / sizeof(long); ++i) {
result += __builtin_popcountl(mask->__mask[i]);
}
for (size_t i = 0; i < cpuset_size / sizeof(long); ++i)
result += __builtin_popcountl(mask->__bits[i]);
Copy link
Member

Choose a reason for hiding this comment

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


return result;
}

Expand Down
7 changes: 5 additions & 2 deletions libc/src/sched/linux/sched_getparam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include "src/sched/sched_getparam.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include "hdr/types/pid_t.h"
#include "hdr/types/struct_sched_param.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
Expand Down
6 changes: 4 additions & 2 deletions libc/src/sched/linux/sched_getscheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "src/sched/sched_getscheduler.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include "hdr/types/pid_t.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
Expand Down
7 changes: 5 additions & 2 deletions libc/src/sched/linux/sched_rr_get_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include "src/sched/sched_rr_get_interval.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include "hdr/types/pid_t.h"
#include "hdr/types/struct_timespec.h"

#include <sys/syscall.h> // For syscall numbers.

#ifdef SYS_sched_rr_get_interval_time64
Expand Down
8 changes: 5 additions & 3 deletions libc/src/sched/linux/sched_setaffinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include "src/sched/sched_setaffinity.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include <sched.h>
#include "hdr/types/cpu_set_t.h"
#include "hdr/types/pid_t.h"
#include "hdr/types/size_t.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
Expand Down
7 changes: 5 additions & 2 deletions libc/src/sched/linux/sched_setparam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include "src/sched/sched_setparam.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include "hdr/types/pid_t.h"
#include "hdr/types/struct_sched_param.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
Expand Down
7 changes: 5 additions & 2 deletions libc/src/sched/linux/sched_setscheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include "src/sched/sched_setscheduler.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/errno/libc_errno.h"

#include "hdr/types/pid_t.h"
#include "hdr/types/struct_sched_param.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
Expand Down
Loading
Loading