]> granicus.if.org Git - strace/blob - mem.c
ucopy: return string size in umovestr
[strace] / mem.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2000 PocketPenguins Inc.  Linux for Hitachi SuperH
7  *                    port by Greg Banks <gbanks@pocketpenguins.com>
8  * Copyright (c) 1999-2017 The strace developers.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include <linux/mman.h>
36 #include <sys/mman.h>
37
38 unsigned long
39 get_pagesize(void)
40 {
41         static unsigned long pagesize;
42
43         if (!pagesize)
44                 pagesize = sysconf(_SC_PAGESIZE);
45         return pagesize;
46 }
47
48 SYS_FUNC(brk)
49 {
50         printaddr(tcp->u_arg[0]);
51
52         return RVAL_DECODED | RVAL_HEX;
53 }
54
55 #include "xlat/mmap_prot.h"
56 #include "xlat/mmap_flags.h"
57
58 #ifndef MAP_HUGE_SHIFT
59 # define MAP_HUGE_SHIFT 26
60 #endif
61
62 #ifndef MAP_HUGE_MASK
63 # define MAP_HUGE_MASK 0x3f
64 #endif
65
66 static void
67 print_mmap_flags(kernel_ulong_t flags)
68 {
69         printxval64(mmap_flags, flags & MAP_TYPE, "MAP_???");
70         flags &= ~MAP_TYPE;
71
72         const unsigned int mask = MAP_HUGE_MASK << MAP_HUGE_SHIFT;
73         const unsigned int hugetlb_value = flags & mask;
74
75         flags &= ~mask;
76         addflags(mmap_flags, flags);
77
78         if (hugetlb_value)
79                 tprintf("|%u<<MAP_HUGE_SHIFT",
80                         hugetlb_value >> MAP_HUGE_SHIFT);
81 }
82
83 static void
84 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
85 {
86         const kernel_ulong_t addr = u_arg[0];
87         const kernel_ulong_t len = u_arg[1];
88         const kernel_ulong_t prot = u_arg[2];
89         const kernel_ulong_t flags = u_arg[3];
90         const int fd = u_arg[4];
91
92         printaddr(addr);
93         tprintf(", %" PRI_klu ", ", len);
94         printflags64(mmap_prot, prot, "PROT_???");
95         tprints(", ");
96         print_mmap_flags(flags);
97         tprints(", ");
98         printfd(tcp, fd);
99         tprintf(", %#llx", offset);
100 }
101
102 /* Syscall name<->function correspondence is messed up on many arches.
103  * For example:
104  * i386 has __NR_mmap == 90, and it is "old mmap", and
105  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
106  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
107  * Confused? Me too!
108  */
109
110 #ifdef HAVE_ARCH_OLD_MMAP
111 /**
112  * Fetch old_mmap/old_mmap_pgoff arguments that are provided as a 6-element
113  * array.  Return pointer to a static array or NULL in case of fetch failure.
114  */
115 kernel_ulong_t *
116 fetch_old_mmap_args(struct tcb *tcp)
117 {
118         static kernel_ulong_t u_arg[6];
119
120         if (current_wordsize == 4) {
121                 unsigned int narrow_arg[6];
122                 if (umove(tcp, tcp->u_arg[0], &narrow_arg))
123                         return NULL;
124                 for (unsigned int i = 0; i < 6; i++)
125                         u_arg[i] = narrow_arg[i];
126         } else {
127                 if (umove(tcp, tcp->u_arg[0], &u_arg))
128                         return NULL;
129         }
130
131         return u_arg;
132 }
133
134 /* Params are pointed to by u_arg[0], offset is in bytes */
135 SYS_FUNC(old_mmap)
136 {
137         kernel_ulong_t *args = fetch_old_mmap_args(tcp);
138
139         if (args)
140                 print_mmap(tcp, args, args[5]);
141         else
142                 printaddr(tcp->u_arg[0]);
143
144         return RVAL_DECODED | RVAL_HEX;
145 }
146
147 # ifdef HAVE_ARCH_OLD_MMAP_PGOFF
148 /* Params are pointed to by u_arg[0], offset is in pages */
149 SYS_FUNC(old_mmap_pgoff)
150 {
151         kernel_ulong_t *args = fetch_old_mmap_args(tcp);
152
153         if (args) {
154                 unsigned long long offset;
155
156                 offset = args[5];
157                 offset *= get_pagesize();
158
159                 print_mmap(tcp, args, offset);
160         } else {
161                 printaddr(tcp->u_arg[0]);
162         }
163
164         return RVAL_DECODED | RVAL_HEX;
165 }
166 # endif /* HAVE_ARCH_OLD_MMAP_PGOFF */
167 #endif /* HAVE_ARCH_OLD_MMAP */
168
169 /* Params are passed directly, offset is in bytes */
170 SYS_FUNC(mmap)
171 {
172         /* Example of kernel-side handling of this variety of mmap:
173          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
174          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
175          * since the above code converts off to pages.
176          */
177         print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
178
179         return RVAL_DECODED | RVAL_HEX;
180 }
181
182 /* Params are passed directly, offset is in pages */
183 SYS_FUNC(mmap_pgoff)
184 {
185         /* Try test/mmap_offset_decode.c */
186         unsigned long long offset;
187         offset = tcp->u_arg[5];
188         offset *= get_pagesize();
189         print_mmap(tcp, tcp->u_arg, offset);
190
191         return RVAL_DECODED | RVAL_HEX;
192 }
193
194 /* Params are passed directly, offset is in 4k units */
195 SYS_FUNC(mmap_4koff)
196 {
197         unsigned long long offset;
198         offset = tcp->u_arg[5];
199         offset <<= 12;
200         print_mmap(tcp, tcp->u_arg, offset);
201
202         return RVAL_DECODED | RVAL_HEX;
203 }
204
205 SYS_FUNC(munmap)
206 {
207         printaddr(tcp->u_arg[0]);
208         tprintf(", %" PRI_klu, tcp->u_arg[1]);
209
210         return RVAL_DECODED;
211 }
212
213 static int
214 do_mprotect(struct tcb *tcp, bool has_pkey)
215 {
216         printaddr(tcp->u_arg[0]);
217         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
218         printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
219
220         if (has_pkey)
221                 tprintf(", %d", (int) tcp->u_arg[3]);
222
223         return RVAL_DECODED;
224 }
225
226 SYS_FUNC(mprotect)
227 {
228         return do_mprotect(tcp, false);
229 }
230
231 SYS_FUNC(pkey_mprotect)
232 {
233         return do_mprotect(tcp, true);
234 }
235
236 #include "xlat/mremap_flags.h"
237
238 SYS_FUNC(mremap)
239 {
240         printaddr(tcp->u_arg[0]);
241         tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
242         printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
243 #ifdef MREMAP_FIXED
244         if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
245             (MREMAP_MAYMOVE | MREMAP_FIXED)) {
246                 tprints(", ");
247                 printaddr(tcp->u_arg[4]);
248         }
249 #endif
250         return RVAL_DECODED | RVAL_HEX;
251 }
252
253 #include "xlat/madvise_cmds.h"
254
255 SYS_FUNC(madvise)
256 {
257         printaddr(tcp->u_arg[0]);
258         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
259         printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
260
261         return RVAL_DECODED;
262 }
263
264 #include "xlat/mlockall_flags.h"
265
266 SYS_FUNC(mlockall)
267 {
268         printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
269
270         return RVAL_DECODED;
271 }
272
273 #include "xlat/mctl_sync.h"
274
275 SYS_FUNC(msync)
276 {
277         /* addr */
278         printaddr(tcp->u_arg[0]);
279         /* len */
280         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
281         /* flags */
282         printflags(mctl_sync, tcp->u_arg[2], "MS_???");
283
284         return RVAL_DECODED;
285 }
286
287 #include "xlat/mlock_flags.h"
288
289 SYS_FUNC(mlock2)
290 {
291         printaddr(tcp->u_arg[0]);
292         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
293         printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
294
295         return RVAL_DECODED;
296 }
297
298 SYS_FUNC(mincore)
299 {
300         if (entering(tcp)) {
301                 printaddr(tcp->u_arg[0]);
302                 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
303         } else {
304                 const unsigned long page_size = get_pagesize();
305                 const unsigned long page_mask = page_size - 1;
306                 unsigned long len = tcp->u_arg[1];
307                 unsigned char *vec = NULL;
308
309                 len = len / page_size + (len & page_mask ? 1 : 0);
310                 if (syserror(tcp) || !verbose(tcp) ||
311                     !tcp->u_arg[2] || !(vec = malloc(len)) ||
312                     umoven(tcp, tcp->u_arg[2], len, vec) < 0)
313                         printaddr(tcp->u_arg[2]);
314                 else {
315                         unsigned long i;
316                         tprints("[");
317                         for (i = 0; i < len; i++) {
318                                 if (i)
319                                         tprints(", ");
320                                 if (abbrev(tcp) && i >= max_strlen) {
321                                         tprints("...");
322                                         break;
323                                 }
324                                 tprints((vec[i] & 1) ? "1" : "0");
325                         }
326                         tprints("]");
327                 }
328                 free(vec);
329         }
330         return 0;
331 }
332
333 #if defined ALPHA || defined IA64 || defined M68K \
334  || defined SPARC || defined SPARC64
335 SYS_FUNC(getpagesize)
336 {
337         return RVAL_DECODED | RVAL_HEX;
338 }
339 #endif
340
341 SYS_FUNC(remap_file_pages)
342 {
343         const kernel_ulong_t addr = tcp->u_arg[0];
344         const kernel_ulong_t size = tcp->u_arg[1];
345         const kernel_ulong_t prot = tcp->u_arg[2];
346         const kernel_ulong_t pgoff = tcp->u_arg[3];
347         const kernel_ulong_t flags = tcp->u_arg[4];
348
349         printaddr(addr);
350         tprintf(", %" PRI_klu ", ", size);
351         printflags64(mmap_prot, prot, "PROT_???");
352         tprintf(", %" PRI_klu ", ", pgoff);
353         print_mmap_flags(flags);
354
355         return RVAL_DECODED;
356 }
357
358 #if defined(POWERPC)
359 static bool
360 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
361 {
362         tprintf("%#08x", *(unsigned int *) elem_buf);
363
364         return true;
365 }
366
367 SYS_FUNC(subpage_prot)
368 {
369         kernel_ulong_t addr = tcp->u_arg[0];
370         kernel_ulong_t len = tcp->u_arg[1];
371         kernel_ulong_t nmemb = len >> 16;
372         kernel_ulong_t map = tcp->u_arg[2];
373
374         printaddr(addr);
375         tprintf(", %" PRI_klu ", ", len);
376
377         unsigned int entry;
378         print_array(tcp, map, nmemb, &entry, sizeof(entry),
379                     umoven_or_printaddr, print_protmap_entry, 0);
380
381         return RVAL_DECODED;
382 }
383 #endif