Skip to content

[herd,cat] Fix some problems with the C architecture#1912

Merged
maranget merged 3 commits into
masterfrom
fix-some-cat-primitives
Jul 13, 2026
Merged

[herd,cat] Fix some problems with the C architecture#1912
maranget merged 3 commits into
masterfrom
fix-some-cat-primitives

Conversation

@maranget

@maranget maranget commented Jul 10, 2026

Copy link
Copy Markdown
Member

This PR fixes two problems:

  1. Since PR [herd] Petty optimisation of the Cat interpreter #1855, the polymorphic value V.Empty circulates more. Some primitives were performing set and relation normalisation by themselves, overlooking the cases of empty and universe. We fix this by using the already present and complete normalisation functions as_set and as_rel slightly generalised to handle different error reporting.
  2. PR [herd] Split exclusive accesses from the X event set #1889 changed how the relation rmw is computed by defining it in stdlb.cat. The Cat definition of the relation rmw assumes that atomic effects are gathered in the set X, which was not the case of the C semantics. As a result the rmw relation was empty when the read-modify-write instructions are implemented by two (atomic) effects.

@maranget maranget force-pushed the fix-some-cat-primitives branch 3 times, most recently from be5585a to 13863fa Compare July 10, 2026 14:52
@akiyks

akiyks commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

The error is fixed, but I see behavior changes in LKMM litmus tests.

Litmus test:

C add_unless_mb

(*
 * Result: Never
 *
 * This litmus test demonstrates that a successful atomic_add_unless
 * acts as a full memory barrier, ensuring that *x=1 propagates to P1
 * before P1 executes *x=2.
 *)

{
	atomic_t z;
}

P0(int *x, int *y, atomic_t *z)
{
	int r0;

	WRITE_ONCE(*x, 1);
	r0 = atomic_add_unless(z,1,5);
	WRITE_ONCE(*y, 1);
}

P1(int *x, int *y)
{
	int r0;

	r0 = READ_ONCE(*y);
	if (r0 == 1)
		WRITE_ONCE(*x, 2);
}

locations [z;]
exists (1:r0=1 /\ x=1)

Expected output

Test add_unless_mb Allowed
States 2
1:r0=0; [x]=1; [z]=1;
1:r0=1; [x]=2; [z]=1;
No
Witnesses
Positive: 0 Negative: 2
Condition exists (1:r0=1 /\ [x]=1)
Observation add_unless_mb Never 0 2
Time add_unless_mb 0.00
Hash=740ecaca893f828d9079c0c827815245

Output as of fix-some-cat-primitives

Test add_unless_mb Allowed
States 3
1:r0=0; [x]=1; [z]=1;
1:r0=1; [x]=1; [z]=1;
1:r0=1; [x]=2; [z]=1;
Ok
Witnesses
Positive: 1 Negative: 2
Condition exists (1:r0=1 /\ [x]=1)
Observation add_unless_mb Sometimes 1 2
Time add_unless_mb 0.00
Hash=740ecaca893f828d9079c0c827815245

There seems to be lost orderings implied by atomic accesses.

@maranget

Copy link
Copy Markdown
Member Author

Hi @akiyks. This probably is a consequence of PR #1889. I have a look.

@fsestini

Copy link
Copy Markdown
Collaborator

Is delift({}) expected to work? With this PR, I get the following:

$ dune exec herd7 -- -set-libdir herd/libdir -model repro.cat herd/tests/instructions/AArch64/A01.litmus
File "./repro.cat", line 1, characters 6-16: primitive delift: argument mismatch

where repro.cat is:

empty delift({})

@maranget

maranget commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

Is delift({}) expected to work? With this PR, I get the following:

Nice catch! Fix on its way. It is likely that V.Empty will not occur un ClassRel context, but robustness commands fixing.

Since PR #1855, the polymorphic value empty  circulates more.
Some primitives were performing set and relation normalisation
by themselves, overlooking the cases of empty and universe.
We fix this by using the already present and complete
normalisation functions `as_set` and `as_rel`
slightly generalised to handle different error reporting.

Reported-by: Akira Yokosawa <akiyks@gmail.com>
@maranget maranget force-pushed the fix-some-cat-primitives branch from 13863fa to b41930f Compare July 10, 2026 16:26
@maranget

Copy link
Copy Markdown
Member Author

Just pushed a small fix for the ClassRel arguments of primitives. In ambiguous cases, I favored event set and relation.

@maranget

Copy link
Copy Markdown
Member Author

Commit ba788a2 should fix the add_unless_mb problem.

@maranget

maranget commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

Unfortunately, the added tests would not have caught the add_unless_mb problem because herd7 generates RMW effects for all the read-modify-write instructions of standard C (i.e. non-lkmm), while atomic_add_unless is implemented by emitting an atomic read and an atomic write.

@maranget maranget changed the title [herd,cat] Normalise the arguments of primitives [herd,cat] Fix some problem with the C architecture Jul 10, 2026
@maranget maranget changed the title [herd,cat] Fix some problem with the C architecture [herd,cat] Fix some problems with the C architecture Jul 10, 2026
@akiyks

akiyks commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Just confirmed this restores expected behavior.
Thanks!

@maranget

Copy link
Copy Markdown
Member Author

Hi @relokin and @HadrienRenaud. I am leaving next Monday for two weeks. Could we merge this PR that fixes two bugs soon?

maranget added 2 commits July 11, 2026 08:34
C models, including the linux kernel memory model, define
this set as `RMW`, however the common `stdlib.cat` file
that defines the `rmw` relation need this set to be called `X`.

Reported-by: Akira Yokosawa <akiyks@gmail.com>
@maranget maranget force-pushed the fix-some-cat-primitives branch from 8b41148 to 0b0dedc Compare July 11, 2026 06:34

@HadrienRenaud HadrienRenaud left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't claim to understand all the details nor consequences of those changes, but overall I don't see why we shouldn't merge it

@maranget

Copy link
Copy Markdown
Member Author

Thanks @HadrienRenaud. Merging.

@maranget maranget merged commit 39b82e0 into master Jul 13, 2026
5 checks passed
@maranget maranget deleted the fix-some-cat-primitives branch July 13, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants