Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[herd] Implement Aarch64 instruction stz2g #763

Merged
merged 3 commits into from
Feb 9, 2024
Merged

Conversation

maranget
Copy link
Member

@maranget maranget commented Jan 23, 2024

Adding the instruction revealed some shortcomings of the current MTE implementation. In particular, it is no longer realistic to assume that a complete array will never span over more than one granule, as the STZ2G instruction sets two granules.

The implementation is correct only in mixed-size mode, it is then an error to execute STZG (resp. STZ2G) on a memory location whose size is strictly less than one (resp. two) granules.

For instance, with the current granule size of 16 bytes, herd will fail on the following test:

AAArch64 TooSmall
Variant=mixed,mte
{
int x=1;
0:X1=x:green;
0:X2=x:red;
}
  P0           ;
 STZG X2,[X1]  ;
forall x=0

We have:

% herd7 TooSmall.litmus
Warning: File "TooSmall.litmus": Unaligned or out-of-bound access: x, 16 bytes (User error)

To reach granule size one has to declare an array:

AArch64 LargeEnough
Variant=mixed,mte
{
int64_t x[3] = {1,2,3};
0:X1=x:green;
0:X2=x:red;
}
  P0           ;
 STZG X2,[X1]  ;
forall x={0,0,3}

Now, the test is accepted, yielding the final values of zero for x[0] and x[1].

For backward compatibility STZG still operates in non-mixed mode, with the expected result of zeroing non-array locations, which is correct assuming that:

  1. the size of non-array locations is less than one granule, and
  2. different locations occupy different granules.

Finally, herd will fail, flagging an error, on tests that both have some array location and some STZG instruction, regardless of whether STZG touches some array or not.

@maranget maranget force-pushed the aarch64-stz2g branch 7 times, most recently from 0fa1147 to 051c3f5 Compare January 25, 2024 13:24
@maranget maranget marked this pull request as ready for review January 25, 2024 13:25
@maranget
Copy link
Member Author

