Skip to content

Commit

Permalink
selftests/rseq: Fix: work-around asm goto compiler bugs
Browse files Browse the repository at this point in the history
commit b53823f upstream.

gcc and clang each have their own compiler bugs with respect to asm
goto. Implement a work-around for compiler versions known to have those
bugs.

gcc prior to 4.8.2 miscompiles asm goto.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670

gcc prior to 8.1.0 miscompiles asm goto at O1.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103908

clang prior to version 13.0.1 miscompiles asm goto at O2.
llvm/llvm-project#52735

Work around these issues by adding a volatile inline asm with
memory clobber in the fallthrough after the asm goto and at each
label target.  Emit this for all compilers in case other similar
issues are found in the future.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20220124171253.22072-14-mathieu.desnoyers@efficios.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
compudj authored and gregkh committed Jul 7, 2022
1 parent 7e1a0a9 commit a4312e2
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 6 deletions.
30 changes: 30 additions & 0 deletions tools/testing/selftests/rseq/compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: LGPL-2.1-only OR MIT */
/*
* rseq/compiler.h
*
* Work-around asm goto compiler bugs.
*
* (C) Copyright 2021 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*/

#ifndef RSEQ_COMPILER_H
#define RSEQ_COMPILER_H

/*
* gcc prior to 4.8.2 miscompiles asm goto.
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
*
* gcc prior to 8.1.0 miscompiles asm goto at O1.
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103908
*
* clang prior to version 13.0.1 miscompiles asm goto at O2.
* https://github.com/llvm/llvm-project/issues/52735
*
* Work around these issues by adding a volatile inline asm with
* memory clobber in the fallthrough after the asm goto and at each
* label target. Emit this for all compilers in case other similar
* issues are found in the future.
*/
#define rseq_after_asm_goto() asm volatile ("" : : : "memory")

#endif /* RSEQ_COMPILER_H_ */
39 changes: 39 additions & 0 deletions tools/testing/selftests/rseq/rseq-arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -263,16 +268,21 @@ int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -317,12 +327,15 @@ int rseq_addv(intptr_t *v, intptr_t count, int cpu)
, error1
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
#endif
}
Expand Down Expand Up @@ -384,16 +397,21 @@ int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -456,16 +474,21 @@ int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -532,18 +555,24 @@ int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
, error1, error2, error3
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("1st expected value comparison failed");
error3:
rseq_after_asm_goto();
rseq_bug("2nd expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -652,16 +681,21 @@ int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -771,16 +805,21 @@ int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down
45 changes: 39 additions & 6 deletions tools/testing/selftests/rseq/rseq-arm64.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,21 @@ int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
, error1, error2
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -300,16 +304,21 @@ int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
, error1, error2
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -348,12 +357,15 @@ int rseq_addv(intptr_t *v, intptr_t count, int cpu)
, error1
#endif
);
rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
#endif
}
Expand Down Expand Up @@ -402,17 +414,21 @@ int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -461,17 +477,21 @@ int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -522,19 +542,24 @@ int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
, error1, error2, error3
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
error3:
rseq_after_asm_goto();
rseq_bug("2nd expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -584,17 +609,21 @@ int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down Expand Up @@ -644,17 +673,21 @@ int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
, error1, error2
#endif
);

rseq_after_asm_goto();
return 0;
abort:
rseq_after_asm_goto();
RSEQ_INJECT_FAILED
return -1;
cmpfail:
rseq_after_asm_goto();
return 1;
#ifdef RSEQ_COMPARE_TWICE
error1:
rseq_after_asm_goto();
rseq_bug("cpu_id comparison failed");
error2:
rseq_after_asm_goto();
rseq_bug("expected value comparison failed");
#endif
}
Expand Down

0 comments on commit a4312e2

Please sign in to comment.