Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.arch / sparc64-adi.c
blob97f6a99e54691f713ee384017013557397284738
1 /* Application Data Integrity (ADI) test in sparc64.
3 Copyright 2017-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <pthread.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <sys/errno.h>
28 #include <sys/utsname.h>
29 #include <sys/param.h>
30 #include <malloc.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <sys/shm.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <poll.h>
38 #include <setjmp.h>
39 #include "adi.h"
41 #define ONEKB 1024
42 #define PAT 0xdeadbeaf
44 #define MAPSIZE 8192
45 #define SHMSIZE 102400
46 #ifndef PROT_ADI
47 #define PROT_ADI 0x10
48 #endif
50 static int
51 memory_fill (char *addr, size_t size, int pattern)
53 long *aligned_addr = (long *) addr;
54 long i;
55 for (i = 0; i < size / sizeof (long); i += ONEKB)
57 *aligned_addr = pattern;
58 aligned_addr = aligned_addr + ONEKB;
60 return (0);
63 int main ()
65 char *haddr;
66 caddr_t vaddr;
67 int version;
69 /* Test ISM. */
70 int shmid = shmget (IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
71 if (shmid == -1)
72 exit(1);
73 char *shmaddr = (char *)shmat (shmid, NULL, 0x666 | SHM_RND);
74 if (shmaddr == (char *)-1)
76 shmctl (shmid, IPC_RMID, NULL);
77 exit(1);
79 /* Enable ADI on ISM segment. */
80 if (mprotect (shmaddr, SHMSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
82 perror ("mprotect failed");
83 goto err_out;
85 if (memory_fill (shmaddr, SHMSIZE, PAT) != 0) /* line breakpoint here */
87 exit(1);
89 adi_clr_version (shmaddr, SHMSIZE);
90 caddr_t vshmaddr = adi_set_version (shmaddr, SHMSIZE, 0x8);
91 if (vshmaddr == 0)
92 exit(1);
93 /* Test mmap. */
94 int fd = open ("/dev/zero", O_RDWR);
95 if (fd < 0)
96 exit(1);
97 char *maddr = (char *)mmap (NULL, MAPSIZE, PROT_READ|PROT_WRITE,
98 MAP_PRIVATE, fd, 0);
99 if (maddr == (char *)-1)
100 exit(1);
101 /* Enable ADI. */
102 if (mprotect (shmaddr, MAPSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
104 perror ("mprotect failed");
105 goto err_out;
108 if (memory_fill (maddr, MAPSIZE, PAT) != 0)
109 exit(1);
110 caddr_t vmaddr = adi_set_version (maddr, MAPSIZE, 0x8);
112 /* Test heap. */
113 haddr = (char*) memalign (MAPSIZE, MAPSIZE);
114 /* Enable ADI. */
115 if (mprotect (shmaddr, MAPSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
117 perror ("mprotect failed");
118 goto err_out;
121 if (memory_fill (haddr, MAPSIZE, PAT) != 0)
122 exit(1);
123 adi_clr_version (haddr, MAPSIZE);
124 /* Set some ADP version number. */
125 caddr_t vaddr1, vaddr2, vaddr3, vaddr4;
126 vaddr = adi_set_version (haddr, 64*2, 0x8);
127 vaddr1 = adi_set_version (haddr+64*2, 64*2, 0x9);
128 vaddr2 = adi_clr_version (haddr+64*4, 64*2);
129 vaddr3 = adi_set_version (haddr+64*6, 64*2, 0xa);
130 vaddr4 = adi_set_version (haddr+64*8, 64*10, 0x3);
131 if (vaddr == 0)
132 exit(1);
133 char *versioned_p = vaddr;
134 *versioned_p = 'a';
135 char *uvp = haddr; // unversioned pointer
136 *uvp = 'b'; // version mismatch trap
138 return (0);
139 err_out:
140 if (shmdt ((const void *)shmaddr) != 0)
141 perror ("Detach failure");
142 shmctl (shmid, IPC_RMID, NULL);
143 exit (1);