Hi @relokin, this PR much elaborates from your work on MTE (In particular PR #695). If you have some time...

Copy link
Member

@relokin relokin left a comment

Choose a reason for hiding this comment

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

This is very good thanks Luc! I've done a quick review added a couple of comments and I will try and test it tomorrow as well.

STZG X2,[X1] ;
MOV W0,#7 ;
STR W0,[X2] ;
STR W0,[X1,#20] ;
Copy link
Member

Choose a reason for hiding this comment

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

It's a little weird that we allow an unchecked STR to x when previously we had a checked STR x. It makes sense to allow unchecked locations and checked locations but mixed uses of the same location is not very realistic. But this might not be in the scope for this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at L02 did you mean this to be:

Suggested change
STR W0,[X1,#20] ;
STR W0,[X2,#20] ;

Copy link
Member Author

@maranget maranget Jan 26, 2024

Choose a reason for hiding this comment

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

I do not understand your remark on "unchecked" STR. Which STR are checked or unchecked ?

As to STR W0,[X1,#20], this is what I intended. As I understand it, both STR succeeds the first one STR W0,[X2] stores into the granule whose color has changed to :red, while the second STR W0,[X1,#20] stores into the next granule, whose color has not changed and hence stayed :green. Assuming a granule size of 16 bytes.

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry, do we assume a default tag (color) for symbolic addresses as well? Meaning x is assumed to be x:green? I was under the impression we only had default values for tag locations (tag(x)).

MTE doesn’t require that all memory instructions perform a tag check. There are cases where the instruction doesn’t perform a tag check (for example stg doesn't check or a load y where y is mapped as Normal Memory and not TaggedNormal doesn’t check either). So I thought, so without checking the code, that if a symbolic address didn’t specify a colour it would signal to herd to slide the tag check.

Copy link
Member Author

Choose a reason for hiding this comment

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

As I understand, all locations have a tag and this tag is green by default. In other words, all locations have the TaggedNormal attribute. Do we understand each other?

Copy link
Member

@relokin relokin Jan 28, 2024

Choose a reason for hiding this comment

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

OK, I think I understand now. In -variant mte:

  • If tag(x) is not explicitly initialised then, herd7 will initialise to `:green"
  • If a register is initialised with a symbolic location which doesn't specify a tag (for example 0:X0=x), herd7 will assume that a default tag :green for this symbolic address (i.e. 0:X0=x:green).
  • All locations are assumed to be mapped as TaggedNormal

and now I also understand why L03 is correctly specified.

herd/tests/instructions/AArch64.MTE/L03.litmus Outdated Show resolved Hide resolved
@@ -0,0 +1,16 @@
AArch64 L05
Copy link
Member

Choose a reason for hiding this comment

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

It would be really good if herd7 could ignore the size of STZG for L03 but throw an error for L05. But I am not sure this is possible. So I guess you're suggesting that STZG should require mixed mode don't you?

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean throw an error when the target location of STZG is an array in non-mixed mode?

Copy link
Member

Choose a reason for hiding this comment

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

I think so yes. I think it would be useful to allow herd7 to work without enabling -variant mixed in cases like L03 after all, L03 is not incorrect (the user didn't check x[1] they're only checking x aka x[0]). But L05 is wrong so they can't trust the result and they should enable -variant mixed. What do you think? Is it possible?

Copy link
Member Author

@maranget maranget Jan 31, 2024

Choose a reason for hiding this comment

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

Your solution implies looking at several places in the execution: (1) apply STZG to an array location and (2) reading some array cell different from the first.

I'd favor a solution that would look at the STZG write only. Then if we can check that STZG base address is an array address, we can:

  1. Fail with a message that suggests using -variant mixed, or
  2. Fill all the array cells with zero, (up to granule size ?)

Also, if the address is not known (ie not read from a register initial state) 1. applies systematically.

A more simple but less expressive solution would be to fail, still suggesting -variant mixed when we both have some array and STZG instruction in the same test.

Copy link
Member

Choose a reason for hiding this comment

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

A more simple but less expressive solution would be to fail, still suggesting -variant mixed when we both have some array and STZG instruction in the same test.

I think that's a very good middle ground. But in any case don't worry too much if it's too hard we can fix in a subsequent PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have finally implemented this middle ground, as it is relatively straightforward.

herd/eventsMonad.ml Outdated Show resolved Hide resolved
Copy link
Member

@relokin relokin left a comment

Choose a reason for hiding this comment

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

LGTM. Very nice change, looking forward to see it merged!

Adding this instruction revealed some shortcomings of
the current MTE implementation.
In particular it is no longer realistic to assume
that a complete array will never span over more
than one granule, as the STZ2G instruction sets two
granules.

The STZG instruction writes zero into one granule
and updates its tag location, while the STG2Z
instruction writes zero into two granules and updates
two tag locations, some changes are required.

To account for STZ2G write of two tag locations, we
change the previous hypothesis that every location
reside in one granule.

1. An array can now span over several granules
2. We accept size mismatches for the write
   of zero of STGZ. As a consequence, in non-mixed size
   (default) mode, STGZ will zero  any non-array location,
   whatever its size., which is correct as long as the
   size of the location is less than one granule.
   For arrays, only the first item would be zero-ed.
   Thus, having both arrays and STGZ in the same test
   triggers a "user error" failure. However,
   the instruction operates as expected
   in mixed-size mode (`-variant mixed`).
3. Mixed-size mode is mandatory for executing STZ2G, and
   for executing tests with some array location
   and some STZG instruction.

Notice: granule size is currently fixed at G=16 bytes. If necessary,
command-line configuration can be implemented in the future.

Notation: For a base address x, successive tag addresses are written
tag(x), tag(x+G) , tag(x+2G), etc. Said otherwise, the tag address of
(byte) location x+o is tag(x+(o*G)/G).

Notice: In vmsa mode, since PR #695, tag addresses are based upon
physical addresses and will appear as tag(PA(x)), tag(PA(x)+G),...
@maranget
Copy link
Member Author

maranget commented Feb 9, 2024

LGTM. Very nice change, looking forward to see it merged!

@relokin, merging on its way. Thanks for your review.

@maranget maranget merged commit b95a692 into master Feb 9, 2024
@maranget maranget deleted the aarch64-stz2g branch February 9, 2024 12:15
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.

2 participants