]> granicus.if.org Git - strace/blob - tests/kexec_load.c
Update NEWS
[strace] / tests / kexec_load.c
1 /*
2  * Check decoding of kexec_load syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #include <asm/unistd.h>
33
34 #ifdef __NR_kexec_load
35
36 # include <stdio.h>
37 # include <unistd.h>
38
39 # include "kernel_types.h"
40
41 struct strval {
42         kernel_ulong_t val;
43         const char *str64;
44         const char *str32;
45         const char *str;
46 };
47
48 struct segm {
49         void *buf;
50         size_t bufsz;
51         void *mem;
52         size_t memsz;
53 };
54
55 int
56 main(void)
57 {
58         enum {
59                 NUM_SEGMS = 17,
60                 NUM_SEGMS_UNCUT = 5,
61                 NUM_SEGMS_UNCUT_MAX = 9,
62                 NUM_SEGMS_CUT = 12,
63                 SEGMS_ARRAY_SIZE = sizeof(struct segm) * NUM_SEGMS,
64         };
65
66         static const kernel_ulong_t bogus_zero =
67                 sizeof(long) < sizeof(kernel_long_t) ?
68                         (kernel_ulong_t) 0xffffffff00000000ULL : 0;
69         static const kernel_ulong_t bogus_entry =
70                 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
71         static const kernel_ulong_t bogus_nsegs =
72                 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
73
74         static const struct strval flags[] = {
75                 { (kernel_ulong_t) 0xbadc0dedda7a1054ULL,
76                         "0xda7a0000 /* KEXEC_ARCH_??? */|0xbadc0ded0000",
77                         "0xda7a0000 /* KEXEC_ARCH_??? */|0x",
78                         "1054 /* KEXEC_??? */" },
79                 { 0, "", "", "KEXEC_ARCH_DEFAULT" },
80                 { 0x2a0003, "", "",
81                         "KEXEC_ARCH_SH|KEXEC_ON_CRASH|KEXEC_PRESERVE_CONTEXT" },
82                 { 0xdead0000, "", "", "0xdead0000 /* KEXEC_ARCH_??? */" },
83         };
84
85         const char *errstr;
86         long rc;
87         struct segm *segms = tail_alloc(SEGMS_ARRAY_SIZE);
88         unsigned int i;
89
90         fill_memory((char *) segms, SEGMS_ARRAY_SIZE);
91         segms[0].buf = segms[0].mem = NULL;
92
93         rc = syscall(__NR_kexec_load, bogus_zero, bogus_zero, bogus_zero,
94                 flags[0].val);
95         printf("kexec_load(NULL, 0, NULL, %s%s) = %s\n",
96                sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
97                flags[0].str, sprintrc(rc));
98
99         rc = syscall(__NR_kexec_load, bogus_entry, bogus_nsegs,
100                      segms + SEGMS_ARRAY_SIZE, flags[1].val);
101         printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
102                (unsigned long) bogus_entry, (unsigned long) bogus_nsegs,
103                segms + SEGMS_ARRAY_SIZE, flags[1].str, sprintrc(rc));
104
105         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS,
106                      segms, flags[2].val);
107         printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
108                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS,
109                segms, flags[2].str, sprintrc(rc));
110
111         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
112                      segms, flags[3].val);
113         errstr = sprintrc(rc);
114         printf("kexec_load(%#lx, %lu, [{buf=NULL, bufsz=%zu, mem=NULL, "
115                "memsz=%zu}, ",
116                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT,
117                segms[0].bufsz, segms[0].memsz);
118         for (i = 1; i < NUM_SEGMS_UNCUT_MAX; i++)
119                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
120                        segms[i].buf, segms[i].bufsz,
121                        segms[i].mem, segms[i].memsz);
122         printf("...], %s) = %s\n", flags[3].str, errstr);
123
124         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
125                      segms + (NUM_SEGMS - NUM_SEGMS_UNCUT_MAX),
126                      flags[0].val);
127         errstr = sprintrc(rc);
128         printf("kexec_load(%#lx, %lu, [",
129                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
130         for (i = NUM_SEGMS - NUM_SEGMS_UNCUT_MAX; i < NUM_SEGMS; i++)
131                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
132                        segms[i].buf, segms[i].bufsz,
133                        segms[i].mem, segms[i].memsz);
134         printf("%p], %s%s) = %s\n",
135                segms + NUM_SEGMS,
136                sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
137                flags[0].str, errstr);
138
139         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_UNCUT,
140                      segms + (NUM_SEGMS - NUM_SEGMS_UNCUT),
141                      flags[1].val);
142         errstr = sprintrc(rc);
143         printf("kexec_load(%#lx, %lu, [",
144                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_UNCUT);
145         for (i = NUM_SEGMS - NUM_SEGMS_UNCUT; i < NUM_SEGMS; i++)
146                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}%s",
147                        segms[i].buf, segms[i].bufsz,
148                        segms[i].mem, segms[i].memsz,
149                        (i == NUM_SEGMS - 1) ? "" : ", ");
150         printf("], %s) = %s\n", flags[1].str, errstr);
151
152         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
153                      segms + 1, flags[2].val);
154         errstr = sprintrc(rc);
155         printf("kexec_load(%#lx, %lu, [",
156                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
157         for (i = 1; i < NUM_SEGMS_UNCUT_MAX + 1; i++)
158                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
159                        segms[i].buf, segms[i].bufsz,
160                        segms[i].mem, segms[i].memsz);
161         printf("...], %s) = %s\n", flags[2].str, errstr);
162
163         puts("+++ exited with 0 +++");
164
165         return 0;
166 }
167
168 #else
169
170 SKIP_MAIN_UNDEFINED("__NR_kexec_load");
171
172 #endif