Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lwmalloc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore the install dir
#lwmalloc-main/
*.zip
*.so
test
toolchain-glibc
10 changes: 10 additions & 0 deletions src/lwmalloc/cleanup.lwmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

source config.lwmalloc

rm -rf "${BUILD_FOLDER}"

make clean
10 changes: 10 additions & 0 deletions src/lwmalloc/cleanup.mdnsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

source config.mdnsd

rm -rf "${BUILD_FOLDER}"

make clean
28 changes: 28 additions & 0 deletions src/lwmalloc/compile.lwmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

source config.lwmalloc

cd "${BUILD_FOLDER}"

export GLIBC_TOOTLCHAIN=$PWD/../toolchain-glibc
export STAGING_DIR="${GLIBC_TOOTLCHAIN}"
export CROSSPATH="${GLIBC_TOOTLCHAIN}/toolchain/bin"
export PATH=${CROSSPATH}:${PATH}

export TARGET=arm-openwrt-linux
export CROSS=arm-openwrt-linux
export BUILD=x86_64-pc-linux-gnu

export CROSSPREFIX=${CROSS}-

export STRIP=${CROSSPREFIX}strip
export CC=${CROSSPREFIX}gcc


make -j $(nproc) || exit 1
make buildtest

${STRIP} liblwmalloc.so || exit 1
7 changes: 7 additions & 0 deletions src/lwmalloc/config.lwmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

VERSION="main"
MAIN_URL="https://github.com/taehyeon-masu/lwmalloc/archive/refs/heads/main.zip"
PACKAGE="lwmalloc-${VERSION}"
ARCHIVE="${PACKAGE}.zip"
BUILD_FOLDER="lwmalloc-${VERSION}"
17 changes: 17 additions & 0 deletions src/lwmalloc/init.lwmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

source config.lwmalloc

rm -rf ./_install


#### Need glibc for lwmalloc cause sbrk has some issue with musl ####

git clone https://github.com/lindenis-org/lindenis-v536-prebuilt /tmp/lindenis-v536-prebuilt || true
mkdir -p ./toolchain-glibc
cp -r /tmp/lindenis-v536-prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain ./toolchain-glibc
12 changes: 12 additions & 0 deletions src/lwmalloc/install.lwmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

source config.lwmalloc

mkdir -p ../../build/yi-hack/lib/

rsync -a ./${BUILD_FOLDER}/liblwmalloc.so ../../build/yi-hack/lib/

# TODO SET /etc/ld.so.preload for loading for using it everywhere ==> need futher tests
21 changes: 21 additions & 0 deletions src/lwmalloc/lwmalloc-main/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#CC=gcc
CFLAGS=-fPIC -Wall -Wextra -O2 -DLIB32
LDFLAGS=-shared
TARGET_LIB=liblwmalloc.so
SOURCES=lwmalloc.c
OBJECTS=$(SOURCES:.c=.o)
.PHONY: all clean

all: $(TARGET_LIB)

$(TARGET_LIB): $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $^

%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f $(TARGET_LIB) $(OBJECTS)

buildtest:
$(CC) $(CFLAGS) -o test test.c
46 changes: 46 additions & 0 deletions src/lwmalloc/lwmalloc-main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# LWMalloc: A Lightweight Dynamic Memory Allocator for Resource-Constrained Environments
LWMalloc is a dynamic memory allocator for resource-constrained systems. It can be injected at runtime to replace malloc/calloc/realloc/free via LD_PRELOAD.

## FILES
----------------------------------------------------------------------
- lwmalloc.c : allocator implementation
- Makefile : build script (produces a shared object)

## BUILD
----------------------------------------------------------------------
```bash
make
# Output: lwmalloc.so
```

## USE AT RUNTIME (LD_PRELOAD)
----------------------------------------------------------------------
Per-process:
LD_PRELOAD=/absolute/or/relative/path/to/lwmalloc.so your_program [args]

Session-wide:
export LD_PRELOAD=/absolute/path/to/lwmalloc.so
your_program [args]
unset LD_PRELOAD

Tip: Prefer an absolute path for LD_PRELOAD.

## QUICK TEST
----------------------------------------------------------------------
A simple test program (test.c) is included in this repository.

1) Build the test:
```bash
gcc -O2 -o test test.c
```

3) Run the test with LWMalloc preloaded:
```bash
LD_PRELOAD=./lwmalloc.so ./test
```

## NOTES & LIMITATIONS
----------------------------------------------------------------------
- Static binaries and some setuid programs may ignore LD_PRELOAD due to loader/security policies.
- Do not preload multiple user-space allocators at the same time.
- If behavior is unexpected, unset LD_PRELOAD and re-run to compare against the system allocator.
Loading