Fix asan r2pipe test by updating libasan and using LD_PRELOAD (#17594)

* Set LD_PRELOAD for unit tests
* Declare failed debug tests as broken if no assertion or Sanitizer in output
* Check for ASAN=1 in cmd_pipe test
* Set asan: true in the GitHubCI matrix
* Fix ttyname-related runtime error
* Upgrade asan build to Ubuntu 20.04
* Quick fix for reg/arena.c runtime error
This commit is contained in:
Khairul Azhar Kasmiran 2020-09-11 18:47:27 +08:00 committed by GitHub
parent ec21daa6c2
commit fe5b80d615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 9 deletions

View File

@ -79,11 +79,12 @@ jobs:
enabled: true
timeout: 60
- name: linux-gcc-tests-asan
os: ubuntu-latest
os: ubuntu-20.04
build_system: meson
compiler: gcc
cflags: '-DR2_ASSERT_STDOUT=1'
meson_options: -Db_sanitize=address,undefined
asan: true
asan_options: 'detect_leaks=0,detect_odr_violation=0'
run_tests: true
enabled: ${{ github.event_name != 'pull_request' || contains(github.head_ref, 'asan') }}
@ -141,8 +142,12 @@ jobs:
if: matrix.build_system == 'meson' && matrix.enabled
run: |
export PATH=${HOME}/.local/bin:${PATH}
if [ "$ASAN" == "true" ]; then
export CFLAGS="-DASAN=1 ${CFLAGS}"
fi
meson --prefix=${HOME} ${{ matrix.meson_options }} build && ninja -C build
env:
ASAN: ${{ matrix.asan }}
CC: ${{ matrix.compiler }}
CFLAGS: ${{ matrix.cflags }}
- name: Install with make
@ -171,11 +176,15 @@ jobs:
if [ "$NEWSHELL" == "true" ]; then
export R2_CFG_NEWSHELL=1
fi
if [ "$ASAN" == "true" ]; then
export ASAN=1
fi
cd test
radare2 -N -Qc 'e cfg.newshell' -
make
env:
NEWSHELL: ${{ matrix.newshell }}
ASAN: ${{ matrix.asan }}
ASAN_OPTIONS: ${{ matrix.asan_options }}
- name: Run fuzz tests
if: matrix.run_tests && matrix.enabled && (github.event_name != 'pull_request' || contains(github.event.pull_request.head.ref, 'fuzz'))

View File

@ -1203,6 +1203,19 @@ R_API R2RTestResultInfo *r2r_run_test(R2RRunConfig *config, R2RTest *test) {
}
}
bool broken = r2r_test_broken (test);
#if ASAN
# if !R2_ASSERT_STDOUT
# error R2_ASSERT_STDOUT undefined or 0
# endif
R2RProcessOutput *out = ret->proc_out;
if (!success && test->type == R2R_TEST_TYPE_CMD && strstr (test->path, "/dbg")
&& (!out->out ||
(!strstr (out->out, "WARNING:") && !strstr (out->out, "ERROR:") && !strstr (out->out, "FATAL:")))
&& (!out->err ||
(!strstr (out->err, "Sanitizer") && !strstr (out->err, "runtime error:")))) {
broken = true;
}
#endif
if (!success) {
ret->result = broken ? R2R_TEST_RESULT_BROKEN : R2R_TEST_RESULT_FAILED;
} else {

View File

@ -211,11 +211,15 @@ static int lang_pipe_run(RLang *lang, const char *code, int len) {
if (safe_in != -1) {
close (safe_in);
}
safe_in = open (ttyname(0), O_RDONLY);
if (safe_in != -1) {
dup2 (safe_in, 0);
} else {
eprintf ("Cannot open ttyname(0) %s\n", ttyname(0));
safe_in = -1;
char *term_in = ttyname (0);
if (term_in) {
safe_in = open (term_in, O_RDONLY);
if (safe_in != -1) {
dup2 (safe_in, 0);
} else {
eprintf ("Cannot open ttyname(0) %s\n", term_in);
}
}
}

View File

@ -43,7 +43,7 @@ R_API ut8 *r_reg_get_bytes(RReg *reg, int type, int *size) {
*size = sz;
}
buf = malloc (sz);
if (buf) {
if (buf && reg->regset[type].arena->bytes) { // TODO change 2nd check to something more reasonable
memcpy (buf, reg->regset[type].arena->bytes, sz);
}
return buf;

View File

@ -8,7 +8,11 @@ RUN
NAME=r2pipe.py
FILE=bins/elf/_Exit (42)
CMDS=#!pipe python3 scripts/get-funcs.py
CMDS=<<EOF
?q `env~?^ASAN=1$`
?+ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.5
#!pipe python3 scripts/get-funcs.py
EOF
EXPECT=<<EOF
Function names:

View File

@ -2,6 +2,11 @@ BINDIR := bin
BINS=$(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
LDFLAGS += $(shell pkg-config --libs r_core)
CFLAGS += $(shell pkg-config --cflags r_core) -g
ifeq ($(ASAN),1)
ASAN_LD_PRELOAD=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.5
else
ASAN_LD_PRELOAD=
endif
all: $(BINS)
@ -10,7 +15,7 @@ $(BINDIR)/%: %.c
$(CC) $< -o $@ $(CFLAGS) $(LDFLAGS)
run:
r=0 ; cd .. ; for a in $(BINS) ; do ./unit/$$a || r=1; done ; echo unit$$r ; exit $$r
r=0 ; cd .. ; for a in $(BINS) ; do $(ASAN_LD_PRELOAD) ./unit/$$a || r=1; done ; echo unit$$r ; exit $$r
clean:
rm -f $(BINS)

View File

@ -46,6 +46,7 @@ if [ "${INSTALL_SYSTEM}" == "meson" ] ; then
if [ "${ASAN}" == "1" ] ; then
# -Db_lundef=false required for issue with clang+meson (see https://github.com/mesonbuild/meson/issues/764)
OPTS="${OPTS} -Db_sanitize=address -Db_lundef=false"
export CFLAGS="-DASAN=1 ${CFLAGS}"
fi
meson --prefix=${TRAVIS_BUILD_DIR}/install ${OPTS} build || exit 1