build: add libucontext test program

pull/10/head
William Pitcock 2018-01-30 03:53:04 +00:00
parent da10e5a66d
commit 29225e0974
3 changed files with 72 additions and 7 deletions

1
.gitignore vendored
View File

@ -34,4 +34,5 @@ Makefile.in
*.o *.o
test_libucontext test_libucontext
libucontext.so
libucontext.so.* libucontext.so.*

View File

@ -13,17 +13,18 @@ LIBUCONTEXT_S_SRC = \
LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o} LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o}
LIBUCONTEXT_SOVERSION = 0 LIBUCONTEXT_SOVERSION = 0
LIBUCONTEXT_NAME = libucontext.so.${LIBUCONTEXT_SOVERSION} LIBUCONTEXT_NAME = libucontext.so
LIBUCONTEXT_PATH = /lib/${LIBUCONTEXT_NAME} LIBUCONTEXT_SONAME = libucontext.so.${LIBUCONTEXT_SOVERSION}
LIBUCONTEXT_PATH = /lib/${LIBUCONTEXT_SONAME}
all: ${LIBUCONTEXT_NAME} all: ${LIBUCONTEXT_SONAME}
${LIBUCONTEXT_NAME}: ${LIBUCONTEXT_OBJ} ${LIBUCONTEXT_NAME}: ${LIBUCONTEXT_OBJ}
$(CC) -o ${LIBUCONTEXT_NAME} -Wl,-soname,${LIBUCONTEXT_NAME} \ $(CC) -o ${LIBUCONTEXT_NAME} -Wl,-soname,${LIBUCONTEXT_SONAME} \
-shared ${LIBUCONTEXT_OBJ} -shared ${LIBUCONTEXT_OBJ}
${LOADER_NAME}: ${LOADER_OBJ} ${LIBUCONTEXT_SONAME}: ${LIBUCONTEXT_NAME}
$(CC) -o ${LOADER_NAME} -fPIE -static ${LOADER_OBJ} ln -sf ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_SONAME}
.c.o: .c.o:
$(CC) -std=c99 -D_BSD_SOURCE -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $< $(CC) -std=c99 -D_BSD_SOURCE -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $<
@ -32,7 +33,16 @@ ${LOADER_NAME}: ${LOADER_OBJ}
$(CC) -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $< $(CC) -fPIC -DPIC ${CFLAGS} ${CPPFLAGS} -c -o $@ $<
clean: clean:
rm -f ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_OBJ} rm -f ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_SONAME} ${LIBUCONTEXT_OBJ}
install: all install: all
install -D -m755 ${LIBUCONTEXT_NAME} ${DESTDIR}/${LIBUCONTEXT_PATH} install -D -m755 ${LIBUCONTEXT_NAME} ${DESTDIR}/${LIBUCONTEXT_PATH}
ln -sf ${LIBUCONTEXT_SONAME} ${DESTDIR}/lib/${LIBUCONTEXT_NAME}
check: test_libucontext ${LIBUCONTEXT_SONAME}
env LD_LIBRARY_PATH=. ./test_libucontext
test_libucontext: ${LIBUCONTEXT_NAME}
$(CC) -std=c99 -D_BSD_SOURCE $@.c -o $@ ${LIBUCONTEXT_NAME}
.PHONY: check

54
test_libucontext.c Normal file
View File

@ -0,0 +1,54 @@
/*
* libucontext test program based on POSIX example program.
* Public domain.
*/
#include <stdio.h>
#include <ucontext.h>
#include <stdint.h>
#include <string.h>
static ucontext_t ctx[3];
static void f1 (void) {
printf("start f1\n");
swapcontext(&ctx[1], &ctx[2]);
printf("finish f1\n");
}
static void f2 (void) {
printf("start f2\n");
swapcontext(&ctx[2], &ctx[1]);
printf("finish f2\n");
}
int main (int argc, const char *argv[]) {
char st1[8192];
char st2[8192];
memset(st1, 'A', sizeof st1);
memset(st2, 'B', sizeof st2);
getcontext(&ctx[1]);
ctx[1].uc_stack.ss_sp = st1;
ctx[1].uc_stack.ss_size = sizeof st1;
ctx[1].uc_link = &ctx[0];
makecontext(&ctx[1], f1, 0);
getcontext(&ctx[2]);
ctx[2].uc_stack.ss_sp = st2;
ctx[2].uc_stack.ss_size = sizeof st2;
ctx[2].uc_link = &ctx[1];
makecontext(&ctx[2], f2, 0);
swapcontext(&ctx[0], &ctx[2]);
return 0;
}