Wed Jul 8 03:44:10 2020 UTC ()
don't read memory directly.
In particular, userland memory may be unmapped at the time of reading.


(ryo)
diff -r1.7 -r1.8 src/sys/arch/aarch64/aarch64/db_disasm.c

cvs diff -r1.7 -r1.8 src/sys/arch/aarch64/aarch64/db_disasm.c (expand / switch to unified diff)

--- src/sys/arch/aarch64/aarch64/db_disasm.c 2019/10/28 18:15:25 1.7
+++ src/sys/arch/aarch64/aarch64/db_disasm.c 2020/07/08 03:44:10 1.8
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: db_disasm.c,v 1.7 2019/10/28 18:15:25 joerg Exp $ */ 1/* $NetBSD: db_disasm.c,v 1.8 2020/07/08 03:44:10 ryo Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2017 Ryo Shimizu <ryo@nerv.org> 4 * Copyright (c) 2017 Ryo Shimizu <ryo@nerv.org>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,36 +17,37 @@ @@ -17,36 +17,37 @@
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.7 2019/10/28 18:15:25 joerg Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.8 2020/07/08 03:44:10 ryo Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <machine/db_machdep.h> 33#include <machine/db_machdep.h>
34#include <ddb/db_interface.h> 34#include <ddb/db_interface.h>
35#include <ddb/db_sym.h> 35#include <ddb/db_sym.h>
36#include <ddb/db_output.h> 36#include <ddb/db_output.h>
37#include <ddb/db_access.h> 37#include <ddb/db_access.h>
38#include <ddb/db_user.h> 38#include <ddb/db_user.h>
39 39
 40#include <aarch64/cpufunc.h>
40#include <arch/aarch64/aarch64/disasm.h> 41#include <arch/aarch64/aarch64/disasm.h>
41 42
42static uint32_t 43static uint32_t
43db_disasm_readword(uintptr_t address) 44db_disasm_readword(uintptr_t address)
44{ 45{
45 return db_get_value(address, sizeof(uint32_t), false); 46 return db_get_value(address, sizeof(uint32_t), false);
46} 47}
47 48
48static void 49static void
49db_disasm_printaddr(uintptr_t address) 50db_disasm_printaddr(uintptr_t address)
50{ 51{
51 db_printf("%lx <", address); 52 db_printf("%lx <", address);
52 db_printsym((db_addr_t)address, DB_STGY_ANY, db_printf); 53 db_printsym((db_addr_t)address, DB_STGY_ANY, db_printf);
@@ -62,27 +63,44 @@ static const disasm_interface_t db_disas @@ -62,27 +63,44 @@ static const disasm_interface_t db_disas
62db_addr_t 63db_addr_t
63db_disasm(db_addr_t loc, bool altfmt) 64db_disasm(db_addr_t loc, bool altfmt)
64{ 65{
65 return disasm(&db_disasm_interface, loc); 66 return disasm(&db_disasm_interface, loc);
66} 67}
67 68
68 69
69static char *strdisasm_ptr; 70static char *strdisasm_ptr;
70static char strdisasm_buf[256]; 71static char strdisasm_buf[256];
71 72
72static uint32_t 73static uint32_t
73strdisasm_readword(uintptr_t address) 74strdisasm_readword(uintptr_t address)
74{ 75{
75 return *(uint32_t *)address; 76 /*
 77 * if it cannot be read due to a EFAULT etc.,
 78 * ignores the error and returns 0
 79 */
 80 uint32_t word = 0;
 81
 82 switch (aarch64_addressspace((vaddr_t)address)) {
 83 case AARCH64_ADDRSPACE_UPPER:
 84 kcopy((void*)address, &word, sizeof(word));
 85 break;
 86 case AARCH64_ADDRSPACE_LOWER:
 87 ufetch_32((uint32_t *)address, &word);
 88 break;
 89 default:
 90 break;
 91 }
 92
 93 return word;
76} 94}
77 95
78static void __printflike(1, 2) 96static void __printflike(1, 2)
79strdisasm_printf(const char *fmt, ...) 97strdisasm_printf(const char *fmt, ...)
80{ 98{
81 va_list ap; 99 va_list ap;
82 int len; 100 int len;
83 101
84 /* calculation spaces to append a string */ 102 /* calculation spaces to append a string */
85 len = strdisasm_buf + sizeof(strdisasm_buf) - strdisasm_ptr; 103 len = strdisasm_buf + sizeof(strdisasm_buf) - strdisasm_ptr;
86 104
87 va_start(ap, fmt); 105 va_start(ap, fmt);
88 len = vsnprintf(strdisasm_ptr, len, fmt, ap); 106 len = vsnprintf(strdisasm_ptr, len, fmt, ap);