From 4c71e748a499ec60b54141109ae9014abf6bcdd3 Mon Sep 17 00:00:00 2001 From: Vikas Tikoo Date: Tue, 20 Jun 2023 01:25:13 +0000 Subject: [PATCH 1/4] Musl loader updates: relr support and relocation of init_array entries libcrypto from openssl 3 has relr relocations which are not supported by musl loader. It also has init_array entries which are not relocated by the loader. - PR adds support for relocating init_array entries. - relr relocations are already supported by musl 1.2.24, I have ported just those changes in this PR. Upstream musl commits related to RELR relocations: https://github.com/bminor/musl/commit/d32dadd60efb9d3b255351a3b532f8e4c3dd0db1 https://github.com/bminor/musl/commit/bf99258564fd5b58974d93201ab61506eb8cb03e Signed-off-by: Vikas Tikoo --- third_party/musl/crt/patch.diff | 137 ++++++++++++++++++++++++++++---- 1 file changed, 120 insertions(+), 17 deletions(-) diff --git a/third_party/musl/crt/patch.diff b/third_party/musl/crt/patch.diff index 650eb1790..87963be8e 100644 --- a/third_party/musl/crt/patch.diff +++ b/third_party/musl/crt/patch.diff @@ -126,6 +126,32 @@ index 92d5c179..e94deece 100644 } #define VDSO_USEFUL +diff --git a/include/elf.h b/include/elf.h +index 549f92c1..5c4740b9 100644 +--- a/include/elf.h ++++ b/include/elf.h +@@ -385,7 +385,8 @@ typedef struct { + #define SHT_PREINIT_ARRAY 16 + #define SHT_GROUP 17 + #define SHT_SYMTAB_SHNDX 18 +-#define SHT_NUM 19 ++#define SHT_RELR 19 ++#define SHT_NUM 20 + #define SHT_LOOS 0x60000000 + #define SHT_GNU_ATTRIBUTES 0x6ffffff5 + #define SHT_GNU_HASH 0x6ffffff6 +@@ -751,7 +752,10 @@ typedef struct { + #define DT_PREINIT_ARRAY 32 + #define DT_PREINIT_ARRAYSZ 33 + #define DT_SYMTAB_SHNDX 34 +-#define DT_NUM 35 ++#define DT_RELRSZ 35 ++#define DT_RELR 36 ++#define DT_RELRENT 37 ++#define DT_NUM 38 + #define DT_LOOS 0x6000000d + #define DT_HIOS 0x6ffff000 + #define DT_LOPROC 0x70000000 diff --git a/include/math.h b/include/math.h index 14f28ec8..09b5790d 100644 --- a/include/math.h @@ -261,7 +287,7 @@ index 20d50f2c..17cd6ac3 100644 +{ +} diff --git a/ldso/dynlink.c b/ldso/dynlink.c -index afec985a..9f9b618d 100644 +index afec985a..9a9b5909 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -3,6 +3,7 @@ @@ -303,7 +329,17 @@ index afec985a..9f9b618d 100644 struct debug { int ver; void *head; -@@ -217,6 +236,14 @@ static int search_vec(size_t *v, size_t *r, size_t key) +@@ -204,7 +223,8 @@ static void decode_vec(size_t *v, size_t *a, size_t cnt) + size_t i; + for (i=0; ibase; ++ size_t *reloc_addr; ++ for (; relr_size; relr++, relr_size-=sizeof(size_t)) ++ if ((relr[0]&1) == 0) { ++ reloc_addr = laddr(dso, relr[0]); ++ *reloc_addr++ += (size_t)base; ++ } else { ++ int i = 0; ++ for (size_t bitmap=relr[0]; (bitmap>>=1); i++) ++ if (bitmap&1) ++ reloc_addr[i] += (size_t)base; ++ reloc_addr += 8*sizeof(size_t)-1; ++ } ++} ++ + static void redo_lazy_relocs() + { + struct dso *p = lazy_head, *next; +@@ -658,6 +703,25 @@ static void *map_library(int fd, struct dso *dso) ph->p_memsz < DEFAULT_STACK_MAX ? ph->p_memsz : DEFAULT_STACK_MAX; } @@ -344,7 +404,7 @@ index afec985a..9f9b618d 100644 } if (ph->p_type != PT_LOAD) continue; nsegs++; -@@ -895,6 +941,7 @@ static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) +@@ -895,6 +959,7 @@ static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) static void decode_dyn(struct dso *p) { size_t dyn[DYN_CNT]; @@ -352,7 +412,7 @@ index afec985a..9f9b618d 100644 decode_vec(p->dynv, dyn, DYN_CNT); p->syms = laddr(p, dyn[DT_SYMTAB]); p->strings = laddr(p, dyn[DT_STRTAB]); -@@ -906,8 +953,21 @@ static void decode_dyn(struct dso *p) +@@ -906,8 +971,21 @@ static void decode_dyn(struct dso *p) p->rpath_orig = p->strings + dyn[DT_RUNPATH]; if (dyn[0]&(1<got = laddr(p, dyn[DT_PLTGOT]); @@ -375,7 +435,7 @@ index afec985a..9f9b618d 100644 if (search_vec(p->dynv, dyn, DT_VERSYM)) p->versym = laddr(p, *dyn); } -@@ -1043,38 +1103,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by) +@@ -1043,38 +1121,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by) fd = path_open(name, p->rpath, buf, sizeof buf); } if (fd == -1) { @@ -415,7 +475,7 @@ index afec985a..9f9b618d 100644 fd = path_open(name, sys_path, buf, sizeof buf); } pathname = buf; -@@ -1098,6 +1127,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by) +@@ -1098,6 +1145,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by) map = noload ? 0 : map_library(fd, &temp_dso); close(fd); if (!map) return 0; @@ -423,7 +483,37 @@ index afec985a..9f9b618d 100644 /* Avoid the danger of getting two versions of libc mapped into the * same process when an absolute pathname was used. The symbols -@@ -1683,6 +1713,13 @@ void __dls2b(size_t *sp, size_t *auxv) +@@ -1334,6 +1382,8 @@ static void reloc_all(struct dso *p) + 2+(dyn[DT_PLTREL]==DT_RELA)); + do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2); + do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3); ++ if (!DL_FDPIC) ++ do_relr_relocs(p, laddr(p, dyn[DT_RELR]), dyn[DT_RELRSZ]); + + if (head != &ldso && p->relro_start != p->relro_end && + mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ) +@@ -1498,7 +1548,19 @@ static void do_init_fini(struct dso **queue) + if (dyn[0] & (1<base && ++ (*fn + (size_t)p->base) < (size_t)(p->base + p->map_len)) ++ { ++ size_t fn_relocated = *fn + (size_t)p->base; ++ ((void (*)(void))fn_relocated)(); ++ } ++ else ++ ((void (*)(void))*fn)(); ++ fn++; ++ } + } + + pthread_mutex_lock(&init_fini_lock); +@@ -1683,6 +1745,13 @@ void __dls2b(size_t *sp, size_t *auxv) else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv); } @@ -437,7 +527,7 @@ index afec985a..9f9b618d 100644 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc * fully functional. Its job is to load (if not already loaded) and * process dependencies and relocations for the main application and -@@ -1716,6 +1753,10 @@ void __dls3(size_t *sp, size_t *auxv) +@@ -1716,6 +1785,10 @@ void __dls3(size_t *sp, size_t *auxv) env_path = getenv("LD_LIBRARY_PATH"); env_preload = getenv("LD_PRELOAD"); } @@ -448,7 +538,7 @@ index afec985a..9f9b618d 100644 /* If the main program was already loaded by the kernel, * AT_PHDR will point to some location other than the dynamic -@@ -1797,6 +1838,7 @@ void __dls3(size_t *sp, size_t *auxv) +@@ -1797,6 +1870,7 @@ void __dls3(size_t *sp, size_t *auxv) dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); _exit(1); } @@ -456,7 +546,7 @@ index afec985a..9f9b618d 100644 close(fd); ldso.name = ldname; app.name = argv[0]; -@@ -1936,6 +1978,7 @@ void __dls3(size_t *sp, size_t *auxv) +@@ -1936,6 +2010,7 @@ void __dls3(size_t *sp, size_t *auxv) /* Determine if malloc was interposed by a replacement implementation * so that calloc and the memalign family can harden against the * possibility of incomplete replacement. */ @@ -464,7 +554,7 @@ index afec985a..9f9b618d 100644 if (find_sym(head, "malloc", 1).dso != &ldso) __malloc_replaced = 1; -@@ -1955,7 +1998,13 @@ void __dls3(size_t *sp, size_t *auxv) +@@ -1955,7 +2030,13 @@ void __dls3(size_t *sp, size_t *auxv) errno = 0; @@ -478,7 +568,7 @@ index afec985a..9f9b618d 100644 for(;;); } -@@ -2058,6 +2107,10 @@ void *dlopen(const char *file, int mode) +@@ -2058,6 +2139,10 @@ void *dlopen(const char *file, int mode) pthread_mutex_lock(&init_fini_lock); if (!p->constructed) ctor_queue = queue_ctors(p); pthread_mutex_unlock(&init_fini_lock); @@ -489,7 +579,7 @@ index afec985a..9f9b618d 100644 if (!p->relocated && (mode & RTLD_LAZY)) { prepare_lazy(p); for (i=0; p->deps[i]; i++) -@@ -2100,6 +2153,7 @@ end: +@@ -2100,6 +2185,7 @@ end: free(ctor_queue); } pthread_setcancelstate(cs, 0); @@ -497,7 +587,7 @@ index afec985a..9f9b618d 100644 return p; } -@@ -2272,11 +2326,16 @@ no_redir: +@@ -2272,11 +2358,16 @@ no_redir: return __dlsym(p, s, ra); } @@ -514,7 +604,7 @@ index afec985a..9f9b618d 100644 for(current = head; current;) { info.dlpi_addr = (uintptr_t)current->base; info.dlpi_name = current->name; -@@ -2289,12 +2348,11 @@ int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void +@@ -2289,12 +2380,11 @@ int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void ret = (callback)(&info, sizeof (info), data); @@ -529,7 +619,7 @@ index afec985a..9f9b618d 100644 return ret; } -@@ -2302,7 +2360,7 @@ static void error(const char *fmt, ...) +@@ -2302,7 +2392,7 @@ static void error(const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -766,6 +856,19 @@ index cbabde47..e78515e4 100644 +size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t); #endif +diff --git a/src/internal/dynlink.h b/src/internal/dynlink.h +index 764e3a1a..0171c71a 100644 +--- a/src/internal/dynlink.h ++++ b/src/internal/dynlink.h +@@ -93,7 +93,7 @@ struct fdpic_dummy_loadmap { + #endif + + #define AUX_CNT 32 +-#define DYN_CNT 32 ++#define DYN_CNT 37 + + typedef void (*stage2_func)(unsigned char *, size_t *); + diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h index 5742dfc5..fe718786 100644 --- a/src/internal/pthread_impl.h From 76aaa8adb5b36ab0115c7a29e7c76eeb9175255b Mon Sep 17 00:00:00 2001 From: Vikas Tikoo Date: Thu, 22 Jun 2023 21:02:44 +0000 Subject: [PATCH 2/4] Increase memory and limit cpus Signed-off-by: Vikas Tikoo --- solutions/python_app/config.json | 3 ++- solutions/python_webserver/Makefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/solutions/python_app/config.json b/solutions/python_app/config.json index c9b11c5f3..b34e3ea7f 100644 --- a/solutions/python_app/config.json +++ b/solutions/python_app/config.json @@ -9,7 +9,8 @@ // Mystikos specific values // The heap size of the user application. Increase this setting if your app experienced OOM. - "MemorySize": "2g", + "MemorySize": "4g", + "MaxAffinityCPUs": 4, // The path to the entry point application in rootfs "ApplicationPath": "/miniconda/bin/python3", // The parameters to the entry point application diff --git a/solutions/python_webserver/Makefile b/solutions/python_webserver/Makefile index b7453837a..b81482282 100644 --- a/solutions/python_webserver/Makefile +++ b/solutions/python_webserver/Makefile @@ -9,7 +9,7 @@ ifdef STRACE OPTS += --strace endif -OPTS += --memory-size=1g --thread-stack-size=1m +OPTS += --memory-size=2g --thread-stack-size=1m --max-affinity-cpus 4 ifdef PERF OPTS += --perf From 47859ef7e7bbb93d5c8b8a362cc9e503c6b83ef7 Mon Sep 17 00:00:00 2001 From: Zijie Wu Date: Thu, 22 Jun 2023 21:36:54 +0000 Subject: [PATCH 3/4] Increase memsize for python flask --- solutions/python_flask_tests/Makefile | 4 ++-- solutions/python_flask_tests/config.json | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/solutions/python_flask_tests/Makefile b/solutions/python_flask_tests/Makefile index c65617f72..57de96429 100644 --- a/solutions/python_flask_tests/Makefile +++ b/solutions/python_flask_tests/Makefile @@ -18,11 +18,11 @@ rootfs: appdir $(MYST) mkcpio appdir rootfs run: - $(RUNTEST) $(MYST_EXEC) $(OPTS) rootfs /usr/local/bin/python /usr/local/bin/pytest --app-config-path config.json + $(RUNTEST) $(MYST_EXEC) --app-config-path config.json $(OPTS) rootfs /usr/local/bin/python /usr/local/bin/pytest run_single: # Run single test for debugging - $(RUNTEST) $(MYST_EXEC) $(OPTS) rootfs /usr/local/bin/python /usr/local/bin/pytest tests/test_instance_config.py::test_egg_installed_paths --app-config-path config.json + $(RUNTEST) $(MYST_EXEC) --app-config-path config.json $(OPTS) rootfs /usr/local/bin/python /usr/local/bin/pytest tests/test_instance_config.py::test_egg_installed_paths clean: rm -rf appdir rootfs diff --git a/solutions/python_flask_tests/config.json b/solutions/python_flask_tests/config.json index 3ed5f4c24..e41d6ac39 100644 --- a/solutions/python_flask_tests/config.json +++ b/solutions/python_flask_tests/config.json @@ -3,7 +3,8 @@ "Debug": 1, "ProductID": 1, "SecurityVersion": 1, - "MemorySize": "256m", + "MemorySize": "1g", + "MaxAffinityCPUs": 4, "CurrentWorkingDirectory": "/app/flask", "ApplicationPath": "/usr/local/bin/python", "HostApplicationParameters": 1, From 4dc20030d2005c8b7fbe15da303175d78d162cf1 Mon Sep 17 00:00:00 2001 From: Vikas Tikoo Date: Fri, 23 Jun 2023 20:32:19 +0000 Subject: [PATCH 4/4] Temporarily disable numpy_core_tests and python_flask_tests Signed-off-by: Vikas Tikoo --- solutions/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/Makefile b/solutions/Makefile index 03b3a1e7e..02c809327 100644 --- a/solutions/Makefile +++ b/solutions/Makefile @@ -18,11 +18,11 @@ DIRS += msgpack_c DIRS += dotnet DIRS += aspnet_core -DIRS += numpy_core_tests +#DIRS += numpy_core_tests DIRS += cpython-tests DIRS += memcached -DIRS += python_flask_tests +#DIRS += python_flask_tests DIRS += python_web_frameworks DIRS += python_webserver ifndef MYST_SKIP_PR_TEST