https://bugs.kde.org/show_bug.cgi?id=529180

            Bug ID: 529180
           Summary: False positives on Aarch64 with BSL, BIT, and BIF
                    instructions
    Classification: Developer tools
           Product: valgrind
      Version First 3.27 GIT
       Reported In:
          Platform: Other
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: memcheck
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

Consider the following testcase:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

static const char *do_bsl(const char *init, const char *uninit)
{
    uint64_t d = -1;
    uint64_t n = (uint64_t)init;
    uint64_t m = (uint64_t)uninit;
    __asm__ (
        "bsl %0.8b, %1.8b, %2.8b"
        : "+w" (d)
        : "w"  (n),
          "w"  (m)
    );
    return (const char *)d;
}

static const char *do_bit(const char *init, const char *uninit)
{
    uint64_t d = (uint64_t)uninit;
    uint64_t n = (uint64_t)init;
    uint64_t m = -1;
    __asm__ (
        "bit %0.8b, %1.8b, %2.8b"
        : "+w" (d)
        : "w"  (n),
          "w"  (m)
    );
    return (const char *)d;
}

static const char *do_bif(const char *init, const char *uninit)
{
    uint64_t d = (uint64_t)uninit;
    uint64_t n = (uint64_t)init;
    uint64_t m = 0;
    __asm__ (
        "bif %0.8b, %1.8b, %2.8b"
        : "+w" (d)
        : "w"  (n),
          "w"  (m)
    );
    return (const char *)d;
}

int main(void)
{
    const char *init = "Hello, World!";
    char **buf = malloc(sizeof (char *));
    asm("" ::: "memory");
    const char *uninit = *buf;

    printf("%s\n", do_bsl(init, uninit));
    printf("%s\n", do_bit(init, uninit));
    printf("%s\n", do_bif(init, uninit));
    free(buf);
    return 0;
}

Compile with 'gcc -O2' and run under Memcheck. It will complain for each
"Hello, World!" printing. But it shouldn't because every printed HW is a bit
selection result with the mask always selecting the initialised pointer.

Such FP may be avoided by changing BSL, BIT, and BIF instructions translation.
E.g. the frontend translates 'BSL d, n, m' to 'm XOR ( (m XOR n) AND d )'.
Memcheck avoids FPs for AND and OR by calculating their shadow values properly
using the original values. However, it seems impossible to do for XOR.
Therefore, I would suggest to avoid XOR in the translations.

The following patch solves the problem:

diff --git a/VEX/priv/guest_arm64_toIR.c b/VEX/priv/guest_arm64_toIR.c
index a4770b86e..039b989c9 100644
--- a/VEX/priv/guest_arm64_toIR.c
+++ b/VEX/priv/guest_arm64_toIR.c
@@ -12706,6 +12706,14 @@ Bool dis_AdvSIMD_three_different(/*MB_OUT*/DisResult*
dres, UInt insn)
 #  undef INSN
 }

+static
+void select_bsl(IRTemp res, IRTemp argM, IRTemp argT, IRTemp argF)
+{
+   assign(res, binop(Iop_OrV128,
+                     binop(Iop_AndV128, mkexpr(argM), mkexpr(argT)),
+                     binop(Iop_AndV128, unop(Iop_NotV128, mkexpr(argM)),
+                           mkexpr(argF))));
+}

 static
 Bool dis_AdvSIMD_three_same(/*MB_OUT*/DisResult* dres, UInt insn)
@@ -12866,22 +12874,13 @@ Bool dis_AdvSIMD_three_same(/*MB_OUT*/DisResult*
dres, UInt insn)
             assign(res, binop(opXOR, mkexpr(argM), mkexpr(argN)));
             break;
          case BITS2(0,1): /* BSL */
-            assign(res, binop(opXOR, mkexpr(argM),
-                              binop(opAND,
-                                    binop(opXOR, mkexpr(argM), mkexpr(argN)),
-                                          mkexpr(argD))));
+            select_bsl(res, argD, argN, argM);
             break;
          case BITS2(1,0): /* BIT */
-            assign(res, binop(opXOR, mkexpr(argD),
-                              binop(opAND,
-                                    binop(opXOR, mkexpr(argD), mkexpr(argN)),
-                                    mkexpr(argM))));
+            select_bsl(res, argM, argN, argD);
             break;
          case BITS2(1,1): /* BIF */
-            assign(res, binop(opXOR, mkexpr(argD),
-                              binop(opAND,
-                                    binop(opXOR, mkexpr(argD), mkexpr(argN)),
-                                    unop(opNOT, mkexpr(argM)))));
+            select_bsl(res, argM, argD, argN);
             break;
          default:
             vassert(0);

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to