Skip to content

Commit 8412eef

Browse files
authored
Merge pull request #5652 from tautschnig/fix-cas
__atomic_compare_exchange{,_n}: correctly handle not-equal case
2 parents a9a3644 + 186a620 commit 8412eef

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

regression/cbmc/atomic_load_store-1/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ int main()
1515
assert(__atomic_exchange_n(&x, 42, 0) == x_before);
1616
__atomic_exchange(&x, &v, &x_before, 0);
1717

18+
int v2 = x == 0;
19+
assert(!__atomic_compare_exchange_n(&x, &v2, 42, 0, 0, 0));
20+
assert(v2 == x);
21+
1822
assert(__atomic_compare_exchange_n(&x, &v, 42, 0, 0, 0));
1923
v = 42;
2024
assert(__atomic_compare_exchange(&x, &v, &v, 0, 0, 0));

src/ansi-c/c_typecheck_gcc_polymorphic_builtins.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,17 +1166,20 @@ static void instantiate_atomic_compare_exchange(
11661166
source_location}};
11671167
success_fence.add_source_location() = source_location;
11681168

1169+
code_assignt assign_not_equal{dereference_exprt{parameter_exprs[1]},
1170+
deref_ptr};
1171+
assign_not_equal.add_source_location() = source_location;
11691172
code_expressiont failure_fence{side_effect_expr_function_callt{
11701173
symbol_exprt::typeless("__atomic_thread_fence"),
11711174
{parameter_exprs[5]},
11721175
typet{},
11731176
source_location}};
11741177
failure_fence.add_source_location() = source_location;
11751178

1176-
block.add(
1177-
code_ifthenelset{result,
1178-
code_blockt{{std::move(assign), std::move(success_fence)}},
1179-
std::move(failure_fence)});
1179+
block.add(code_ifthenelset{
1180+
result,
1181+
code_blockt{{std::move(assign), std::move(success_fence)}},
1182+
code_blockt{{std::move(assign_not_equal), std::move(failure_fence)}}});
11801183

11811184
block.add(code_expressiont{side_effect_expr_function_callt{
11821185
symbol_exprt::typeless(CPROVER_PREFIX "atomic_end"),

0 commit comments

Comments
 (0)