Skip to content

Commit b9f1384

Browse files
authored
Rework regtest runner (#92)
* Comply better with C99 where GCC is not specified * Rework regtest runner The C++ script is rewritten in Makefile and shell script for more packaging-friendly debugging. * Properly invalidate regtest cached artifacts * Add GitHub Action
1 parent eddeab3 commit b9f1384

11 files changed

Lines changed: 2694 additions & 2582 deletions

File tree

.github/workflows/debug.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Debugging build and test
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
build:
7+
runs-on: ubuntu-slim
8+
steps:
9+
- uses: actions/checkout@v4
10+
- name: Build (debugging mode)
11+
run: make debug
12+
- name: Run make check
13+
run: make check
14+
- name: Build regression test runner
15+
run: make -C test/regtest
16+
- name: Run regression test suite
17+
run: cd test/regtest && ./regtest

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,22 @@
22
*.o
33
*.a
44
src/e9patch/e9loader_*.c
5+
test/regtest/*.so
6+
test/regtest/*.exe
7+
test/regtest/*.log
8+
test/regtest/*.out
9+
test/regtest/*.diff
10+
test/regtest/bugs
11+
test/regtest/dl
12+
test/regtest/fini
13+
test/regtest/init
14+
test/regtest/inst
15+
test/regtest/patch
16+
test/regtest/regtest
17+
test/regtest/test
18+
test/regtest/test.libc
19+
test/regtest/test.pie
20+
test/regtest/test_c
21+
test/regtest/test_c.debug
522
/e9patch
623
/e9tool

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all clean install dev release debug sanitize
1+
.PHONY: all clean check install dev release debug sanitize
22

33
#########################################################################
44
# BUILD COMMON
@@ -66,6 +66,7 @@ clean:
6666
$(MAKE) -C contrib/zydis clean
6767
rm -rf $(E9PATCH_OBJS) $(E9TOOL_OBJS) e9patch e9tool \
6868
src/e9patch/e9loader_*.c e9loader_*.o e9loader_*.bin
69+
$(MAKE) -C test/regtest clean
6970

7071
src/e9patch/e9loader_elf.c: src/e9patch/e9loader_elf.cpp
7172
$(CXX) -std=c++11 -Wall -fno-stack-protector -Wno-unused-function -fPIC \
@@ -82,6 +83,9 @@ src/e9patch/e9loader_pe.c: src/e9patch/e9loader_pe.cpp
8283
src/e9patch/e9elf.o: src/e9patch/e9loader_elf.c
8384
src/e9patch/e9pe.o: src/e9patch/e9loader_pe.c
8485

86+
check: all
87+
$(MAKE) -C test/regtest check
88+
8589
install: all
8690
install -d "$(DESTDIR)$(PREFIX)/bin"
8791
install -m 755 e9patch "$(DESTDIR)$(PREFIX)/bin/e9patch"

test/regtest/Makefile

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,81 @@
1+
.POSIX:
2+
.PHONY: all clean check
3+
4+
E9TOOL ?= ../../e9tool
5+
E9COMPILE ?= ../../e9compile.sh
6+
17
FCF_NONE := $(shell \
28
if gcc -fcf-protection=none --version 2>&1 | grep -q 'unrecognized'; \
39
then true; \
410
else echo -fcf-protection=none; fi)
511

6-
all:
7-
gcc -x assembler-with-cpp -o test test.s -no-pie -nostdlib \
12+
BASE ::= test test.pie bugs test.libc libtest.so test_c test_c.debug example.so
13+
TRAMPOLINE ::= inst patch dl init fini
14+
IN ::= $(wildcard *.in)
15+
EXE ::= $(IN:.in=.exe)
16+
17+
all: regtest $(BASE) $(TRAMPOLINE)
18+
19+
clean:
20+
rm -f regtest
21+
rm -f $(BASE) $(TRAMPOLINE) $(EXE)
22+
rm -f *.out *.log *.diff
23+
24+
regtest: regtest.cpp
25+
$(CXX) -std=c++11 -O2 -g -fPIC -pie $< -o $@
26+
27+
%.exe: in=$(shell head -1 $<)
28+
%.exe: %.in $(BASE) $(TRAMPOLINE) $(E9TOOL)
29+
$(E9TOOL) $(E9TOOL_OPTIONS) -M 'addr >= &"entry"' $(in)\
30+
-E data..data_END -E data2...text -E .text..begin -o $@
31+
32+
test: test.s
33+
$(CC) -x assembler-with-cpp -o $@ $< -no-pie -nostdlib \
834
-Wl,--section-start=.text=0xa000000 -Wl,--section-start=.bss=0xc000000 \
935
-Wl,-z -Wl,max-page-size=4096 -DPIE=0
10-
gcc -x assembler-with-cpp -o test.pie test.s -pie -nostdlib \
36+
37+
test.pie: test.s
38+
$(CC) -x assembler-with-cpp -o $@ $< -pie -nostdlib \
1139
-Wl,--section-start=.text=0xa000000 -Wl,--section-start=.bss=0xc000000 \
1240
-Wl,-z -Wl,max-page-size=4096 -DPIE=1 \
1341
-Wl,--export-dynamic
14-
gcc -x assembler-with-cpp -o bugs bugs.s -no-pie -nostdlib \
42+
43+
bugs: bugs.s
44+
$(CC) -x assembler-with-cpp -o $@ $< -no-pie -nostdlib \
1545
-Wl,--section-start=.text=0xa000000 -Wl,--section-start=.bss=0xc000000 \
1646
-Wl,-z -Wl,max-page-size=4096 -DPIE=0
17-
gcc -x assembler-with-cpp -o test.libc test_libc.s -pie -Wl,--export-dynamic
18-
gcc -x assembler-with-cpp -shared -o libtest.so libtest.s
19-
gcc -O2 -fPIC $(FCF_NONE) -pie -o test_c test_c.c \
47+
48+
test.libc: test_libc.s
49+
$(CC) -x assembler-with-cpp -pie $< -Wl,--export-dynamic -o $@
50+
51+
libtest.so: libtest.s
52+
$(CC) -x assembler-with-cpp $< -shared -o $@
53+
54+
test_c: test_c.c
55+
$(CC) -O2 -fPIC $(FCF_NONE) -pie -o $@ $< \
2056
-Wl,--export-dynamic -U_FORTIFY_SOURCE
2157
strip test_c
22-
gcc -O0 -g -fPIC -pie -o test_c.debug test_c.c
23-
../../e9compile.sh inst.c -I ../../examples/
24-
../../e9compile.sh patch.cpp -std=c++11 -I ../../examples/
25-
NO_SIMD_CHECK=1 ../../e9compile.sh dl.c -I ../../examples/
26-
../../e9compile.sh init.c -I ../../examples/
27-
../../e9compile.sh fini.c -I ../../examples/
28-
g++ -std=c++11 -fPIC -shared -o example.so -O2 \
29-
../../examples/plugins/example.cpp -I ../../src/e9tool/
30-
g++ -std=c++11 -pie -fPIC -o regtest regtest.cpp -O2
31-
echo "XXX" > FILE.txt
32-
chmod 0640 FILE.txt
3358

34-
clean:
35-
rm -f *.log *.out *.exe test test.pie test.libc libtest.so inst inst.o \
36-
patch patch.o init init.o regtest
59+
test_c.debug: test_c.c
60+
$(CC) -O0 -g -fPIC -pie $< -o $@
61+
62+
inst: inst.c ../../examples/stdlib.c $(E9COMPILE)
63+
$(E9COMPILE) $< -I../../examples
64+
65+
patch: patch.cpp ../../examples/stdlib.c $(E9COMPILE)
66+
$(E9COMPILE) $< -std=c++11 -I../../examples
67+
68+
dl: dl.c ../../examples/stdlib.c $(E9COMPILE)
69+
NO_SIMD_CHECK=1 $(E9COMPILE) $< -I../../examples
70+
71+
init: init.c ../../examples/stdlib.c $(E9COMPILE)
72+
$(E9COMPILE) $< -I../../examples
73+
74+
fini: fini.c ../../examples/stdlib.c $(E9COMPILE)
75+
$(E9COMPILE) $< -I../../examples
76+
77+
example.so: ../../examples/plugins/example.cpp ../../src/e9tool/e9plugin.h
78+
$(CXX) -std=c++11 -O2 -fPIC -I../../src/e9tool $< -shared -o $@
79+
80+
check: run-tests $(EXE)
81+
./$^

test/regtest/init_dso.cmd

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
LD_PRELOAD=$PWD/init_dso.exe ./test.pie
1+
#!/bin/sh
2+
LD_PRELOAD=./init_dso.exe ./test.pie

test/regtest/init_dso_2.cmd

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
LD_PRELOAD=$PWD/init_dso.exe ./test.pie a b c 1 2 3
1+
#!/bin/sh
2+
LD_PRELOAD=./init_dso.exe ./test.pie a b c 1 2 3

test/regtest/regtest.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cstdlib>
2424
#include <cstring>
2525

26+
#include <sys/stat.h>
2627
#include <sys/types.h>
2728
#include <sys/wait.h>
2829
#include <dirent.h>
@@ -110,20 +111,10 @@ static bool runTest(const struct dirent *test, const std::string &options)
110111
}
111112

112113
// Step (2): execute the EXE
113-
FILE *CMD = fopen(cmd.c_str(), "r");
114114
command.clear();
115-
command += "./exec.sh ";
116-
if (CMD != NULL)
117-
{
118-
for (int i = 0; (c = getc(CMD)) != '\n' && isprint(c) && i < 1024; i++)
119-
command += c;
120-
fclose(CMD);
121-
}
122-
else
123-
{
124-
command += "./";
125-
command += exe;
126-
}
115+
command += "./exec.sh ./";
116+
struct stat cmd_stat;
117+
command += (stat(cmd.c_str(), &cmd_stat) == 0) ? cmd : exe;
127118
command += " >";
128119
command += out;
129120
command += " 2>&1";

test/regtest/run-tests

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
fails=()
3+
for exe in $*
4+
do
5+
tst=${exe%.exe}
6+
cmd=$tst.cmd
7+
out=$tst.out
8+
exp=$tst.exp
9+
10+
if test -f $cmd
11+
then ./exec.sh ./$cmd 1>$out 2>&1
12+
else ./exec.sh ./$exe 1>$out 2>&1
13+
fi
14+
sed -i 's/ (core dumped)$//' $out
15+
16+
diff -u $out $exp
17+
if test $? -ne 0
18+
then fails+=($tst)
19+
fi
20+
done
21+
22+
if test "$fails"
23+
then
24+
echo "Failing ${#fails[@]}/$# tests: ${fails[@]}"
25+
exit 1
26+
fi

test/regtest/stat.cmd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
trap 'rm -f FILE.txt' EXIT HUP INT TERM
3+
echo XXX > FILE.txt
4+
chmod 0640 FILE.txt
5+
./stat.exe

test/regtest/test_c.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
5+
#include <sys/types.h>
56

67
#ifndef putchar
78
#undef putchar
89
#endif
910

10-
asm (
11+
#ifndef __GNUC__
12+
#define __asm__ asm
13+
#endif
14+
15+
__asm__ (
1116
".globl entry\n"
1217
".set entry,0x0\n"
1318
);
@@ -69,12 +74,12 @@ __attribute__((__noinline__)) void triforce(ssize_t n)
6974
void data_func_2(void)
7075
{
7176
printf("invoked data_func()\n");
72-
asm volatile ("nop");
77+
__asm__ volatile ("nop");
7378
}
7479

7580
static void data_func(void)
7681
{
77-
asm volatile (
82+
__asm__ volatile (
7883
"xchg %r15, %r15\n"
7984
"callq data_func_2");
8085
}

0 commit comments

Comments
 (0)