From fe5b80d615881bd1c31e0a99d655f4af90acf126 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Fri, 11 Sep 2020 18:47:27 +0800 Subject: [PATCH] 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 --- .github/workflows/ci.yml | 11 ++++++++++- binr/r2r/run.c | 13 +++++++++++++ libr/lang/p/pipe.c | 14 +++++++++----- libr/reg/arena.c | 2 +- test/db/cmd/cmd_pipe | 6 +++++- test/unit/Makefile | 7 ++++++- travis-script | 1 + 7 files changed, 45 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cb73d218f..5bed51bc1e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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')) diff --git a/binr/r2r/run.c b/binr/r2r/run.c index 4d85c876e1..ea2175cec0 100644 --- a/binr/r2r/run.c +++ b/binr/r2r/run.c @@ -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 { diff --git a/libr/lang/p/pipe.c b/libr/lang/p/pipe.c index 0bbef28d28..3abecba2b7 100644 --- a/libr/lang/p/pipe.c +++ b/libr/lang/p/pipe.c @@ -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); + } } } diff --git a/libr/reg/arena.c b/libr/reg/arena.c index 91d731918e..fc17c2941e 100644 --- a/libr/reg/arena.c +++ b/libr/reg/arena.c @@ -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; diff --git a/test/db/cmd/cmd_pipe b/test/db/cmd/cmd_pipe index fc7ec0b363..b07bd7a60b 100644 --- a/test/db/cmd/cmd_pipe +++ b/test/db/cmd/cmd_pipe @@ -8,7 +8,11 @@ RUN NAME=r2pipe.py FILE=bins/elf/_Exit (42) -CMDS=#!pipe python3 scripts/get-funcs.py +CMDS=<