Skip to content

Commit 0210d25

Browse files
visitorckwakpm00
authored andcommitted
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_TYPE_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_TYPE_UNDEFINED, ensuring compliance with qsort() requirements. Link: https://www.qualys.com/2024/01/30/qsort.txt [1] Link: https://lkml.kernel.org/r/20241226140332.2670689-1-visitorckw@gmail.com Fixes: 57fa189 ("scripts/sorttable: Implement build-time ORC unwind table sorting") Fixes: fb79944 ("x86,objtool: Split UNWIND_HINT_EMPTY in two") Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Cc: Ching-Chun (Jim) Huang <jserv@ccns.ncku.edu.tw> Cc: <chuang@cs.nycu.edu.tw> Cc: Ingo Molnar <mingo@kernel.org> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Shile Zhang <shile.zhang@linux.alibaba.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent dd2a5b5 commit 0210d25

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

scripts/sorttable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static inline unsigned long orc_ip(const int *ip)
110110

111111
static int orc_sort_cmp(const void *_a, const void *_b)
112112
{
113-
struct orc_entry *orc_a;
113+
struct orc_entry *orc_a, *orc_b;
114114
const int *a = g_orc_ip_table + *(int *)_a;
115115
const int *b = g_orc_ip_table + *(int *)_b;
116116
unsigned long a_val = orc_ip(a);
@@ -128,6 +128,9 @@ static int orc_sort_cmp(const void *_a, const void *_b)
128128
* whitelisted .o files which didn't get objtool generation.
129129
*/
130130
orc_a = g_orc_table + (a - g_orc_ip_table);
131+
orc_b = g_orc_table + (b - g_orc_ip_table);
132+
if (orc_a->type == ORC_TYPE_UNDEFINED && orc_b->type == ORC_TYPE_UNDEFINED)
133+
return 0;
131134
return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1;
132135
}
133136

0 commit comments

Comments
 (0)