<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/73578>73578</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            HWAddressSantizer header needs a function to generate tags for end-users
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          spectecjr
      </td>
    </tr>
</table>

<pre>
    To simplify adding HWASan support to custom allocators, particularly on architectures which have specific requirements surrounding the implementation/type of tags, it would be useful if `hwasan_interface.h` had a function to generate a unique memory tag from a pointer (maybe XOR'd with the current CPU cycle counter value to reduce the chance of ABA errors/use after free).

The implementation might look something like the following pseudocode:

```cpp
#if ARM
inline unsigned char __hwasan_generate_tag(const volatile void* x)
{
    // ARM64 MTE tags are only 4-bits
    // Use current CPU counter as an entropy-source.
    return (unsigned char)( (uint64_t)x  ^ READ_CYCLECOUNTER_INTRINSIC() ) & 0x0F;
}
#else
    return (unsigned char)( (uint64_t)x  ^ READ_CYCLECOUNTER_INTRINSIC() ) & 0xFF;
#endif
```

Adding this to the API would have the benefit of both satisfying any current or future hardware requirements portably and with forward compatibility, as well as reducing the confusion for new devs on how to generate a suitable tag. (Most devs for the existing API will probably shift and mask the lower bits of the pointer, which isn't a robust solution given the aforementioned issues).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VV1v2zoP_jXKDdHAUZyvi1ykaYMVeNcN_cDecxXIEhVrlSVPlJL6_PoDKenWdTi3B0jiWCb5kA_50ILIHBzims2u2exmJFJsfVhTjzKi_B5GjVfD-skDma63Rg8glDLuAJ--bR6FA0p970OE6EEmir4DYa2XIvpAjG-hFyEamawIdgDvQATZmogypoAEp9bIFlpxRMiIRhsJAX8kE7BDFwkoheCTK4ixRchJlEciGu8Y38WhR_AaojgUPBPh5JNV0CAkQp0sGA1sXrUnQcLtjYsYtJ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