From: Mark Geisert <mark@maxrnd.com>
To: cygwin-patches@cygwin.com
Cc: Mark Geisert <mark@maxrnd.com>
Subject: [PATCH] Cygwin: Fix type mismatch on sys/cpuset.h
Date: Fri, 10 Mar 2023 02:18:21 -0800 [thread overview]
Message-ID: <20230310101821.18319-1-mark@maxrnd.com> (raw)
Addresses https://cygwin.com/pipermail/cygwin/2023-March/253220.html
Take the opportunity to follow FreeBSD's and Linux's lead in recasting
macro inline code as calls to static inline functions. This allows the
macros to be type-safe. In addition, added a lower bound check to the
functions that use a cpu number to avoid a potential buffer underrun on
a bad argument. h/t to Corinna for the advice on recasting.
---
winsup/cygwin/include/sys/cpuset.h | 138 ++++++++++++++++++++---------
1 file changed, 96 insertions(+), 42 deletions(-)
diff --git a/winsup/cygwin/include/sys/cpuset.h b/winsup/cygwin/include/sys/cpuset.h
index 572565165..d83359fdf 100644
--- a/winsup/cygwin/include/sys/cpuset.h
+++ b/winsup/cygwin/include/sys/cpuset.h
@@ -31,50 +31,104 @@ int __sched_getaffinity_sys (pid_t, size_t, cpu_set_t *);
/* These macros alloc or free dynamically-sized cpu sets of size 'num' cpus.
Allocations are padded such that full-word operations can be done easily. */
-#define CPU_ALLOC_SIZE(num) ((num+__NCPUBITS-1) / __NCPUBITS) * sizeof (__cpu_mask)
-#define CPU_ALLOC(num) __builtin_malloc (CPU_ALLOC_SIZE(num))
-#define CPU_FREE(set) __builtin_free (set)
+#define CPU_ALLOC_SIZE(num) __cpuset_alloc_size (num)
+static inline size_t
+__cpuset_alloc_size (int num)
+{
+ return (size_t) (((num + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask));
+}
+
+#define CPU_ALLOC(num) __cpuset_alloc (num)
+static inline cpu_set_t *
+__cpuset_alloc (int num)
+{
+ return (cpu_set_t *) __builtin_malloc (CPU_ALLOC_SIZE(num));
+}
+
+#define CPU_FREE(set) __cpuset_free (set)
+static inline void
+__cpuset_free (cpu_set_t *set)
+{
+ __builtin_free (set);
+}
/* These _S macros operate on dynamically-sized cpu sets of size 'siz' bytes */
-#define CPU_ZERO_S(siz, set) __builtin_memset (set, 0, siz)
-
-#define CPU_SET_S(cpu,siz,set) \
- if (cpu < 8 * siz) \
- (set)->__bits[__CPUELT(cpu)] |= __CPUMASK(cpu);
-
-#define CPU_CLR_S(cpu,siz,set) \
- if (cpu < 8 * siz) \
- (set)->__bits[__CPUELT(cpu)] &= ~(__CPUMASK(cpu));
-
-#define CPU_ISSET_S(cpu,siz,set) \
- ({int res = 0; \
- if (cpu < 8 * siz) \
- res = !!((set)->__bits[__CPUELT(cpu)] & __CPUMASK(cpu)); \
- res;})
-
-#define CPU_COUNT_S(siz, set) \
- ({int tot = 0;\
- for (int i = 0; i < siz / sizeof (__cpu_mask); i++) \
- tot += __builtin_popcountl ((set)->__bits[i]); \
- tot;})
-
-#define CPU_AND_S(siz, dst, src1, src2) \
- for (int i = 0; i < siz / sizeof (__cpu_mask); i++) \
- (dst)->__bits[i] = (src1)->__bits[i] & (src2)->__bits[i];
-
-#define CPU_OR_S(siz, dst, src1, src2) \
- for (int i = 0; i < siz / sizeof (__cpu_mask); i++) \
- (dst)->__bits[i] = (src1)->__bits[i] | (src2)->__bits[i];
-
-#define CPU_XOR_S(siz, dst, src1, src2) \
- for (int i = 0; i < siz / sizeof (__cpu_mask); i++) \
- (dst)->__bits[i] = (src1)->__bits[i] ^ (src2)->__bits[i];
-
-#define CPU_EQUAL_S(siz, src1, src2) \
- ({int res = 1; \
- for (int i = 0; res && i < siz / sizeof (__cpu_mask); i++) \
- res &= (src1)->__bits[i] == (src2)->__bits[i]; \
- res;})
+#define CPU_ZERO_S(siz, set) __cpuset_zero_s (siz, set)
+static inline void
+__cpuset_zero_s (size_t siz, cpu_set_t *set)
+{
+ (void) __builtin_memset (set, 0, siz);
+}
+
+#define CPU_SET_S(cpu, siz, set) __cpuset_set_s (cpu, siz, set)
+static inline void
+__cpuset_set_s (int cpu, size_t siz, cpu_set_t *set)
+{
+ if (cpu >= 0 && cpu < 8 * siz)
+ (set)->__bits[__CPUELT(cpu)] |= __CPUMASK(cpu);
+}
+
+#define CPU_CLR_S(cpu, siz, set) __cpuset_clr_s (cpu, siz, set)
+static inline void
+__cpuset_clr_s (int cpu, size_t siz, cpu_set_t *set)
+{
+ if (cpu >= 0 && cpu < 8 * siz)
+ (set)->__bits[__CPUELT(cpu)] &= ~(__CPUMASK(cpu));
+}
+
+#define CPU_ISSET_S(cpu, siz, set) __cpuset_isset_s (cpu, siz, set)
+static inline int
+__cpuset_isset_s (int cpu, size_t siz, cpu_set_t *set)
+{
+ int res = 0;
+ if (cpu >= 0 && cpu < 8 * siz)
+ res = !!((set)->__bits[__CPUELT(cpu)] & __CPUMASK(cpu));
+ return res;
+}
+
+#define CPU_COUNT_S(siz, set) __cpuset_count_s (siz, set)
+static inline int
+__cpuset_count_s (size_t siz, cpu_set_t *set)
+{
+ int res = 0;
+ for (int i = 0; i < siz / sizeof (__cpu_mask); i++)
+ res += __builtin_popcountl ((set)->__bits[i]);
+ return res;
+}
+
+#define CPU_AND_S(siz, dst, src1, src2) __cpuset_and_s (siz, dst, src1, src2)
+static inline void
+__cpuset_and_s (size_t siz, cpu_set_t *dst, cpu_set_t *src1, cpu_set_t *src2)
+{
+ for (int i = 0; i < siz / sizeof (__cpu_mask); i++)
+ (dst)->__bits[i] = (src1)->__bits[i] & (src2)->__bits[i];
+}
+
+#define CPU_OR_S(siz, dst, src1, src2) __cpuset_or_s (siz, dst, src1, src2)
+static inline void
+__cpuset_or_s (size_t siz, cpu_set_t *dst, cpu_set_t *src1, cpu_set_t *src2)
+{
+ for (int i = 0; i < siz / sizeof (__cpu_mask); i++)
+ (dst)->__bits[i] = (src1)->__bits[i] | (src2)->__bits[i];
+}
+
+#define CPU_XOR_S(siz, dst, src1, src2) __cpuset_xor_s (siz, dst, src1, src2)
+static inline void
+__cpuset_xor_s (size_t siz, cpu_set_t *dst, cpu_set_t *src1, cpu_set_t *src2)
+{
+ for (int i = 0; i < siz / sizeof (__cpu_mask); i++)
+ (dst)->__bits[i] = (src1)->__bits[i] ^ (src2)->__bits[i];
+}
+
+#define CPU_EQUAL_S(siz, src1, src2) __cpuset_equal_s (siz, src1, src2)
+static inline int
+__cpuset_equal_s (size_t siz, cpu_set_t *src1, cpu_set_t *src2)
+{
+ int res = 1;
+ for (int i = 0; res && i < siz / sizeof (__cpu_mask); i++)
+ res &= (src1)->__bits[i] == (src2)->__bits[i];
+ return res;
+}
/* These macros operate on fixed-size cpu sets of size __CPU_SETSIZE cpus */
#define CPU_ZERO(set) CPU_ZERO_S(sizeof (cpu_set_t), set)
--
2.39.0
next reply other threads:[~2023-03-10 10:18 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-10 10:18 Mark Geisert [this message]
2023-03-13 9:31 ` Corinna Vinschen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230310101821.18319-1-mark@maxrnd.com \
--to=mark@maxrnd.com \
--cc=cygwin-patches@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).