-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.c
409 lines (377 loc) · 12 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Authors: Simon Kuenzer <[email protected]>
*
* Copyright (c) 2019, NEC Laboratories Europe GmbH,
* NEC Corporation. All rights reserved.
* Copyright (c) 2023, Unikraft GmbH. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <uk/config.h>
#include <libelf.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <uk/errptr.h>
#include <uk/essentials.h>
#include <uk/plat/memory.h>
#if CONFIG_LIBPOSIX_PROCESS
#include <uk/process.h>
#endif /* CONFIG_LIBPOSIX_PROCESS */
#include <uk/thread.h>
#include <uk/sched.h>
#if CONFIG_LIBUKRANDOM
#include <uk/random.h>
#endif /* CONFIG_LIBUKRANDOM */
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPATH
#include <uk/argparse.h>
#include <uk/streambuf.h>
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPATH */
#include "elf_prog.h"
#if CONFIG_LIBPOSIX_ENVIRON
extern const char **environ;
#else /* !CONFIG_LIBPOSIX_ENVIRON */
#define environ NULL
#endif /* !CONFIG_LIBPOSIX_ENVIRON */
#ifndef PAGES2BYTES
#define PAGES2BYTES(x) ((x) << __PAGE_SHIFT)
#endif
/*
* Internal version of `basename`
* We keep an own version here that modifies the input string in-place.
* Main reasons are that `nolibc` does not provide `basename()` and there exist
* two versions, in general: a GNU variant with `<string.h>` and a POSIX variant
* with `<libgen.h>`. Depending on the libc used, only one or the other could be
* available.
* NOTE: This version modifies the input string by overwriting trailing slashes.
*/
static inline char *basename_internal(char *path)
{
char *bn;
if (unlikely(!path))
return NULL;
again:
bn = strrchr(path, '/');
if (!bn) {
/* No slash found, path is basename */
return path;
}
if (bn[1] == '\0') {
/* Remove trailing slash */
bn[0] = '\0';
goto again;
}
return ++bn;
}
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPATH
/*
* Routine that locates an executable in a colon-separated list of directories.
* On success, it returns a malloc'ed C-string containing the full path. It is
* in the responsibility of the caller to free the string after use with
* `free()`.
*/
static inline char *locate_exec(const char *basename, const char *path_env)
{
struct uk_streambuf sb;
const char *path_next;
const char *path_cur;
size_t path_cur_len;
struct stat f_stat;
char *buf;
int err;
if (!basename || basename[0] == '/' || basename[0] == '.') {
/* no name given, absolute, or cwd-relative */
err = -EINVAL;
goto err_out;
}
buf = malloc(PATH_MAX);
if (!buf) {
err = -ENOMEM;
goto err_out;
}
/* Iterate over paths */
path_cur = path_env;
path_next = path_env;
while (path_next) {
path_cur = path_next;
path_cur_len = uk_nextarg_r(&path_next, ':');
uk_streambuf_init(&sb, buf, PATH_MAX, UK_STREAMBUF_C_TERMSHIFT);
uk_streambuf_memcpy(&sb, path_cur, path_cur_len);
if (path_cur_len > 0)
uk_streambuf_reserve(&sb, 2); /* last byte of memcpy
* and 1 byte of reserve
* would be overwritten by
* successive strcpy calls
*/
uk_streambuf_strcpy(&sb, "/");
uk_streambuf_strcpy(&sb, basename);
if (uk_streambuf_istruncated(&sb)) {
err = -ENOSPC;
goto err_free_buf;
}
uk_pr_debug("Looking for executable under %s...\n",
(char *)uk_streambuf_buf(&sb));
if (stat((char *)uk_streambuf_buf(&sb), &f_stat) != 0)
continue; /* file not found */
if (unlikely(!(f_stat.st_mode & S_IFREG)))
continue; /* found but not a file */
#if CONFIG_APPELFLOADER_VFSEXEC_EXECBIT
if (unlikely(!(f_stat.st_mode & (S_IXUSR | S_IFREG))))
continue; /* found but not an executable */
#endif /* !CONFIG_APPELFLOADER_VFSEXEC_EXECBIT */
uk_pr_debug("+ Found.\n");
return buf;
}
uk_pr_debug("No executable found for %s\n", basename);
err = -ENOENT;
err_free_buf:
free(buf);
err_out:
return ERR2PTR(err);
}
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPATH */
/*
* Init libelf
*/
static __constructor void _libelf_init(void) {
if (elf_version(EV_CURRENT) == EV_NONE)
UK_CRASH("Failed to initialize libelf: Version error");
}
int main(int argc, const char *argv[])
{
#if CONFIG_APPELFLOADER_INITRDEXEC
struct ukplat_memregion_desc *img;
int rc;
#else /* CONFIG_APPELFLOADER_VFSEXEC */
const char *path;
char *realpath = NULL;
/* reference of strdup()'ed `path` that is converted into `progname` */
char *progname_conv = NULL;
#endif /* CONFIG_APPELFLOADER_VFSEXEC */
const char *progname;
struct elf_prog *prog;
struct uk_thread *app_thread;
struct uk_sched *s = uk_sched_current();
uint64_t rand[2];
int ret = 0;
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPATH
char *env_path;
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPATH */
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPWD
char *env_pwd;
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPWD */
int envc;
UK_ASSERT(s);
/*
* Prepare `progname` (and `path`) from command line
* or compiled-in settings
*/
#if CONFIG_APPELFLOADER_CUSTOMAPPNAME
if (unlikely(argc <= 1 || !argv)) {
uk_pr_err("Program name missing (no argv[1])\n");
ret = 1;
goto out;
}
#if CONFIG_APPELFLOADER_INITRDEXEC
progname = argv[1];
#else /* CONFIG_APPELFLOADER_VFSEXEC */
path = argv[1];
/* retrieve progname from path */
progname_conv = strdup(path);
progname = basename_internal(progname_conv);
#endif /* CONFIG_APPELFLOADER_VFSEXEC */
/* Cut off kernel name (argv[0]) and program name (argv[1])
* from argument vector
*/
argv = &argv[2];
argc -= 2;
#else /* !CONFIG_APPELFLOADER_CUSTOMAPPNAME */
/* Ensure argv[0] exists and is set
* NOTE: Because we can assume to have argv[0] always set by convention,
* we use an assertion here.
*/
UK_ASSERT(argc >= 1 && argv && argv[0]);
#if CONFIG_APPELFLOADER_INITRDEXEC
progname = argv[0];
#else /* CONFIG_APPELFLOADER_VFSEXEC */
path = CONFIG_APPELFLOADER_VFSEXEC_PATH;
/* retrieve progname from path */
progname_conv = strdup(path);
progname = basename_internal(progname_conv);
#endif /* CONFIG_APPELFLOADER_VFSEXEC */
/* Cut off kernel name (argv[0]) from argument vector */
argv = &argv[1];
argc -= 1;
#endif /* !CONFIG_APPELFLOADER_CUSTOMAPPNAME */
#if CONFIG_APPELFLOADER_INITRDEXEC
/*
* Locate ELF initramdisk
*/
uk_pr_debug("Searching for ELF initramdisk...\n");
rc = ukplat_memregion_find_initrd0(&img);
if (unlikely(rc < 0 || !img->vbase || !img->len)) {
uk_pr_err("No image found (initrd parameter missing?)\n");
ret = 1;
goto out;
}
uk_pr_info("Image at %p, len %"__PRIsz" bytes\n",
(void *) img->vbase, img->len);
#endif /* CONFIG_APPELFLOADER_INITRDEXEC */
/*
* Create thread container
* It will have a new stack and an ukarch_ctx
*/
app_thread = uk_thread_create_container(uk_alloc_get_default(),
s->a_stack,
PAGES2BYTES(CONFIG_APPELFLOADER_STACK_NBPAGES),
s->a_auxstack,
0,
s->a_uktls,
false,
progname,
NULL, NULL);
if (unlikely(!app_thread)) {
uk_pr_err("%s: Failed to allocate thread container\n",
progname);
ret = 1;
goto out;
}
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPWD
/*
* Set working directory if `PWD` env variable is set
* FIXME: Remotely set this for target thread
*/
env_pwd = getenv("PWD");
if (env_pwd) {
uk_pr_debug("%s: Changing working directory to '%s'\n",
progname, env_pwd);
if (chdir(env_pwd) < 0) {
uk_pr_err("%s: Failed to change working directory to '%s': %s (%d)\n",
progname, env_pwd, strerror(errno), errno);
goto out_free_thread;
}
}
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPWD */
/*
* Parse image
*/
#if CONFIG_APPELFLOADER_INITRDEXEC
uk_pr_debug("%s: Load executable...\n", progname);
prog = elf_load_img(uk_alloc_get_default(), (void *) img->vbase,
img->len, progname);
#else /* CONFIG_APPELFLOADER_VFSEXEC */
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPATH
env_path = getenv("PATH");
if (env_path) {
realpath = locate_exec(path, env_path);
if (PTR2ERR(realpath) == -EINVAL) {
realpath = NULL;
} else if (PTRISERR(realpath) && PTR2ERR(realpath) != -EINVAL) {
uk_pr_err("%s: Failed to find executable in envirenment ($PATH): %s (%d)",
progname, strerror(-PTR2ERR(realpath)),
PTR2ERR(realpath));
goto out_free_thread;
}
}
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPATH */
uk_pr_debug("%s: Load executable (%s)...\n",
progname, realpath ? realpath : path);
prog = elf_load_vfs(uk_alloc_get_default(),
realpath ? realpath : path, progname);
#if CONFIG_APPELFLOADER_VFSEXEC_ENVPATH
if (realpath)
free(realpath);
#endif /* CONFIG_APPELFLOADER_VFSEXEC_ENVPATH */
#endif /* CONFIG_APPELFLOADER_VFSEXEC */
if (unlikely(PTRISERR(prog) || !prog)) {
ret = -errno;
goto out_free_thread;
}
uk_pr_info("%s: ELF program loaded to 0x%"PRIx64"-0x%"PRIx64" (%"__PRIsz" B), entry at %p\n",
progname,
(uint64_t) prog->vabase,
(uint64_t) prog->vabase + prog->valen,
prog->valen, (void *) prog->entry);
/*
* Initialize application thread
*/
#if CONFIG_LIBUKRANDOM
uk_random_fill_buffer(rand, sizeof(rand));
#else /* !CONFIG_LIBUKRANDOM */
/* Without random numbers, use a hardcoded seed */
uk_pr_warn("%s: Using hard-coded random seed\n", progname);
rand[0] = 0xB0B0;
rand[1] = 0xF00D;
#endif /* !CONFIG_LIBUKRANDOM */
uk_pr_debug("%s: Prepare application thread...\n", progname);
ret = elf_arg_env_count(&argc, argv, &envc, environ,
PAGES2BYTES(CONFIG_APPELFLOADER_STACK_NBPAGES));
if (unlikely(ret < 0)) {
uk_pr_err("Args + env size exceeds limit, increase stack size\n");
goto out_free_thread;
}
elf_ctx_init(&app_thread->ctx, prog, progname,
argc, argv, envc, environ, rand);
app_thread->flags |= UK_THREADF_RUNNABLE;
#if CONFIG_LIBPOSIX_PROCESS
uk_posix_process_create(uk_alloc_get_default(),
app_thread,
uk_thread_current());
#endif
uk_pr_debug("%s: Application stack at %p - %p, pointer: %p\n",
progname,
(void *) app_thread->_mem.stack,
(void *) ((uintptr_t) app_thread->_mem.stack
+ PAGES2BYTES(CONFIG_APPELFLOADER_STACK_NBPAGES)),
(void *) app_thread->ctx.sp);
uk_pr_debug("%s: Application entrance at %p\n",
progname,
(void *) app_thread->ctx.ip);
/*
* Execute application
*/
uk_sched_thread_add(s, app_thread);
/*
* FIXME: Instead of an infinite wait, wait for application
* to exit (this needs thread_wait support with
* uksched and/or posix-process)
*/
for (;;)
sleep(10);
/* TODO: As soon as we are able to return: properly exit/shutdown */
out_free_thread:
uk_thread_release(app_thread);
out:
#if CONFIG_APPELFLOADER_VFSEXEC
if (progname_conv)
free(progname_conv);
#endif /* CONFIG_APPELFLOADER_VFSEXEC */
return ret;
}