diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index c48a6a1a2..668d631e9 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -21,6 +21,7 @@ jobs: use-configure: use-configure compiler: clang use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: no-extra-checks @@ -29,6 +30,7 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: sanitize run-extra-checks: no-extra-checks @@ -37,6 +39,7 @@ jobs: use-configure: no-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: run-extra-checks @@ -45,6 +48,7 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: no-extra-checks @@ -53,9 +57,27 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: no-libdeflate + use-isal: no-isal sanitize: sanitize run-extra-checks: no-extra-checks + # x86-64, gcc, configure, isal, sanitize + - os: ubuntu-latest + use-configure: use-configure + compiler: gcc + use-libdeflate: no-libdeflate + use-isal: use-isal + sanitize: sanitize + run-extra-checks: no-extra-checks + + # x86-64, gcc, configure, isal, libdeflate, sanitize + - os: ubuntu-latest + use-configure: use-configure + compiler: gcc + use-libdeflate: use-libdeflate + use-isal: use-isal + sanitize: sanitize + run-extra-checks: no-extra-checks env: UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1 @@ -72,14 +94,18 @@ jobs: run: | autoconf_pkgs="" deflate_pkgs="" + isal_pkgs="" if [ '${{ matrix.use-configure }}' = 'use-configure' ] ; then autoconf_pkgs="autoconf automake" fi if [ '${{ matrix.use-libdeflate }}' = 'use-libdeflate' ] ; then deflate_pkgs="libdeflate-dev" fi + if [ '${{ matrix.use-isal}}' = 'use-isal' ] ; then + isal_pkgs="libisal-dev" + fi sudo apt-get update - sudo apt-get install -y --no-install-suggests --no-install-recommends $autoconf_pkgs make ${{ matrix.compiler }} perl zlib1g-dev libbz2-dev liblzma-dev libcurl4-gnutls-dev libssl-dev $deflate_pkgs + sudo apt-get install -y --no-install-suggests --no-install-recommends $autoconf_pkgs make ${{ matrix.compiler }} perl zlib1g-dev libbz2-dev liblzma-dev libcurl4-gnutls-dev libssl-dev $deflate_pkgs $isal_pkgs - name: Configure if: ${{ matrix.use-configure == 'use-configure' }} diff --git a/INSTALL b/INSTALL index 220ac13f7..2c04e5cb4 100644 --- a/INSTALL +++ b/INSTALL @@ -39,10 +39,14 @@ a development ('-dev' or '-devel') package separate from the main library. libcurl (optional, but strongly recommended) libcrypto (optional for Amazon S3 support; not needed on MacOS) libdeflate (optional, but strongly recommended for faster gzip) + isa-l (optional, but strongly recommended for faster "fast" level of gzip) Disabling libbzip2 and liblzma will make some CRAM files unreadable, so is not recommended. +ISA-L is available as libisal on Debian-based linux distributions and as isa-l +on conda-forge. + Using libcurl provides HTSlib with network protocol support, for example it enables the use of ftp://, http://, and https:// URLs. It is also required if direct access to Amazon S3 or Google Cloud @@ -182,6 +186,13 @@ various features and specify further optional external requirements: By default, ./configure will probe for libdeflate and use it if available. To prevent this, use --without-libdeflate. +--with-isal + ISA-L is another heavily optimized library for DEFLATE-based compression + and decompression. It provides a few very fast compression levels at + similar size of level 1. It also includes a fast gzip stream decoder. + By default, ./configure will probe for isa-l and use it if + available. To prevent this, use --without-isal. + Each --enable-FEATURE/--disable-FEATURE/--with-PACKAGE/--without-PACKAGE option listed also has an opposite, e.g., --without-external-htscodecs or --disable-plugins. However, apart from those options for which the diff --git a/bgzf.c b/bgzf.c index e21a18e70..101beb877 100644 --- a/bgzf.c +++ b/bgzf.c @@ -41,6 +41,19 @@ #include #endif +#ifdef HAVE_ISAL +#include +#include +#endif + +#ifdef __x86_64__ +#define X86_64 __x86_64__ +#elif defined(_M_AMD64) +#define X86_64 _M_AMD64 +#else +#define X86_64 0 +#endif + #include "htslib/hts.h" #include "htslib/bgzf.h" #include "htslib/hfile.h" @@ -397,6 +410,34 @@ static const char *bgzf_zerr(int errnum, z_stream *zs) } } +#if HAVE_ISAL +static const char *isal_inflate_err(int errnum) +{ + static char buffer[32]; + + switch (errnum) { + case ISAL_INVALID_BLOCK: + return "invalid deflate block found"; + case ISAL_INVALID_SYMBOL: + return "invalid deflate symbol found"; + case ISAL_INVALID_LOOKBACK: + return "invalid lookback distance found"; + case ISAL_INVALID_WRAPPER: + return "invalid gzip/zlib wrapper found"; + case ISAL_UNSUPPORTED_METHOD: + return "gzip/zlib wrapper specifies unsupported compress method"; + case ISAL_INCORRECT_CHECKSUM: + return "incorrect checksum found"; + case ISAL_NEED_DICT: + return "data was compressed using a dictionary"; + case ISAL_DECOMP_OK: // 0: maybe gzgets error Z_NULL + default: + snprintf(buffer, sizeof(buffer), "[%d] unknown", errnum); + return buffer; // FIXME: Not thread-safe. + } +} +#endif + static BGZF *bgzf_read_init(hFILE *hfpr, const char *filename) { BGZF *fp; @@ -487,6 +528,7 @@ static BGZF *bgzf_write_init(const char *mode) fp->gz_stream->zalloc = NULL; fp->gz_stream->zfree = NULL; fp->gz_stream->msg = NULL; + fp->isal_inflate_state= NULL; int ret = deflateInit2(fp->gz_stream, fp->compress_level, Z_DEFLATED, 15|16, 8, Z_DEFAULT_STRATEGY); if (ret!=Z_OK) { @@ -570,11 +612,74 @@ BGZF *bgzf_hopen(hFILE *hfp, const char *mode) return fp; } -#ifdef HAVE_LIBDEFLATE + +#if HAVE_LIBDEFLATE uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { return libdeflate_crc32(crc, buf, len); } +#elif HAVE_ISAL +uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { + return crc32_gzip_refl(crc, buf, len); +} +#else +uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { + return crc32(crc, buf, len); +} +#endif +#if HAVE_ISAL +static int bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_t slen, int level) +{ + if (level < 0 || level > 3) { + return -1; + } + struct isal_zstream *z = malloc(sizeof(struct isal_zstream)); + if (z == NULL) { + return -1; + } + static int level_buf_sizes[] = { + ISAL_DEF_LVL0_DEFAULT, + ISAL_DEF_LVL1_DEFAULT, + ISAL_DEF_LVL2_DEFAULT, + ISAL_DEF_LVL3_DEFAULT + }; + size_t level_buf_size = level_buf_sizes[level]; + uint8_t *level_buf = malloc(level_buf_size); + if (level_buf == NULL) { + free(z); + return -1; + } + isal_deflate_stateless_init(z); + z->level = level; + z->level_buf = level_buf; + z->level_buf_size = level_buf_size; + z->gzip_flag = IGZIP_GZIP_NO_HDR; // Also calculates length and CRC + z->next_in = (uint8_t *)src; + z->avail_in = slen; + z->next_out = dst + BLOCK_HEADER_LENGTH; + z->avail_out = *dlen - BLOCK_HEADER_LENGTH; + z->flush = FULL_FLUSH; + z->end_of_stream = 1; + + /* isal_deflate_stateless will throw an overflow error when there is not + * enough room in the output buffer */ + int ret = isal_deflate_stateless(z); + if (ret != COMP_OK) { + hts_log_error("Call to bgzf_compress_isal failed"); + free(level_buf); + free(z); + return -1; + } + *dlen = z->next_out - (uint8_t *)dst; + free(level_buf); + free(z); + memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block + packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes + return 0; +} +#endif + +#if HAVE_LIBDEFLATE int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level) { if (slen == 0) { @@ -598,6 +703,13 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le } else { level = level > 0 ? level : 6; // libdeflate doesn't honour -1 as default + + #if HAVE_ISAL + if (level == 1) { + return bgzf_compress_isal(dst, dlen, src, slen, level); + } + #endif + // NB levels go up to 12 here. int lvl_map[] = {0,1,2,3,5,6,7,8,10,12}; level = lvl_map[level>9 ?9 :level]; @@ -634,9 +746,6 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le #else -uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { - return crc32(crc, buf, len); -} int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level) { @@ -653,6 +762,10 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le u16_to_le(~slen, &dst[BLOCK_HEADER_LENGTH+3]); // ones-complement length memcpy(dst + BLOCK_HEADER_LENGTH+5, src, slen); *dlen = slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; + #if HAVE_ISAL + } else if (level == 1) { + return bgzf_compress_isal(dst, dlen, src, slen, level); + #endif } else { // compress the body zs.zalloc = NULL; zs.zfree = NULL; @@ -742,7 +855,46 @@ static int deflate_block(BGZF *fp, int block_length) return comp_size; } -#ifdef HAVE_LIBDEFLATE +#if HAVE_ISAL && X86_64 +static int bgzf_uncompress(uint8_t *dst, size_t *dlen, + const uint8_t *src, size_t slen, + uint32_t expected_crc) { + struct inflate_state *z = malloc(sizeof(struct inflate_state)); + if (z == NULL) { + hts_log_error("Allocating isal inflate state failed"); + return -1; + } + isal_inflate_init(z); + z->next_in = (uint8_t *)src; + z->avail_in = slen; + z->next_out = dst; + z->avail_out = *dlen; + z->crc_flag = ISAL_GZIP_NO_HDR; + + /* isal_deflate_stateless will throw an overflow error when there is not + * enough room in the output buffer */ + int ret = isal_inflate_stateless(z); + if (ret != ISAL_DECOMP_OK) { + hts_log_error("Inflate operation failed: %d", ret); + free(z); + return -1; + + } + *dlen = z->next_out - dst; + uint32_t crc = z->crc; + free(z); + #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + // Pretend the CRC was OK so the fuzzer doesn't have to get it right + crc = expected_crc; + #endif + if (crc != expected_crc) { + hts_log_error("CRC32 checksum mismatch"); + return -2; + } + return 0; +} + +#elif HAVE_LIBDEFLATE static int bgzf_uncompress(uint8_t *dst, size_t *dlen, const uint8_t *src, size_t slen, @@ -840,6 +992,61 @@ static int inflate_block(BGZF* fp, int block_length) return dlen; } +#if HAVE_ISAL && X86_64 +static int inflate_gzip_block(BGZF *fp) +{ + // we will set this to true when we detect EOF, so we don't bang against the EOF more than once per call + int input_eof = 0; + + // write to the part of the output buffer after block_offset + fp->isal_inflate_state->next_out = (Bytef*)fp->uncompressed_block + fp->block_offset; + fp->isal_inflate_state->avail_out = BGZF_MAX_BLOCK_SIZE - fp->block_offset; + + while ( fp->isal_inflate_state->avail_out != 0 ) { + // until we fill the output buffer (or hit EOF) + + if ( !input_eof && fp->isal_inflate_state->avail_in == 0 ) { + // we are out of input data in the buffer. Get more. + fp->isal_inflate_state->next_in = fp->compressed_block; + int ret = hread(fp->fp, fp->compressed_block, BGZF_BLOCK_SIZE); + if ( ret < 0 ) { + // hread had an error. Pass it on. + return ret; + } + fp->isal_inflate_state->avail_in = ret; + if ( fp->isal_inflate_state->avail_in < BGZF_BLOCK_SIZE ) { + // we have reached EOF but the decompressor hasn't necessarily + input_eof = 1; + } + } + + // decompress as much data as we can + int ret = isal_inflate(fp->isal_inflate_state); + + if ( ret != ISAL_DECOMP_OK ) { + // an error occurred, other than running out of space + hts_log_error("Inflate operation failed: %s", isal_inflate_err(ret)); + fp->errcode |= BGZF_ERR_ZLIB; + return -1; + } else if ( fp->isal_inflate_state->block_state == ISAL_BLOCK_FINISH ) { + // we finished a GZIP member + // scratch for peeking to see if the file is over + char c; + if (fp->isal_inflate_state->avail_in > 0 || hpeek(fp->fp, &c, 1) == 1) { + // there is more data; try and read another GZIP member in the remaining data + isal_inflate_reset(fp->isal_inflate_state); + } else { + // we consumed all the input data and hit Z_STREAM_END + // so stop looping, even if we never fill the output buffer + break; + } + } + } + + // when we get here, the buffer is full or there is an EOF after a complete gzip member + return BGZF_MAX_BLOCK_SIZE - fp->isal_inflate_state->avail_out; +} +#else // Decompress the next part of a non-blocked GZIP file. // Return the number of uncompressed bytes read, 0 on EOF, or a negative number on error. // Will fill the output buffer unless the end of the GZIP file is reached. @@ -908,7 +1115,7 @@ static int inflate_gzip_block(BGZF *fp) // when we get here, the buffer is full or there is an EOF after a complete gzip member return BGZF_MAX_BLOCK_SIZE - fp->gz_stream->avail_out; } - +#endif // Returns: 0 on success (BGZF header); -1 on non-BGZF GZIP header; -2 on error static int check_header(const uint8_t *header) { @@ -1146,7 +1353,7 @@ static int bgzf_read_block_core(BGZF *fp) } // Reading compressed file - if ( fp->is_gzip && fp->gz_stream ) // is this is an initialized gzip stream? + if ( fp->is_gzip && (fp->gz_stream || fp->isal_inflate_state )) // is this is an initialized gzip stream? { count = inflate_gzip_block(fp); if ( count<0 ) @@ -1191,6 +1398,19 @@ static int bgzf_read_block_core(BGZF *fp) count = hread(fp->fp, cblock+sizeof(header), BGZF_BLOCK_SIZE - sizeof(header)) + sizeof(header); fp->is_gzip = 1; +#if HAVE_ISAL && X86_64 + fp->isal_inflate_state = (struct inflate_state *)calloc(1, sizeof(struct inflate_state)); + if (fp->isal_inflate_state == NULL) { + hts_log_error("Out of memory"); + fp->errcode = BGZF_ERR_ZLIB; + return -1; + } + isal_inflate_init(fp->isal_inflate_state); + fp->isal_inflate_state->crc_flag = ISAL_GZIP; + fp->isal_inflate_state->hist_bits = 15; + fp->isal_inflate_state->avail_in = count; + fp->isal_inflate_state->next_in = cblock; +#else fp->gz_stream = (z_stream*) calloc(1,sizeof(z_stream)); // Set up zlib, using a window size of 15, and its built-in GZIP header processing (+16). int ret = inflateInit2(fp->gz_stream, 15 + 16); @@ -1202,6 +1422,7 @@ static int bgzf_read_block_core(BGZF *fp) } fp->gz_stream->avail_in = count; fp->gz_stream->next_in = cblock; +#endif count = inflate_gzip_block(fp); if ( count<0 ) { @@ -2184,6 +2405,7 @@ int bgzf_close(BGZF* fp) hts_log_error("Call to inflateEnd/deflateEnd failed: %s", bgzf_zerr(ret, NULL)); } free(fp->gz_stream); + free(fp->isal_inflate_state); } ret = hclose(fp->fp); if (ret != 0) return -1; diff --git a/configure.ac b/configure.ac index 399e506de..526b77431 100644 --- a/configure.ac +++ b/configure.ac @@ -286,6 +286,11 @@ AC_ARG_WITH([libdeflate], [use libdeflate for faster crc and deflate algorithms])], [], [with_libdeflate=check]) +AC_ARG_WITH([isal], + [AS_HELP_STRING([--with-isal], + [use isal for faster crc and faster deflate at compression level 1 and 2.])], + [], [with_isal=check]) + AC_ARG_WITH([plugin-dir], [AS_HELP_STRING([--with-plugin-dir=DIR], [plugin installation location [LIBEXECDIR/htslib]])], @@ -568,6 +573,27 @@ are not currently on them. Either configure with --without-libdeflate or resolve this error to build HTSlib.])])])]) +AS_IF([test "x$with_isal" != "xno"], + [isal=ok + AC_CHECK_HEADER([isa-l.h],[],[isal='missing header'],[;]) + AC_CHECK_LIB([isal], [isal_deflate_stateless],[:],[isal='missing library']) + AS_IF([test "$isal" = "ok"], + [AC_DEFINE([HAVE_ISAL], 1, [Define if ISAL is available.]) + LIBS="-lisal $LIBS" + private_LIBS="$private_LIBS -lisal" + static_LIBS="$static_LIBS -lisal"], + [AS_IF([test "x$with_isal" != "xcheck"], + [MSG_ERROR([isal development files not found: $isal + +You requested isal, but do not have the required header / library +files. The source for isal is available from +. You may have to adjust +search paths in CPPFLAGS and/or LDFLAGS if the header and library +are not currently on them. + +Either configure with --without-isal or resolve this error to build +HTSlib.])])])]) + libcurl=disabled if test "$enable_libcurl" != no; then libcurl_devel=ok diff --git a/htslib/bgzf.h b/htslib/bgzf.h index 420faed16..3a90869b6 100644 --- a/htslib/bgzf.h +++ b/htslib/bgzf.h @@ -64,6 +64,7 @@ struct bgzf_mtaux_t; typedef struct bgzidx_t bgzidx_t; typedef struct bgzf_cache_t bgzf_cache_t; struct z_stream_s; +struct inflate_state; struct BGZF { // Reserved bits should be written as 0; read as "don't care" @@ -80,6 +81,7 @@ struct BGZF { bgzidx_t *idx; // BGZF index int idx_build_otf; // build index on the fly, set by bgzf_index_build_init() struct z_stream_s *gz_stream; // for gzip-compressed files + struct inflate_state *isal_inflate_state; // For faster gzip decompression with ISA-L int64_t seeked; // virtual offset of last seek }; #ifndef HTS_BGZF_TYPEDEF