Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-20386

Replace inline asm with compiler-builtin intrinsic functions

Details

    Description

      As noted in MDEV-20377, inline assembler will cause MemorySanitizer to report false positives for uninitialized values. It would be more portable to use the compiler built-in functions not only on Windows, but on all platforms.

      Here is a crude patch for WolfSSL:

      diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c
      index 6b0d5dafc..817619d16 100644
      --- a/wolfcrypt/src/random.c
      +++ b/wolfcrypt/src/random.c
      @@ -173,7 +173,7 @@ int wc_RNG_GenerateByte(WC_RNG* rng, byte* b)
           static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz);
           #endif
       
      -#ifdef USE_WINDOWS_API
      +#if 1 /*def USE_WINDOWS_API */
           #include <immintrin.h>
       #endif /* USE_WINDOWS_API */
       #endif
      @@ -1282,7 +1282,7 @@ int wc_FreeNetRandom(void)
       
       #ifdef HAVE_INTEL_RDSEED
       
      -#ifndef USE_WINDOWS_API
      +#if 0/*ndef USE_WINDOWS_API*/
       
           /* return 0 on success */
           static WC_INLINE int IntelRDseed64(word64* seed)
      @@ -1302,7 +1302,7 @@ int wc_FreeNetRandom(void)
           {
               int ok;
       
      -        ok = _rdseed64_step(seed);
      +        ok = _rdseed64_step((unsigned long long*) seed);
               return (ok) ? 0 : -1;
           }
       
      

      and another for InnoDB:

      diff --git a/storage/innobase/ut/ut0crc32.cc b/storage/innobase/ut/ut0crc32.cc
      index 4a6447c1dcf..58273e9058e 100644
      --- a/storage/innobase/ut/ut0crc32.cc
      +++ b/storage/innobase/ut/ut0crc32.cc
      @@ -219,6 +219,8 @@ ut_crc32_8_hw(
       {
       #ifdef _MSC_VER
       	*crc = _mm_crc32_u8(*crc, (*data)[0]);
      +#elif 1
      +	*crc = __builtin_ia32_crc32qi(*crc, (*data)[0]);
       #else
       	asm("crc32b %1, %0"
       	    /* output operands */
      @@ -251,6 +253,8 @@ ut_crc32_64_low_hw(
       #else
       #error Not Supported processors type.
       #endif
      +#elif 1
      +	crc_64bit = __builtin_ia32_crc32di(crc_64bit, data);
       #else
       	asm("crc32q %1, %0"
       	    /* output operands */
      

      Both these patches will require some compiler switches to enable the instructions to be emitted. For clang 8.0.1, -march=native did the trick for me, but that is obviously unacceptable. According to diagnostic messages from the compiler, the features might be called "rdseed" and "SSE 4.2".

      Attachments

        Issue Links

          Activity

            danblack, I did not claim to fix earlier versions than 10.5. The main motivation of this is to be able to use WITH_MSAN without any -march. There have been a lot of fixes in 10.5 to the CRC-32 and CRC-32C code that we did not port to earlier versions. Does the 10.3 build fail on a platform where clang is the default compiler, such as FreeBSD or Mac OS X?

            marko Marko Mäkelä added a comment - danblack , I did not claim to fix earlier versions than 10.5. The main motivation of this is to be able to use WITH_MSAN without any -march . There have been a lot of fixes in 10.5 to the CRC-32 and CRC-32C code that we did not port to earlier versions. Does the 10.3 build fail on a platform where clang is the default compiler, such as FreeBSD or Mac OS X?
            danblack Daniel Black added a comment - pushed https://travis-ci.org/github/grooverdan/mariadb-server/builds/738179878 as OSX test with MSAN enabled.

            danblack, I updated the bb-10.5-MDEV-20386 branch again. I am anticipating a WolfSSL release soon. (In our main branches, we use release tags for the submodule, not ‘random’ commits.)

            marko Marko Mäkelä added a comment - danblack , I updated the bb-10.5- MDEV-20386 branch again. I am anticipating a WolfSSL release soon. (In our main branches, we use release tags for the submodule, not ‘random’ commits.)
            marko Marko Mäkelä added a comment - Another test with WolfSSL v4.6.0-stable
            marko Marko Mäkelä added a comment - - edited

            MDEV-24514 will enable the hand-written assembler code (WOLFSSL_AESNI) when MemorySanitizer is enabled.

            marko Marko Mäkelä added a comment - - edited MDEV-24514 will enable the hand-written assembler code ( WOLFSSL_AESNI ) when MemorySanitizer is enabled.

            People

              marko Marko Mäkelä
              marko Marko Mäkelä
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.