1 // RUN: %clang_hwasan %s -o %t
5 // REQUIRES: pointer-tagging
7 #include <sanitizer/hwasan_interface.h>
13 int main(int argc
, char **argv
) {
14 const size_t kPS
= sysconf(_SC_PAGESIZE
);
15 const int kSize
= kPS
/ atoi(argv
[1]);
18 // Get any mmaped pointer.
20 mmap(0, kSize
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
21 if (r
== MAP_FAILED
) {
22 perror("Failed to mmap: ");
26 // Check that the pointer is untagged.
27 if (r
!= __hwasan_tag_pointer(r
, 0)) {
28 fprintf(stderr
, "Pointer returned by mmap should be untagged.\n");
32 // Manually tag the pointer and the memory.
33 __hwasan_tag_memory(r
, kTag
, kPS
);
34 int *p1
= __hwasan_tag_pointer(r
, kTag
);
36 // Check that the pointer and the tag match.
37 if (__hwasan_test_shadow(p1
, kPS
) != -1) {
38 fprintf(stderr
, "Failed to tag.\n");
42 if (munmap((char *)r
+ 1, kSize
) == 0) {
43 perror("munmap should fail: ");
47 if (__hwasan_test_shadow(p1
, kPS
) != -1) {
48 fprintf(stderr
, "Still must be tagged.\n");
52 // First munmmap and then mmap the same pointer using MAP_FIXED.
53 if (munmap((char *)r
, kSize
) != 0) {
54 perror("Failed to unmap: ");
58 if (__hwasan_test_shadow(r
, kPS
) != -1) {
59 fprintf(stderr
, "Shadow memory was not cleaned by munmap.\n");
62 __hwasan_tag_memory(r
, kTag
, kPS
);
63 int *p2
= (int *)mmap(r
, kSize
, PROT_READ
| PROT_WRITE
,
64 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
66 // Check that the pointer has no tag in it.
68 fprintf(stderr
, "The mmap pointer has a non-zero tag in it.\n");
72 // Make sure we can access the shadow with an untagged pointer.
73 if (__hwasan_test_shadow(p2
, kPS
) != -1) {
74 fprintf(stderr
, "Shadow memory was not cleaned by mmap.\n");