From 9b9bea921f479d1c166418acb9473a5aa01c2f80 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Wed, 26 Aug 2015 07:33:06 +0200 Subject: [PATCH] Do not instrument malloc implementation variables Since the heap is reconstructed upon state transfer, the old malloc state is discarded. In order to avoid state transfer errors, we can and in fact must discard the internal state of the malloc implementation. This patch achieves this by using the sectionify pass to mark the variables in the libminc malloc object as state that must be skipped during state transfer. Change-Id: Ie330f582c8bd45f37a878ea41fa0f9d4a18045e1 --- minix/lib/libminc/Makefile | 9 +++++++-- minix/llvm/include/magic_common.h | 2 ++ minix/llvm/passes/magic/MagicPass.cpp | 2 +- share/mk/bsd.lib.mk | 5 +++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/minix/lib/libminc/Makefile b/minix/lib/libminc/Makefile index 3239fa517..ece7b4758 100644 --- a/minix/lib/libminc/Makefile +++ b/minix/lib/libminc/Makefile @@ -263,6 +263,13 @@ CLEANFILES+= ${f:C/\.o/.bc/} CPPFLAGS.${i}+= -I${LIBCDIR}/stdlib .endfor +CPPFLAGS.malloc.c+= -D_LIBSYS +# Avoid magic instrumentation of the malloc data variables, since the heap is +# reconstructed upon state transfer. We do need to instrument the malloc +# functions, since we need to hook their mmap/munmap calls. +SECTIONIFY.malloc.c+= -sectionify-no-override \ + -sectionify-data-section-map=.*/magic_malloc_data + .for f in \ strcspn.o ${f} ${f:C/\.o/.bc/}: ${LIBCDIR}/string/${f:C/\.o/.c/} @@ -294,8 +301,6 @@ CLEANFILES+= ${f:C/\.o/.bc/} .endif # ${USE_BITCODE:Uno} == "yes" .endfor -CPPFLAGS.malloc.c+= -D_LIBSYS - .for f in \ brksize.o _do_kernel_call_intr.o get_minix_kerninfo.o _ipc.o ucontext.o ${f} ${f:C/\.o/.bc/}: ${LIBMINIXCARCHDIR}/sys/${f:C/\.o/.S/} diff --git a/minix/llvm/include/magic_common.h b/minix/llvm/include/magic_common.h index 7f9d2f1ed..7b1048d5b 100644 --- a/minix/llvm/include/magic_common.h +++ b/minix/llvm/include/magic_common.h @@ -55,6 +55,8 @@ #define MAGIC_HIDDEN_ARRAY_PREFIX ".arr.magic" #define MAGIC_HIDDEN_STR_PREFIX ".str.magic" +#define MAGIC_MALLOC_VARS_SECTION_PREFIX "magic_malloc_data" + /* Magic configuration. */ #ifndef MAGIC_OUTPUT_CTL #define MAGIC_OUTPUT_CTL 0 diff --git a/minix/llvm/passes/magic/MagicPass.cpp b/minix/llvm/passes/magic/MagicPass.cpp index 9badbddbc..3fb9d201d 100644 --- a/minix/llvm/passes/magic/MagicPass.cpp +++ b/minix/llvm/passes/magic/MagicPass.cpp @@ -68,7 +68,7 @@ MMAPCtlFunction("magic-mmap-ctlfunc", static cl::opt MagicDataSections("magic-data-sections", cl::desc("Specify all the colon-separated magic data section regexes not to instrument"), - cl::init("^" MAGIC_STATIC_VARS_SECTION_PREFIX ".*$:^" UNBL_SECTION_PREFIX ".*$"), cl::NotHidden, cl::ValueRequired); + cl::init("^" MAGIC_STATIC_VARS_SECTION_PREFIX ".*$:^" UNBL_SECTION_PREFIX ".*$:^" MAGIC_MALLOC_VARS_SECTION_PREFIX ".*$"), cl::NotHidden, cl::ValueRequired); static cl::opt MagicFunctionSections("magic-function-sections", diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk index 08e3ec2ae..1eb4f2d7f 100644 --- a/share/mk/bsd.lib.mk +++ b/share/mk/bsd.lib.mk @@ -207,12 +207,17 @@ SHLIB_SHFLAGS+= -L ${DESTDIR}/usr/lib SHLIB_SHFLAGS+= -Wl,-plugin=${GOLD_PLUGIN} \ -Wl,-plugin-opt=-disable-opt +SECTIONIFYPASS?=${NETBSDSRCDIR}/minix/llvm/bin/sectionify.so + .S.bc: ${.TARGET:.bc=.o} rm -f ${.TARGET} ln ${.TARGET:.bc=.o} ${.TARGET} .c.bc: ${_MKTARGET_COMPILE} ${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} -o ${.TARGET} -flto + if [ -n '${SECTIONIFY.${.IMPSRC:T}}' ]; then \ + ${OPT} -load ${SECTIONIFYPASS} -sectionify ${SECTIONIFY.${.IMPSRC:T}} -o ${.TARGET}.tmp ${.TARGET} && mv -f ${.TARGET}.tmp ${.TARGET}; \ + fi .cc.bc .cxx.bc .cpp.bc: ${_MKTARGET_COMPILE} -- 2.44.0