]> granicus.if.org Git - strace/blob - tests/kexec_load.c
tests: use fixed timestamps in utime related tests
[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 struct strval {
40         kernel_ulong_t val;
41         const char *str64;
42         const char *str32;
43         const char *str;
44 };
45
46 struct segm {
47         void *buf;
48         size_t bufsz;
49         void *mem;
50         size_t memsz;
51 };
52
53 int
54 main(void)
55 {
56         enum {
57                 NUM_SEGMS = 17,
58                 NUM_SEGMS_UNCUT = 5,
59                 NUM_SEGMS_UNCUT_MAX = 9,
60                 NUM_SEGMS_CUT = 12,
61                 SEGMS_ARRAY_SIZE = sizeof(struct segm) * NUM_SEGMS,
62         };
63
64         static const kernel_ulong_t bogus_zero =
65                 sizeof(long) < sizeof(kernel_long_t) ? F8ILL_KULONG_MASK : 0;
66         static const kernel_ulong_t bogus_entry =
67                 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
68         static const kernel_ulong_t bogus_nsegs =
69                 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
70
71         static const struct strval flags[] = {
72                 { (kernel_ulong_t) 0xbadc0dedda7a1054ULL,
73                         "0xda7a0000 /* KEXEC_ARCH_??? */|0xbadc0ded0000",
74                         "0xda7a0000 /* KEXEC_ARCH_??? */|0x",
75                         "1054 /* KEXEC_??? */" },
76                 { 0, "", "", "KEXEC_ARCH_DEFAULT" },
77                 { 0x2a0003, "", "",
78                         "KEXEC_ARCH_SH|KEXEC_ON_CRASH|KEXEC_PRESERVE_CONTEXT" },
79                 { 0xdead0000, "", "", "0xdead0000 /* KEXEC_ARCH_??? */" },
80         };
81
82         const char *errstr;
83         long rc;
84         struct segm *segms = tail_alloc(SEGMS_ARRAY_SIZE);
85         unsigned int i;
86
87         fill_memory(segms, SEGMS_ARRAY_SIZE);
88         segms[0].buf = segms[0].mem = NULL;
89
90         rc = syscall(__NR_kexec_load, bogus_zero, bogus_zero, bogus_zero,
91                 flags[0].val);
92         printf("kexec_load(NULL, 0, NULL, %s%s) = %s\n",
93                sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
94                flags[0].str, sprintrc(rc));
95
96         rc = syscall(__NR_kexec_load, bogus_entry, bogus_nsegs,
97                      segms + SEGMS_ARRAY_SIZE, flags[1].val);
98         printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
99                (unsigned long) bogus_entry, (unsigned long) bogus_nsegs,
100                segms + SEGMS_ARRAY_SIZE, flags[1].str, sprintrc(rc));
101
102         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS,
103                      segms, flags[2].val);
104         printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
105                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS,
106                segms, flags[2].str, sprintrc(rc));
107
108         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
109                      segms, flags[3].val);
110         errstr = sprintrc(rc);
111         printf("kexec_load(%#lx, %lu, [{buf=NULL, bufsz=%zu, mem=NULL, "
112                "memsz=%zu}, ",
113                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT,
114                segms[0].bufsz, segms[0].memsz);
115         for (i = 1; i < NUM_SEGMS_UNCUT_MAX; i++)
116                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
117                        segms[i].buf, segms[i].bufsz,
118                        segms[i].mem, segms[i].memsz);
119         printf("...], %s) = %s\n", flags[3].str, errstr);
120
121         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
122                      segms + (NUM_SEGMS - NUM_SEGMS_UNCUT_MAX),
123                      flags[0].val);
124         errstr = sprintrc(rc);
125         printf("kexec_load(%#lx, %lu, [",
126                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
127         for (i = NUM_SEGMS - NUM_SEGMS_UNCUT_MAX; i < NUM_SEGMS; i++)
128                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
129                        segms[i].buf, segms[i].bufsz,
130                        segms[i].mem, segms[i].memsz);
131         printf("%p], %s%s) = %s\n",
132                segms + NUM_SEGMS,
133                sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
134                flags[0].str, errstr);
135
136         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_UNCUT,
137                      segms + (NUM_SEGMS - NUM_SEGMS_UNCUT),
138                      flags[1].val);
139         errstr = sprintrc(rc);
140         printf("kexec_load(%#lx, %lu, [",
141                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_UNCUT);
142         for (i = NUM_SEGMS - NUM_SEGMS_UNCUT; i < NUM_SEGMS; i++)
143                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}%s",
144                        segms[i].buf, segms[i].bufsz,
145                        segms[i].mem, segms[i].memsz,
146                        (i == NUM_SEGMS - 1) ? "" : ", ");
147         printf("], %s) = %s\n", flags[1].str, errstr);
148
149         rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
150                      segms + 1, flags[2].val);
151         errstr = sprintrc(rc);
152         printf("kexec_load(%#lx, %lu, [",
153                (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
154         for (i = 1; i < NUM_SEGMS_UNCUT_MAX + 1; i++)
155                 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
156                        segms[i].buf, segms[i].bufsz,
157                        segms[i].mem, segms[i].memsz);
158         printf("...], %s) = %s\n", flags[2].str, errstr);
159
160         puts("+++ exited with 0 +++");
161
162         return 0;
163 }
164
165 #else
166
167 SKIP_MAIN_UNDEFINED("__NR_kexec_load");
168
169 #endif