Skip to contentSkip to conversation

scripts/sorttable: fix orc_sort_cmp() to maintain symmetry and transitivity

PATCH In review Kuan-Wei Chiu 2 participants 2 messages Jan 6, 2025 – Jan 7, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/sorttable: fix orc_sort_cmp() to maintain symmetry and transitivity

The orc_sort_cmp() function, used with qsort(), previously violated the symmetry and transitivity rules required by the C standard. Specifically, when both entries are ORC_REG_UNDEFINED, it could result in both a < b and b < a, which breaks the required symmetry and transitivity. This can lead to undefined behavior and incorrect sorting results, potentially causing memory corruption in glibc implementations [1].

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

Fix the comparison logic to return 0 when both entries are ORC_REG_UNDEFINED, ensuring compliance with qsort() requirements.

Fixes 57fa18994285 ("scripts/sorttable: Implement build-time ORC unwind table sorting")
Fixes fb799447ae29 ("x86,objtool: Split UNWIND_HINT_EMPTY in two")
Cc Ching-Chun (Jim) Huang <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Shile Zhang <[email protected]>
Cc Steven Rostedt <[email protected]>
(cherry picked from commit 0210d251162f4033350a94a43f95b1c39ec84a90)
---
 scripts/sorttable.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/sorttable.h +5 -1
index a2baa2fefb13..9013b6984d68 100644
@@ -103,7 +103,7 @@ static inline unsigned long orc_ip(const int *ip)
 
 static int orc_sort_cmp(const void *_a, const void *_b)
 {
-	struct orc_entry *orc_a;
+	struct orc_entry *orc_a, *orc_b;
 	const int *a = g_orc_ip_table + *(int *)_a;
 	const int *b = g_orc_ip_table + *(int *)_b;
 	unsigned long a_val = orc_ip(a);
@@ -121,6 +121,10 @@ static int orc_sort_cmp(const void *_a, const void *_b)
 	 * whitelisted .o files which didn't get objtool generation.
 	 */
 	orc_a = g_orc_table + (a - g_orc_ip_table);
+	orc_b = g_orc_table + (b - g_orc_ip_table);
+	if (orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end &&
+	    orc_b->sp_reg == ORC_REG_UNDEFINED && !orc_b->end)
+		return 0;
 	return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw

[ Sasha's backport helper bot ]

Hi,

Found matching upstream commit: 0210d251162f4033350a94a43f95b1c39ec84a90

Status in newer kernel trees:
6.12.y | Not found
6.6.y | Not found
6.1.y | Not found
5.15.y | Not found

Note: The patch differs from the upstream commit:
---
1: 0210d251162f ! 1: 02225d4eea29 scripts/sorttable: fix orc_sort_cmp() to maintain symmetry and transitivity
@@ Commit message

The orc_sort_cmp() function, used with qsort(), previously violated the symmetry and transitivity rules required by the C standard. Specifically,
- when both entries are ORC_TYPE_UNDEFINED, it could result in both a < b
+ when both entries are ORC_REG_UNDEFINED, it could result in both a < b
and b < a, which breaks the required symmetry and transitivity. This can lead to undefined behavior and incorrect sorting results, potentially causing memory corruption in glibc implementations [1].
@@ Commit message
Transitivity: If x < y and y < z, then x < z.

Fix the comparison logic to return 0 when both entries are
- ORC_TYPE_UNDEFINED, ensuring compliance with qsort() requirements.
+ ORC_REG_UNDEFINED, ensuring compliance with qsort() requirements.

@@ Commit message

+ (cherry picked from commit 0210d251162f4033350a94a43f95b1c39ec84a90)
+ Signed-off-by: Kuan-Wei Chiu <[email protected]>

## scripts/sorttable.h ##
@@ scripts/sorttable.h: static inline unsigned long orc_ip(const int *ip)
@@ scripts/sorttable.h: static int orc_sort_cmp(const void *_a, const void *_b)
*/
orc_a = g_orc_table + (a - g_orc_ip_table);
+ orc_b = g_orc_table + (b - g_orc_ip_table);
-+ if (orc_a->type == ORC_TYPE_UNDEFINED && orc_b->type == ORC_TYPE_UNDEFINED)
++ if (orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end &&
++ orc_b->sp_reg == ORC_REG_UNDEFINED && !orc_b->end)
+ return 0;
- return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1;
+ return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
}

---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-5.15.y       |  Success    |  Success   |