gdb: SPARC ADI support
The M7 processor supports an Application Data Integrity (ADI) feature
that detects invalid data accesses. When software allocates data, it
chooses a 4-bit version number, sets the version in the upper 4 bits
of the 64-bit pointer to that data, and stores the 4-bit version in
every cacheline of the object. Hardware saves the latter in spare
bits in the cache and memory hierarchy. On each load and store, the
processor compares the upper 4 VA (virtual address) bits to the
cacheline's version. If there is a mismatch, the processor generates a
version mismatch trap which can be either precise or disrupting. The
trap is an error condition which the kernel delivers to the process as
a SIGSEGV signal.
The upper 4 bits of the VA represent a version and are not part of the
true address. The processor clears these bits and sign extends bit 59
to generate the true address.
Note that 32-bit applications cannot use ADI.
This patch adds ADI support in gdb which allows the user to examine
current version tags and assign new version tags in the program. It
also catches and reports precise or disrupting memory corruption
traps.
gdb/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* sparc64-tdep.h: (adi_normalize_address): New export.
* sparc-nat.h: (open_adi_tag_fd): New export.
* sparc64-linux-nat.c: (open_adi_tag_fd): New function.
* sparc64-linux-tdep.c:
(SEGV_ACCADI, SEGV_ADIDERR, SEGV_ADIPERR) New defines.
(sparc64_linux_handle_segmentation_fault): New function.
(sparc64_linux_init_abi): Register
sparc64_linux_handle_segmentation_fault
* sparc64-tdep.c: Include cli-utils.h,gdbcmd.h,auxv.h.
(sparc64_addr_bits_remove): New function.
(sparc64_init_abi): Register sparc64_addr_bits_remove.
(MAX_PROC_NAME_SIZE): New macro.
(AT_ADI_BLKSZ, AT_ADI_NBITS, AT_ADI_UEONADI) New defines.
(sparc64adilist): New variable.
(adi_proc_list): New variable.
(find_adi_info): New function.
(add_adi_info): New function.
(get_adi_info_proc): New function.
(get_adi_info): New function.
(info_adi_command): New function.
(read_maps_entry): New function.
(adi_available): New function.
(adi_normalize_address): New function.
(adi_align_address): New function.
(adi_convert_byte_count): New function.
(adi_tag_fd): New function.
(adi_is_addr_mapped): New function.
(adi_read_versions): New function.
(adi_write_versions): New function.
(adi_print_versions): New function.
(do_examine): New function.
(do_assign): New function.
(adi_examine_command): New function.
(adi_assign_command): New function.
(_initialize_sparc64_adi_tdep): New function.
gdb/doc/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* gdb.texinfo (Architectures): Add new Sparc64 section to document
ADI support.
* NEWS: Add "adi examine" and "adi assign" commands.
gdb/testsuite/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* gdb.arch/sparc64-adi.exp: New file.
* gdb.arch/sparc64-adi.c: New file.
2017-08-23 16:57:37 +08:00
|
|
|
/* Application Data Integrity (ADI) test in sparc64.
|
|
|
|
|
2018-01-01 12:43:02 +08:00
|
|
|
Copyright 2017-2018 Free Software Foundation, Inc.
|
gdb: SPARC ADI support
The M7 processor supports an Application Data Integrity (ADI) feature
that detects invalid data accesses. When software allocates data, it
chooses a 4-bit version number, sets the version in the upper 4 bits
of the 64-bit pointer to that data, and stores the 4-bit version in
every cacheline of the object. Hardware saves the latter in spare
bits in the cache and memory hierarchy. On each load and store, the
processor compares the upper 4 VA (virtual address) bits to the
cacheline's version. If there is a mismatch, the processor generates a
version mismatch trap which can be either precise or disrupting. The
trap is an error condition which the kernel delivers to the process as
a SIGSEGV signal.
The upper 4 bits of the VA represent a version and are not part of the
true address. The processor clears these bits and sign extends bit 59
to generate the true address.
Note that 32-bit applications cannot use ADI.
This patch adds ADI support in gdb which allows the user to examine
current version tags and assign new version tags in the program. It
also catches and reports precise or disrupting memory corruption
traps.
gdb/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* sparc64-tdep.h: (adi_normalize_address): New export.
* sparc-nat.h: (open_adi_tag_fd): New export.
* sparc64-linux-nat.c: (open_adi_tag_fd): New function.
* sparc64-linux-tdep.c:
(SEGV_ACCADI, SEGV_ADIDERR, SEGV_ADIPERR) New defines.
(sparc64_linux_handle_segmentation_fault): New function.
(sparc64_linux_init_abi): Register
sparc64_linux_handle_segmentation_fault
* sparc64-tdep.c: Include cli-utils.h,gdbcmd.h,auxv.h.
(sparc64_addr_bits_remove): New function.
(sparc64_init_abi): Register sparc64_addr_bits_remove.
(MAX_PROC_NAME_SIZE): New macro.
(AT_ADI_BLKSZ, AT_ADI_NBITS, AT_ADI_UEONADI) New defines.
(sparc64adilist): New variable.
(adi_proc_list): New variable.
(find_adi_info): New function.
(add_adi_info): New function.
(get_adi_info_proc): New function.
(get_adi_info): New function.
(info_adi_command): New function.
(read_maps_entry): New function.
(adi_available): New function.
(adi_normalize_address): New function.
(adi_align_address): New function.
(adi_convert_byte_count): New function.
(adi_tag_fd): New function.
(adi_is_addr_mapped): New function.
(adi_read_versions): New function.
(adi_write_versions): New function.
(adi_print_versions): New function.
(do_examine): New function.
(do_assign): New function.
(adi_examine_command): New function.
(adi_assign_command): New function.
(_initialize_sparc64_adi_tdep): New function.
gdb/doc/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* gdb.texinfo (Architectures): Add new Sparc64 section to document
ADI support.
* NEWS: Add "adi examine" and "adi assign" commands.
gdb/testsuite/ChangeLog:
2017-08-07 Weimin Pan <weimin.pan@oracle.com>
* gdb.arch/sparc64-adi.exp: New file.
* gdb.arch/sparc64-adi.c: New file.
2017-08-23 16:57:37 +08:00
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include "adi.h"
|
|
|
|
|
|
|
|
#define ONEKB 1024
|
|
|
|
#define PAT 0xdeadbeaf
|
|
|
|
|
|
|
|
#define MAPSIZE 8192
|
|
|
|
#define SHMSIZE 102400
|
|
|
|
#ifndef PROT_ADI
|
|
|
|
#define PROT_ADI 0x10
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
memory_fill (char *addr, size_t size, int pattern)
|
|
|
|
{
|
|
|
|
long *aligned_addr = (long *) addr;
|
|
|
|
long i;
|
|
|
|
for (i = 0; i < size / sizeof (long); i += ONEKB)
|
|
|
|
{
|
|
|
|
*aligned_addr = pattern;
|
|
|
|
aligned_addr = aligned_addr + ONEKB;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
char *haddr;
|
|
|
|
caddr_t vaddr;
|
|
|
|
int version;
|
|
|
|
|
|
|
|
/* Test ISM. */
|
|
|
|
int shmid = shmget (IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
|
|
|
|
if (shmid == -1)
|
|
|
|
exit(1);
|
|
|
|
char *shmaddr = (char *)shmat (shmid, NULL, 0x666 | SHM_RND);
|
|
|
|
if (shmaddr == (char *)-1)
|
|
|
|
{
|
|
|
|
shmctl (shmid, IPC_RMID, NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Enable ADI on ISM segment. */
|
|
|
|
if (mprotect (shmaddr, SHMSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
|
|
|
|
{
|
|
|
|
perror ("mprotect failed");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
if (memory_fill (shmaddr, SHMSIZE, PAT) != 0) /* line breakpoint here */
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
adi_clr_version (shmaddr, SHMSIZE);
|
|
|
|
caddr_t vshmaddr = adi_set_version (shmaddr, SHMSIZE, 0x8);
|
|
|
|
if (vshmaddr == 0)
|
|
|
|
exit(1);
|
|
|
|
/* Test mmap. */
|
|
|
|
int fd = open ("/dev/zero", O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
exit(1);
|
|
|
|
char *maddr = (char *)mmap (NULL, MAPSIZE, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_PRIVATE, fd, 0);
|
|
|
|
if (maddr == (char *)-1)
|
|
|
|
exit(1);
|
|
|
|
/* Enable ADI. */
|
|
|
|
if (mprotect (shmaddr, MAPSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
|
|
|
|
{
|
|
|
|
perror ("mprotect failed");
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (memory_fill (maddr, MAPSIZE, PAT) != 0)
|
|
|
|
exit(1);
|
|
|
|
caddr_t vmaddr = adi_set_version (maddr, MAPSIZE, 0x8);
|
|
|
|
|
|
|
|
/* Test heap. */
|
|
|
|
haddr = (char*) memalign (MAPSIZE, MAPSIZE);
|
|
|
|
/* Enable ADI. */
|
|
|
|
if (mprotect (shmaddr, MAPSIZE, PROT_READ|PROT_WRITE|PROT_ADI))
|
|
|
|
{
|
|
|
|
perror ("mprotect failed");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memory_fill (haddr, MAPSIZE, PAT) != 0)
|
|
|
|
exit(1);
|
|
|
|
adi_clr_version (haddr, MAPSIZE);
|
|
|
|
/* Set some ADP version number. */
|
|
|
|
caddr_t vaddr1, vaddr2, vaddr3, vaddr4;
|
|
|
|
vaddr = adi_set_version (haddr, 64*2, 0x8);
|
|
|
|
vaddr1 = adi_set_version (haddr+64*2, 64*2, 0x9);
|
|
|
|
vaddr2 = adi_clr_version (haddr+64*4, 64*2);
|
|
|
|
vaddr3 = adi_set_version (haddr+64*6, 64*2, 0xa);
|
|
|
|
vaddr4 = adi_set_version (haddr+64*8, 64*10, 0x3);
|
|
|
|
if (vaddr == 0)
|
|
|
|
exit(1);
|
|
|
|
char *versioned_p = vaddr;
|
|
|
|
*versioned_p = 'a';
|
|
|
|
char *uvp = haddr; // unversioned pointer
|
|
|
|
*uvp = 'b'; // version mismatch trap
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
err_out:
|
|
|
|
if (shmdt ((const void *)shmaddr) != 0)
|
|
|
|
perror ("Detach failure");
|
|
|
|
shmctl (shmid, IPC_RMID, NULL);
|
|
|
|
exit (1);
|
|
|
|
}
|