diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
commit | 8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch) | |
tree | df55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/env | |
parent | Initial commit. (diff) | |
download | wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.tar.xz wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.zip |
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-top-half/musl/src/env')
-rw-r--r-- | libc-top-half/musl/src/env/__environ.c | 6 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/__init_tls.c | 186 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/__libc_start_main.c | 97 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/__reset_tls.c | 15 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/__stack_chk_fail.c | 49 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/clearenv.c | 21 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/getenv.c | 20 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/putenv.c | 53 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/secure_getenv.c | 8 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/setenv.c | 42 | ||||
-rw-r--r-- | libc-top-half/musl/src/env/unsetenv.c | 35 |
11 files changed, 532 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/env/__environ.c b/libc-top-half/musl/src/env/__environ.c new file mode 100644 index 0000000..fe8abcf --- /dev/null +++ b/libc-top-half/musl/src/env/__environ.c @@ -0,0 +1,6 @@ +#include <unistd.h> + +char **__environ = 0; +weak_alias(__environ, ___environ); +weak_alias(__environ, _environ); +weak_alias(__environ, environ); diff --git a/libc-top-half/musl/src/env/__init_tls.c b/libc-top-half/musl/src/env/__init_tls.c new file mode 100644 index 0000000..ee785bc --- /dev/null +++ b/libc-top-half/musl/src/env/__init_tls.c @@ -0,0 +1,186 @@ +#ifdef __wasilibc_unmodified_upstream +#define SYSCALL_NO_TLS 1 +#include <elf.h> +#endif +#include <limits.h> +#ifdef __wasilibc_unmodified_upstream +#include <sys/mman.h> +#endif +#include <string.h> +#include <stddef.h> +#include "pthread_impl.h" +#include "libc.h" +#include "atomic.h" +#include "syscall.h" + +volatile int __thread_list_lock; + +#ifndef __wasilibc_unmodified_upstream +void __wasi_init_tp() { + __init_tp((void *)__get_tp()); +} +#endif + +int __init_tp(void *p) +{ + pthread_t td = p; + td->self = td; +#ifdef __wasilibc_unmodified_upstream + int r = __set_thread_area(TP_ADJ(p)); + if (r < 0) return -1; + if (!r) libc.can_do_threads = 1; + td->detach_state = DT_JOINABLE; + td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock); +#endif + td->locale = &libc.global_locale; + td->robust_list.head = &td->robust_list.head; + td->sysinfo = __sysinfo; + td->next = td->prev = td; + return 0; +} + +#ifdef __wasilibc_unmodified_upstream + +static struct builtin_tls { + char c; + struct pthread pt; + void *space[16]; +} builtin_tls[1]; +#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) + +static struct tls_module main_tls; +#endif + +#ifndef __wasilibc_unmodified_upstream +extern void __wasm_init_tls(void*); +#endif + +void *__copy_tls(unsigned char *mem) +{ +#ifdef __wasilibc_unmodified_upstream + pthread_t td; + struct tls_module *p; + size_t i; + uintptr_t *dtv; + +#ifdef TLS_ABOVE_TP + dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1); + + mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1); + td = (pthread_t)mem; + mem += sizeof(struct pthread); + + for (i=1, p=libc.tls_head; p; i++, p=p->next) { + dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET; + memcpy(mem + p->offset, p->image, p->len); + } +#else + dtv = (uintptr_t *)mem; + + mem += libc.tls_size - sizeof(struct pthread); + mem -= (uintptr_t)mem & (libc.tls_align-1); + td = (pthread_t)mem; + + for (i=1, p=libc.tls_head; p; i++, p=p->next) { + dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET; + memcpy(mem - p->offset, p->image, p->len); + } +#endif + dtv[0] = libc.tls_cnt; + td->dtv = dtv; + return td; +#else + size_t tls_align = __builtin_wasm_tls_align(); + volatile void* tls_base = __builtin_wasm_tls_base(); + mem += tls_align; + mem -= (uintptr_t)mem & (tls_align-1); + __wasm_init_tls(mem); + __asm__("local.get %0\n" + "global.set __tls_base\n" + :: "r"(tls_base)); + return mem; +#endif +} + +#ifdef __wasilibc_unmodified_upstream +#if ULONG_MAX == 0xffffffff +typedef Elf32_Phdr Phdr; +#else +typedef Elf64_Phdr Phdr; +#endif + +extern weak hidden const size_t _DYNAMIC[]; + +static void static_init_tls(size_t *aux) +{ + unsigned char *p; + size_t n; + Phdr *phdr, *tls_phdr=0; + size_t base = 0; + void *mem; + + for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) { + phdr = (void *)p; + if (phdr->p_type == PT_PHDR) + base = aux[AT_PHDR] - phdr->p_vaddr; + if (phdr->p_type == PT_DYNAMIC && _DYNAMIC) + base = (size_t)_DYNAMIC - phdr->p_vaddr; + if (phdr->p_type == PT_TLS) + tls_phdr = phdr; + if (phdr->p_type == PT_GNU_STACK && + phdr->p_memsz > __default_stacksize) + __default_stacksize = + phdr->p_memsz < DEFAULT_STACK_MAX ? + phdr->p_memsz : DEFAULT_STACK_MAX; + } + + if (tls_phdr) { + main_tls.image = (void *)(base + tls_phdr->p_vaddr); + main_tls.len = tls_phdr->p_filesz; + main_tls.size = tls_phdr->p_memsz; + main_tls.align = tls_phdr->p_align; + libc.tls_cnt = 1; + libc.tls_head = &main_tls; + } + + main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image) + & (main_tls.align-1); +#ifdef TLS_ABOVE_TP + main_tls.offset = GAP_ABOVE_TP; + main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image) + & (main_tls.align-1); +#else + main_tls.offset = main_tls.size; +#endif + if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN; + + libc.tls_align = main_tls.align; + libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread) +#ifdef TLS_ABOVE_TP + + main_tls.offset +#endif + + main_tls.size + main_tls.align + + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN; + + if (libc.tls_size > sizeof builtin_tls) { +#ifndef SYS_mmap2 +#define SYS_mmap2 SYS_mmap +#endif + mem = (void *)__syscall( + SYS_mmap2, + 0, libc.tls_size, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + /* -4095...-1 cast to void * will crash on dereference anyway, + * so don't bloat the init code checking for error codes and + * explicitly calling a_crash(). */ + } else { + mem = builtin_tls; + } + + /* Failure to initialize thread pointer is always fatal. */ + if (__init_tp(__copy_tls(mem)) < 0) + a_crash(); +} + +weak_alias(static_init_tls, __init_tls); +#endif diff --git a/libc-top-half/musl/src/env/__libc_start_main.c b/libc-top-half/musl/src/env/__libc_start_main.c new file mode 100644 index 0000000..c5b277b --- /dev/null +++ b/libc-top-half/musl/src/env/__libc_start_main.c @@ -0,0 +1,97 @@ +#include <elf.h> +#include <poll.h> +#include <fcntl.h> +#include <signal.h> +#include <unistd.h> +#include "syscall.h" +#include "atomic.h" +#include "libc.h" + +static void dummy(void) {} +weak_alias(dummy, _init); + +extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void); + +static void dummy1(void *p) {} +weak_alias(dummy1, __init_ssp); + +#define AUX_CNT 38 + +#ifdef __GNUC__ +__attribute__((__noinline__)) +#endif +void __init_libc(char **envp, char *pn) +{ + size_t i, *auxv, aux[AUX_CNT] = { 0 }; + __environ = envp; + for (i=0; envp[i]; i++); + libc.auxv = auxv = (void *)(envp+i+1); + for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1]; + __hwcap = aux[AT_HWCAP]; + if (aux[AT_SYSINFO]) __sysinfo = aux[AT_SYSINFO]; + libc.page_size = aux[AT_PAGESZ]; + + if (!pn) pn = (void*)aux[AT_EXECFN]; + if (!pn) pn = ""; + __progname = __progname_full = pn; + for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1; + + __init_tls(aux); + __init_ssp((void *)aux[AT_RANDOM]); + + if (aux[AT_UID]==aux[AT_EUID] && aux[AT_GID]==aux[AT_EGID] + && !aux[AT_SECURE]) return; + + struct pollfd pfd[3] = { {.fd=0}, {.fd=1}, {.fd=2} }; + int r = +#ifdef SYS_poll + __syscall(SYS_poll, pfd, 3, 0); +#else + __syscall(SYS_ppoll, pfd, 3, &(struct timespec){0}, 0, _NSIG/8); +#endif + if (r<0) a_crash(); + for (i=0; i<3; i++) if (pfd[i].revents&POLLNVAL) + if (__sys_open("/dev/null", O_RDWR)<0) + a_crash(); + libc.secure = 1; +} + +static void libc_start_init(void) +{ + _init(); + uintptr_t a = (uintptr_t)&__init_array_start; + for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)())) + (*(void (**)(void))a)(); +} + +weak_alias(libc_start_init, __libc_start_init); + +typedef int lsm2_fn(int (*)(int,char **,char **), int, char **); +static lsm2_fn libc_start_main_stage2; + +int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv, + void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)()) +{ + char **envp = argv+argc+1; + + /* External linkage, and explicit noinline attribute if available, + * are used to prevent the stack frame used during init from + * persisting for the entire process lifetime. */ + __init_libc(envp, argv[0]); + + /* Barrier against hoisting application code or anything using ssp + * or thread pointer prior to its initialization above. */ + lsm2_fn *stage2 = libc_start_main_stage2; + __asm__ ( "" : "+r"(stage2) : : "memory" ); + return stage2(main, argc, argv); +} + +static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, char **argv) +{ + char **envp = argv+argc+1; + __libc_start_init(); + + /* Pass control to the application */ + exit(main(argc, argv, envp)); + return 0; +} diff --git a/libc-top-half/musl/src/env/__reset_tls.c b/libc-top-half/musl/src/env/__reset_tls.c new file mode 100644 index 0000000..15685bc --- /dev/null +++ b/libc-top-half/musl/src/env/__reset_tls.c @@ -0,0 +1,15 @@ +#include <string.h> +#include "pthread_impl.h" +#include "libc.h" + +void __reset_tls() +{ + pthread_t self = __pthread_self(); + struct tls_module *p; + size_t i, n = self->dtv[0]; + if (n) for (p=libc.tls_head, i=1; i<=n; i++, p=p->next) { + char *mem = (char *)(self->dtv[i] - DTP_OFFSET); + memcpy(mem, p->image, p->len); + memset(mem+p->len, 0, p->size - p->len); + } +} diff --git a/libc-top-half/musl/src/env/__stack_chk_fail.c b/libc-top-half/musl/src/env/__stack_chk_fail.c new file mode 100644 index 0000000..cb7a3f3 --- /dev/null +++ b/libc-top-half/musl/src/env/__stack_chk_fail.c @@ -0,0 +1,49 @@ +#include <string.h> +#include <stdint.h> +#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) +#include "pthread_impl.h" +#else +// In non-_REENTRANT, include it for `a_crash` +# include "atomic.h" +#endif + +uintptr_t __stack_chk_guard; + +void __init_ssp(void *entropy) +{ + if (entropy) memcpy(&__stack_chk_guard, entropy, sizeof(uintptr_t)); + else __stack_chk_guard = (uintptr_t)&__stack_chk_guard * 1103515245; + +#if UINTPTR_MAX >= 0xffffffffffffffff + /* Sacrifice 8 bits of entropy on 64bit to prevent leaking/ + * overwriting the canary via string-manipulation functions. + * The NULL byte is on the second byte so that off-by-ones can + * still be detected. Endianness is taken care of + * automatically. */ + ((char *)&__stack_chk_guard)[1] = 0; +#endif + +#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) + __pthread_self()->canary = __stack_chk_guard; +#endif +} + +void __stack_chk_fail(void) +{ + a_crash(); +} + +hidden void __stack_chk_fail_local(void); + +weak_alias(__stack_chk_fail, __stack_chk_fail_local); + +#ifndef __wasilibc_unmodified_upstream +# include <wasi/api.h> + +__attribute__((constructor(60))) +static void __wasilibc_init_ssp(void) { + uintptr_t entropy; + int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t)); + __init_ssp(r ? NULL : &entropy); +} +#endif diff --git a/libc-top-half/musl/src/env/clearenv.c b/libc-top-half/musl/src/env/clearenv.c new file mode 100644 index 0000000..0abbec3 --- /dev/null +++ b/libc-top-half/musl/src/env/clearenv.c @@ -0,0 +1,21 @@ +#define _GNU_SOURCE +#include <stdlib.h> +#include <unistd.h> + +static void dummy(char *old, char *new) {} +weak_alias(dummy, __env_rm_add); + +int clearenv() +{ +#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init. +#else +// This specialized header is included within the function body to arranges for +// the environment variables to be lazily initialized. It redefined `__environ`, +// so don't remove or reorder it with respect to other code. +#include "wasi/libc-environ-compat.h" +#endif + char **e = __environ; + __environ = 0; + if (e) while (*e) __env_rm_add(*e++, 0); + return 0; +} diff --git a/libc-top-half/musl/src/env/getenv.c b/libc-top-half/musl/src/env/getenv.c new file mode 100644 index 0000000..346c333 --- /dev/null +++ b/libc-top-half/musl/src/env/getenv.c @@ -0,0 +1,20 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +char *getenv(const char *name) +{ +#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init. +#else +// This specialized header is included within the function body to arranges for +// the environment variables to be lazily initialized. It redefined `__environ`, +// so don't remove or reorder it with respect to other code. +#include "wasi/libc-environ-compat.h" +#endif + size_t l = __strchrnul(name, '=') - name; + if (l && !name[l] && __environ) + for (char **e = __environ; *e; e++) + if (!strncmp(name, *e, l) && l[*e] == '=') + return *e + l+1; + return 0; +} diff --git a/libc-top-half/musl/src/env/putenv.c b/libc-top-half/musl/src/env/putenv.c new file mode 100644 index 0000000..0d59895 --- /dev/null +++ b/libc-top-half/musl/src/env/putenv.c @@ -0,0 +1,53 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static void dummy(char *old, char *new) {} +weak_alias(dummy, __env_rm_add); + +int __putenv(char *s, size_t l, char *r) +{ +#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init. +#else +// This specialized header is included within the function body to arranges for +// the environment variables to be lazily initialized. It redefined `__environ`, +// so don't remove or reorder it with respect to other code. +#include "wasi/libc-environ-compat.h" +#endif + size_t i=0; + if (__environ) { + for (char **e = __environ; *e; e++, i++) + if (!strncmp(s, *e, l+1)) { + char *tmp = *e; + *e = s; + __env_rm_add(tmp, r); + return 0; + } + } + static char **oldenv; + char **newenv; + if (__environ == oldenv) { + newenv = realloc(oldenv, sizeof *newenv * (i+2)); + if (!newenv) goto oom; + } else { + newenv = malloc(sizeof *newenv * (i+2)); + if (!newenv) goto oom; + if (i) memcpy(newenv, __environ, sizeof *newenv * i); + free(oldenv); + } + newenv[i] = s; + newenv[i+1] = 0; + __environ = oldenv = newenv; + if (r) __env_rm_add(0, r); + return 0; +oom: + free(r); + return -1; +} + +int putenv(char *s) +{ + size_t l = __strchrnul(s, '=') - s; + if (!l || !s[l]) return unsetenv(s); + return __putenv(s, l, 0); +} diff --git a/libc-top-half/musl/src/env/secure_getenv.c b/libc-top-half/musl/src/env/secure_getenv.c new file mode 100644 index 0000000..72322f8 --- /dev/null +++ b/libc-top-half/musl/src/env/secure_getenv.c @@ -0,0 +1,8 @@ +#define _GNU_SOURCE +#include <stdlib.h> +#include "libc.h" + +char *secure_getenv(const char *name) +{ + return libc.secure ? NULL : getenv(name); +} diff --git a/libc-top-half/musl/src/env/setenv.c b/libc-top-half/musl/src/env/setenv.c new file mode 100644 index 0000000..c5226b6 --- /dev/null +++ b/libc-top-half/musl/src/env/setenv.c @@ -0,0 +1,42 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +void __env_rm_add(char *old, char *new) +{ + static char **env_alloced; + static size_t env_alloced_n; + for (size_t i=0; i < env_alloced_n; i++) + if (env_alloced[i] == old) { + env_alloced[i] = new; + free(old); + return; + } else if (!env_alloced[i] && new) { + env_alloced[i] = new; + new = 0; + } + if (!new) return; + char **t = realloc(env_alloced, sizeof *t * (env_alloced_n+1)); + if (!t) return; + (env_alloced = t)[env_alloced_n++] = new; +} + +int setenv(const char *var, const char *value, int overwrite) +{ + char *s; + size_t l1, l2; + + if (!var || !(l1 = __strchrnul(var, '=') - var) || var[l1]) { + errno = EINVAL; + return -1; + } + if (!overwrite && getenv(var)) return 0; + + l2 = strlen(value); + s = malloc(l1+l2+2); + if (!s) return -1; + memcpy(s, var, l1); + s[l1] = '='; + memcpy(s+l1+1, value, l2+1); + return __putenv(s, l1, s); +} diff --git a/libc-top-half/musl/src/env/unsetenv.c b/libc-top-half/musl/src/env/unsetenv.c new file mode 100644 index 0000000..40f0eea --- /dev/null +++ b/libc-top-half/musl/src/env/unsetenv.c @@ -0,0 +1,35 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +static void dummy(char *old, char *new) {} +weak_alias(dummy, __env_rm_add); + +int unsetenv(const char *name) +{ + size_t l = __strchrnul(name, '=') - name; + if (!l || name[l]) { + errno = EINVAL; + return -1; + } +#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init. +#else +// This specialized header is included within the function body to arranges for +// the environment variables to be lazily initialized. It redefined `__environ`, +// so don't remove or reorder it with respect to other code. +#include "wasi/libc-environ-compat.h" +#endif + if (__environ) { + char **e = __environ, **eo = e; + for (; *e; e++) + if (!strncmp(name, *e, l) && l[*e] == '=') + __env_rm_add(*e, 0); + else if (eo != e) + *eo++ = *e; + else + eo++; + if (eo != e) *eo = 0; + } + return 0; +} |