From: Victor Julien Date: Sat, 19 Sep 2015 20:59:03 +0000 (+0200) Subject: mpm: introduce ac-ks X-Git-Tag: suricata-3.1RC1~394 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=887ddf1ed89f6d22080bce7b9a99c2c7deeb554e;p=thirdparty%2Fsuricata.git mpm: introduce ac-ks Introduce 'ac-ks' or the Kenneth Steele AC implementation. It's actually 'ac-tile' written by Ken for the Tilera platform. This patch adds support for it on other architectures as well. Enable ac-tile for other archs as 'ac-ks'. Fix a bunch of OOB reads in the loops that triggered ASAN. --- diff --git a/src/util-mpm-ac-tile-small.c b/src/util-mpm-ac-tile-small.c index a99da0634e..fd937f4a6b 100644 --- a/src/util-mpm-ac-tile-small.c +++ b/src/util-mpm-ac-tile-small.c @@ -45,12 +45,12 @@ uint32_t FUNC_NAME(SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, STYPE state = 0; int c = xlate[buf[0]]; /* If buflen at least 4 bytes and buf 4-byte aligned. */ - if (buflen >= 4 && ((uint64_t)buf & 0x3) == 0) { + if (buflen >= (4 + EXTRA) && ((uint64_t)buf & 0x3) == 0) { BTYPE data = *(BTYPE* restrict)(&buf[0]); uint64_t index = 0; /* Process 4*floor(buflen/4) bytes. */ i = 0; - while (i < (buflen & ~0x3)) { + while ((i + EXTRA) < (buflen & ~0x3)) { BTYPE data1 = *(BTYPE* restrict)(&buf[i + 4]); index = SINDEX(index, state); state = SLOAD(state_table + index + c); @@ -88,7 +88,10 @@ uint32_t FUNC_NAME(SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, uint64_t index = 0 ; index = SINDEX(index, state); state = SLOAD(state_table + index + c); - c = xlate[buf[i+1]]; +#ifndef __tile__ + if (likely(i+1 < buflen)) +#endif + c = xlate[buf[i+1]]; if (unlikely(SCHECK(state))) { matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray); } diff --git a/src/util-mpm-ac-tile.c b/src/util-mpm-ac-tile.c index f3a52d6e56..1fb43e9d59 100644 --- a/src/util-mpm-ac-tile.c +++ b/src/util-mpm-ac-tile.c @@ -80,15 +80,6 @@ #include "util-memcpy.h" #include "util-mpm-ac-tile.h" -#ifndef __tile__ -void MpmACTileRegister(void) -{ -} -#endif - -/* There are Tilera Tile-Gx specific optimizations in this code. */ -#ifdef __tile__ - void SCACTileInitCtx(MpmCtx *); void SCACTileInitThreadCtx(MpmCtx *, MpmThreadCtx *, uint32_t); void SCACTileDestroyCtx(MpmCtx *); @@ -1050,7 +1041,7 @@ static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx) mpm_ctx->memory_cnt++; mpm_ctx->memory_size += size; - SCLogInfo("Delta Table size %d, alphabet: %d, %d-byte states: %d", + SCLogDebug("Delta Table size %d, alphabet: %d, %d-byte states: %d", size, ctx->alphabet_size, ctx->bytes_per_state, ctx->state_count); /* Copy next state from Goto table, which is 32 bits and encode it into the next @@ -1460,10 +1451,19 @@ void SCACTileDestroyCtx(MpmCtx *mpm_ctx) #define SCHECK(x) ((x) > 0) #define BTYPE int32_t // Extract byte N=0,1,2,3 from x +#ifdef __tile__ #define BYTE0(x) __insn_bfextu(x, 0, 7) #define BYTE1(x) __insn_bfextu(x, 8, 15) #define BYTE2(x) __insn_bfextu(x, 16, 23) #define BYTE3(x) __insn_bfextu(x, 24, 31) +#define EXTRA 0 +#else /* fallback */ +#define BYTE0(x) (((x) & 0x000000ff) >> 0) +#define BYTE1(x) (((x) & 0x0000ff00) >> 8) +#define BYTE2(x) (((x) & 0x00ff0000) >> 16) +#define BYTE3(x) (((x) & 0xff000000) >> 24) +#define EXTRA 4 // need 4 extra bytes to avoid OOB reads +#endif int CheckMatch(SCACTileSearchCtx *ctx, PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen, @@ -1494,7 +1494,11 @@ int CheckMatch(SCACTileSearchCtx *ctx, PatternMatcherQueue *pmq, /* Double check case-sensitve match now. */ if (patterns[k] >> 31) { uint16_t patlen = pattern_list[pindex].patlen; +#ifdef __tile__ if (SCMemcmpNZ(pattern_list[pindex].cs, buf_offset - patlen, patlen) != 0) { +#else + if (SCMemcmp(pattern_list[pindex].cs, buf_offset - patlen, patlen) != 0) { +#endif /* Case-sensitive match failed. */ continue; } @@ -1571,13 +1575,22 @@ uint32_t SCACTileSearchLarge(SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ct * Next state entry has MSB as "match" and 15 LSB bits as next-state index. */ // y = 1<