public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
@ 2023-03-14  8:56 Mark Geisert
  2023-03-14 11:00 ` Corinna Vinschen
  2023-07-01 14:20 ` Jon Turney
  0 siblings, 2 replies; 6+ messages in thread
From: Mark Geisert @ 2023-03-14  8:56 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Mark Geisert

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.

Fixes: 362b98b49af5 ("Cygwin: Implement CPU_SET(3) macros")

---
 winsup/cygwin/include/sys/cpuset.h | 138 ++++++++++++++++++++---------
 winsup/cygwin/release/3.4.7        |   5 ++
 2 files changed, 101 insertions(+), 42 deletions(-)
 create mode 100644 winsup/cygwin/release/3.4.7

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)
diff --git a/winsup/cygwin/release/3.4.7 b/winsup/cygwin/release/3.4.7
new file mode 100644
index 000000000..eba5de473
--- /dev/null
+++ b/winsup/cygwin/release/3.4.7
@@ -0,0 +1,5 @@
+Bug Fixes
+---------
+
+Fix CPU_SET(3) macro type mismatch by making the macros type-safe.
+Addresses https://cygwin.com/pipermail/cygwin/2023-March/253220.html
-- 
2.39.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
  2023-03-14  8:56 [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h Mark Geisert
@ 2023-03-14 11:00 ` Corinna Vinschen
  2023-07-01 14:20 ` Jon Turney
  1 sibling, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2023-03-14 11:00 UTC (permalink / raw)
  To: cygwin-patches

On Mar 14 01:56, Mark Geisert wrote:
> 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.
> 
> Fixes: 362b98b49af5 ("Cygwin: Implement CPU_SET(3) macros")

Pushed.


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
  2023-03-14  8:56 [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h Mark Geisert
  2023-03-14 11:00 ` Corinna Vinschen
@ 2023-07-01 14:20 ` Jon Turney
  2023-07-01 15:21   ` Brian Inglis
  1 sibling, 1 reply; 6+ messages in thread
From: Jon Turney @ 2023-07-01 14:20 UTC (permalink / raw)
  To: Mark Geisert, Cygwin Patches

On 14/03/2023 08:56, Mark Geisert wrote:
> 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.
> 
> Fixes: 362b98b49af5 ("Cygwin: Implement CPU_SET(3) macros")
> 

There's been a couple of reports that this leads to compilation failures 
when this header is included in -std=c89 mode.

Solutions are probably something like:

* Use __inline__ rather than inline
* Don't use initial declaration inside the for loop's init-statement

e.g. https://github.com/tinyproxy/tinyproxy/issues/499


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
  2023-07-01 14:20 ` Jon Turney
@ 2023-07-01 15:21   ` Brian Inglis
  2023-07-02 22:05     ` Mark Geisert
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Inglis @ 2023-07-01 15:21 UTC (permalink / raw)
  To: cygwin-patches

On 2023-07-01 08:20, Jon Turney wrote:
> On 14/03/2023 08:56, Mark Geisert wrote:
>> 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.
>>
>> Fixes: 362b98b49af5 ("Cygwin: Implement CPU_SET(3) macros")

> There's been a couple of reports that this leads to compilation failures when 
> this header is included in -std=c89 mode.
> Solutions are probably something like:
> * Use __inline__ rather than inline
> * Don't use initial declaration inside the for loop's init-statement
> e.g. https://github.com/tinyproxy/tinyproxy/issues/499

/usr/include/sys/cdefs.h appears to support using __inline instead of __inline__ 
or inline, and is included many places __inline is used: it appears to be 
necessary, but may not be sufficient.

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retirer     but when there is no more to cut
                                 -- Antoine de Saint-Exupéry

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
  2023-07-01 15:21   ` Brian Inglis
@ 2023-07-02 22:05     ` Mark Geisert
  2023-07-03  6:15       ` Mark Geisert
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Geisert @ 2023-07-02 22:05 UTC (permalink / raw)
  To: cygwin-patches

Hi all,

Brian Inglis wrote:
> On 2023-07-01 08:20, Jon Turney wrote:
>> On 14/03/2023 08:56, Mark Geisert wrote:
>>> 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.
>>>
>>> Fixes: 362b98b49af5 ("Cygwin: Implement CPU_SET(3) macros")
> 
>> There's been a couple of reports that this leads to compilation failures when 
>> this header is included in -std=c89 mode.
>> Solutions are probably something like:
>> * Use __inline__ rather than inline
>> * Don't use initial declaration inside the for loop's init-statement
>> e.g. https://github.com/tinyproxy/tinyproxy/issues/499
> 
> /usr/include/sys/cdefs.h appears to support using __inline instead of __inline__ 
> or inline, and is included many places __inline is used: it appears to be 
> necessary, but may not be sufficient.

Thanks for the report and investigations.  I'll address this shortly.

..mark

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h
  2023-07-02 22:05     ` Mark Geisert
@ 2023-07-03  6:15       ` Mark Geisert
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Geisert @ 2023-07-03  6:15 UTC (permalink / raw)
  To: cygwin-patches

Mark Geisert wrote:
[...]
> Thanks for the report and investigations.  I'll address this shortly.

A candidate patch for 3.4.7 is incoming.  If it passes muster I'll fire off a 
patch for 3.3.6.  I don't know why I'm using military terminology.  Independence 
Day soon in the US, I guess.
Cheers,

..mark

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-03  6:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14  8:56 [PATCH v2] Cygwin: Fix type mismatch on sys/cpuset.h Mark Geisert
2023-03-14 11:00 ` Corinna Vinschen
2023-07-01 14:20 ` Jon Turney
2023-07-01 15:21   ` Brian Inglis
2023-07-02 22:05     ` Mark Geisert
2023-07-03  6:15       ` Mark Geisert

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).