Skip to contentSkip to conversation

perf bench: Fix undefined behavior in cmpworker()

PATCH Superseded Kuan-Wei Chiu 2 participants 3 messages Jan 7, 2025 – Jan 16, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf bench: Fix undefined behavior in cmpworker()

The comparison function cmpworker() violates the C standard's requirements for qsort() comparison functions, which mandate symmetry and transitivity:

Symmetry: If x < y, then y > x.
Transitivity: If x < y and y < z, then x < z.

In its current implementation, cmpworker() incorrectly returns 0 when w1->tid < w2->tid, which breaks both symmetry and transitivity. This violation causes undefined behavior, potentially leading to issues such as memory corruption in glibc [1].

Fix the issue by returning -1 when w1->tid < w2->tid, ensuring compliance with the C standard and preventing undefined behavior.

Fixes 121dd9ea0116 ("perf bench: Add epoll parallel epoll_wait benchmark")

---
Changes in v2:
- Rewrite commit message

 tools/perf/bench/epoll-wait.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/bench/epoll-wait.c +1 -1
index ef5c4257844d..4868d610e9bf 100644
@@ -420,7 +420,7 @@ static int cmpworker(const void *p1, const void *p2)
 
 	struct worker *w1 = (struct worker *) p1;
 	struct worker *w2 = (struct worker *) p2;
-	return w1->tid > w2->tid;
+	return w1->tid > w2->tid ? 1 : -1;
 }
 
 int bench_epoll_wait(int argc, const char **argv)
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 31 lines · Kuan-Wei Chiu

The comparison function cmpworker() violates the C standard's requirements for qsort() comparison functions, which mandate symmetry and transitivity:

Symmetry: If x < y, then y > x.
Transitivity: If x < y and y < z, then x < z.

In its current implementation, cmpworker() incorrectly returns 0 when w1->tid < w2->tid, which breaks both symmetry and transitivity. This violation causes undefined behavior, potentially leading to issues such as memory corruption in glibc [1].

Fix the issue by returning -1 when w1->tid < w2->tid, ensuring compliance with the C standard and preventing undefined behavior.

Fixes 121dd9ea0116 ("perf bench: Add epoll parallel epoll_wait benchmark")

---
Changes in v2:
- Rewrite commit message

  tools/perf/bench/epoll-wait.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/bench/epoll-wait.c +0 -0
index ef5c4257844d..4868d610e9bf 100644

tools/perf/bench/epoll-wait.c static int cmpworker(const void *p1, const void *p2) · line 423

Quoted 5 lines
tools/perf/bench/epoll-wait.c +1 -1
@@ -420,7 +420,7 @@ static int cmpworker(const void *p1, const void *p2)
 
  	struct worker *w1 = (struct worker *) p1;
  	struct worker *w2 = (struct worker *) p2;
-	return w1->tid > w2->tid;
+	return w1->tid > w2->tid ? 1 : -1;

I suppose you can skip the 0 for equality because you know that no two tids are the same?

Anyone looking at this in the future might still think it's still wrong unless it does the full comparison. Even if it's not technically required I would write it like a "normal" one now that we're here:

  if (w1->tid > w2->tid) return 1;
  if (w1->tid < w2->tid) return -1;
  return 0;
#2 lore raw
Quoted 29 lines · James Clark
Quoted 26 lines · Kuan-Wei Chiu

The comparison function cmpworker() violates the C standard's requirements for qsort() comparison functions, which mandate symmetry and transitivity:

Symmetry: If x < y, then y > x.
Transitivity: If x < y and y < z, then x < z.

In its current implementation, cmpworker() incorrectly returns 0 when w1->tid < w2->tid, which breaks both symmetry and transitivity. This violation causes undefined behavior, potentially leading to issues such as memory corruption in glibc [1].

Fix the issue by returning -1 when w1->tid < w2->tid, ensuring compliance with the C standard and preventing undefined behavior.

Fixes 121dd9ea0116 ("perf bench: Add epoll parallel epoll_wait benchmark")

---
Changes in v2:
- Rewrite commit message

  tools/perf/bench/epoll-wait.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

tools/perf/bench/epoll-wait.c static int cmpworker(const void *p1, const void *p2) · line 422

Quoted 9 lines
tools/perf/bench/epoll-wait.c +1 -1
index ef5c4257844d..4868d610e9bf 100644
@@ -420,7 +420,7 @@ static int cmpworker(const void *p1, const void *p2)
  	struct worker *w1 = (struct worker *) p1;
  	struct worker *w2 = (struct worker *) p2;
-	return w1->tid > w2->tid;
+	return w1->tid > w2->tid ? 1 : -1;

I suppose you can skip the 0 for equality because you know that no two tids are the same?

Yes, exactly.

Quoted 8 lines

Anyone looking at this in the future might still think it's still wrong unless it does the full comparison. Even if it's not technically required I would write it like a "normal" one now that we're here:

  if (w1->tid > w2->tid) return 1;
  if (w1->tid < w2->tid) return -1;
  return 0;

Sure. I'll make that change in v3.

Regards,
Kuan-Wei