32 #define DUMP_REGIONS 0 // set to 1 to dump a visual representation of the regions list and sbb tree.
42 static void *ReallocFunc(
void *ptr,
const size_t size)
51 static void *CallocFunc(
const size_t nm,
const size_t sz)
63 #define CALLOC(cfg, n, s) (cfg)->Calloc ? (cfg)->Calloc((n), (s)) : CallocFunc((n), (s))
64 #define REALLOC(cfg, ptr, orig_s, s) \
65 (cfg)->Realloc ? (cfg)->Realloc((ptr), (orig_s), (s)) : ReallocFunc((ptr), (s))
66 #define FREE(cfg, ptr, s) \
67 (cfg)->Free ? (cfg)->Free((ptr), (s)) : SCFree((ptr))
94 const uint64_t lre = lookup->
offset + lookup->
len;
95 const uint64_t tre = intree->
offset + intree->
len;
98 else if (lookup->
offset < tre && lre <= tre)
112 const int comp = InclusiveCompare(elm, tmp);
117 }
else if (comp > 0) {
134 const uint64_t reg_o =
144 if (re >= reg_o && re <= reg_re) {
145 SCLogDebug(
"r %p is in-scope: %" PRIu64
" >= %" PRIu64
" && %" PRIu64
" <= %" PRIu64, r, re,
161 SCLogDebug(
"looking for first region matching %" PRIu64
"/%" PRIu64,
offset, data_re);
164 for (; r != NULL; r = r->
next) {
165 if (RegionsIntersect(cfg, r,
offset, data_re) ==
true) {
179 SCLogDebug(
"starting at %p/%" PRIu64
", offset %" PRIu64
", data_re %" PRIu64, r,
182 for (; r != NULL; r = r->
next) {
185 SCLogDebug(
"checking: %p/%" PRIu64
"/%" PRIu64
", offset %" PRIu64
"/%" PRIu64, r,
188 if (!RegionsIntersect(cfg, r,
offset, data_re))
204 for (; r != NULL; r = r->
next) {
205 if (!RegionsIntersect(cfg, r,
offset, data_re)) {
231 if (aux_r->
buf == NULL) {
232 FREE(cfg, aux_r,
sizeof(*aux_r));
265 int r = InitBuffer(sb, cfg);
289 FREE(cfg, r,
sizeof(*r));
339 (void)SBB_RB_INSERT(&sb->
sbb_tree, sbb);
348 sbb2->
len = data_len;
354 if (SBB_RB_INSERT(&sb->
sbb_tree, sbb2) != NULL) {
355 FREE(cfg, sbb2,
sizeof(*sbb2));
383 (void)SBB_RB_INSERT(&sb->
sbb_tree, sbb);
401 const uint64_t tr_re = tr->
offset + tr->
len;
422 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
424 SBB_RB_REMOVE(tree, tr);
434 }
else if (sa->
offset <= tr->
offset && sa_re >= tr_re) {
435 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
437 SBB_RB_REMOVE(tree, tr);
441 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
448 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
449 " bo %u sz %u BUF_OFFSET UPDATED",
460 sa_re >= tr->
offset && sa_re < tr_re)
463 uint32_t combined_len = sa->
len + tr->
len;
467 SBB_RB_REMOVE(tree, tr);
471 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
478 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
479 " bo %u sz %u BUF_OFFSET UPDATED",
495 const uint64_t tr_re = tr->
offset + tr->
len;
514 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
518 SBB_RB_REMOVE(tree, tr);
528 }
else if (sa->
offset <= tr->
offset && sa_re >= tr_re) {
529 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
533 SBB_RB_REMOVE(tree, tr);
537 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
545 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
546 " bo %u sz %u BUF_OFFSET UPDATED",
559 uint32_t combined_len = sa->
len + tr->
len;
566 SBB_RB_REMOVE(tree, tr);
570 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
577 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
578 " bo %u sz %u BUF_OFFSET UPDATED",
605 SCLogDebug(
"* insert failed: exact match in tree with %p %" PRIu64
"/%u", res, res->
offset,
613 if (SBB_RB_PREV(sbb) == NULL) {
616 ConsolidateBackward(sb, cfg, region, tree, sbb);
618 ConsolidateFwd(sb, cfg, region, tree, sbb);
664 if (sbb->
len >= shrink_by) {
665 sbb->
len -= shrink_by;
692 static inline uint32_t ToNextMultipleOf(
const uint32_t in,
const uint32_t
m)
696 const uint32_t x = in %
m;
704 static thread_local
bool g2s_warn_once =
false;
711 if (!g2s_warn_once) {
712 SCLogWarning(
"StreamingBuffer::GrowRegionToSize() tried to alloc %u bytes, exceeds "
715 g2s_warn_once =
true;
721 const uint32_t grow = ToNextMultipleOf(size, cfg->
buf_size);
732 size_t diff = grow - region->
buf_size;
733 void *new_mem = ((
char *)ptr) + region->
buf_size;
734 memset(new_mem, 0, diff);
740 if (region->
buf_size > sb->buf_size_max) {
741 sb->buf_size_max = region->
buf_size;
750 return GrowRegionToSize(sb, cfg, &sb->
region, size);
790 static inline void StreamingBufferSlideToOffsetWithRegions(
796 SCLogDebug(
"slide_offset %" PRIu64, slide_offset);
801 const bool main_is_oow = RegionBeforeOffset(&sb->
region, slide_offset);
822 if (RegionBeforeOffset(r, slide_offset)) {
829 FREE(cfg, r,
sizeof(*r));
832 }
else if (RegionContainsOffset(r, slide_offset)) {
851 if (to_shift && to_shift != &sb->
region) {
863 FREE(cfg, to_shift,
sizeof(*to_shift));
871 SCLogDebug(
"shift start region %p", to_shift);
880 const uint32_t new_data_size = to_shift->
buf_size - s;
881 uint32_t new_mem_size = ToNextMultipleOf(new_data_size, cfg->
buf_size);
892 const uint64_t next_re =
next->stream_offset +
next->buf_size;
893 const uint32_t mem_size = ToNextMultipleOf(next_re - slide_offset, cfg->
buf_size);
897 SCLogDebug(
"replace main with the next bigger region");
899 const uint32_t next_data_offset =
next->stream_offset - slide_offset;
900 const uint32_t prev_buf_size =
next->buf_size;
901 const uint32_t start_data_offset = slide_offset - start->
stream_offset;
903 if (start_data_offset > start->
buf_size) {
904 new_mem_size = new_data_size;
908 if (GrowRegionToSize(sb, cfg,
next, mem_size) != 0) {
909 new_mem_size = new_data_size;
918 memmove(
next->buf + next_data_offset,
next->buf, prev_buf_size);
925 const uint32_t start_data_size = start->
buf_size - start_data_offset;
926 memcpy(
next->buf, start->
buf + start_data_offset, start_data_size);
944 if (GrowRegionToSize(sb, cfg, start, mem_size) != 0) {
945 new_mem_size = new_data_size;
948 SCLogDebug(
"start->buf now size %u", mem_size);
953 SCLogDebug(
"s %u new_data_size %u", s, new_data_size);
954 memmove(start->
buf, start->
buf + s, new_data_size);
960 SCLogDebug(
"copy next->buf %p/%u to start->buf offset %u",
next->buf,
961 next->buf_size, new_data_size);
962 memcpy(start->
buf + new_data_size,
next->buf,
next->buf_size);
977 SCLogDebug(
"s %u new_data_size %u", s, new_data_size);
978 memmove(to_shift->
buf, to_shift->
buf + s, new_data_size);
986 if (s < to_shift->buf_offset)
1014 StreamingBufferSlideToOffsetWithRegions(sb, cfg,
offset);
1032 if (sb->
head != NULL) {
1036 SCLogDebug(
"sliding %u forward, size of original buffer left after slide %u", slide,
1052 SCLogDebug(
"sliding %u forward, size of original buffer left after slide %u", slide,
1074 uint64_t len64 =
len;
1075 if (len64 + buf_offset64 > UINT32_MAX) {
1082 StreamingBufferSegment *seg,
const uint8_t *data, uint32_t data_len)
1088 if (InitBuffer(sb, cfg) == -1)
1092 int r = DataFits(sb, data_len);
1096 }
else if (r == 0) {
1098 if (GrowToSize(sb, cfg, data_len) !=
SC_OK)
1106 if (DataFits(sb, data_len) != 1)
1111 seg->segment_len = data_len;
1116 return SBBUpdate(sb, cfg, &sb->
region, rel_offset, data_len);
1126 const uint8_t *data, uint32_t data_len)
1131 if (InitBuffer(sb, cfg) == -1)
1135 int r = DataFits(sb, data_len);
1139 }
else if (r == 0) {
1141 if (GrowToSize(sb, cfg, data_len) !=
SC_OK)
1155 return SBBUpdate(sb, cfg, &sb->
region, rel_offset, data_len);
1161 static int DataFitsAtOffset(
1164 const uint64_t offset64 =
offset;
1165 const uint64_t len64 =
len;
1166 if (offset64 + len64 > UINT32_MAX)
1168 return (
offset + len <= region->buf_size);
1171 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1204 #if defined(DEBUG) && DUMP_REGIONS == 1
1210 snprintf(gap,
sizeof(gap),
"[ gap:%" PRIu64
" ]",
1214 printf(
"[ %s offset:%" PRIu64
" size:%u offset:%u ]%s", r == &sb->
region ?
"main" :
"aux",
1218 bool at_least_one =
false;
1223 if (last_re != sbb->
offset) {
1224 printf(
"[ gap:%" PRIu64
" ]", sbb->
offset - last_re);
1226 printf(
"[ sbb offset:%" PRIu64
" len:%u ]", sbb->
offset, sbb->
len);
1227 at_least_one =
true;
1233 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1237 const uint8_t *_data = NULL;
1238 uint32_t _data_len = 0;
1253 uint32_t dst_buf_size)
1257 const uint64_t data_re = data_offset + data_len;
1258 SCLogDebug(
"sb %p dst %p src_start %p src_end %p data_offset %" PRIu64
1259 "/data_len %u/data_re %" PRIu64,
1260 sb,
dst, src_start, src_end, data_offset, data_len, data_re);
1266 const uint32_t dst_size = dst_buf_size;
1270 const uint32_t old_size =
dst->buf_size;
1271 const uint32_t dst_copy_offset =
dst->stream_offset - dst_offset;
1273 const uint32_t old_offset =
dst->buf_offset;
1274 SCLogDebug(
"old_size %u, old_offset %u, dst_copy_offset %u", old_size, old_offset,
1277 if ((retval = GrowRegionToSize(sb, cfg,
dst, dst_size)) !=
SC_OK) {
1284 if (dst_copy_offset != 0)
1285 memmove(
dst->buf + dst_copy_offset,
dst->buf, old_size);
1286 if (dst_offset !=
dst->stream_offset) {
1287 dst->stream_offset = dst_offset;
1289 dst->buf_offset = 0;
1294 new_offset += data_len;
1297 bool start_is_main =
false;
1298 if (src_start == &sb->
region) {
1301 start_is_main =
true;
1303 if (src_start !=
dst)
1305 if (src_start == src_end) {
1306 SCLogDebug(
"src_start == src_end == main, we're done");
1311 src_start = src_start->
next;
1321 SCLogDebug(
"skipping r %p as it is 'dst'", r);
1328 const uint32_t target_offset = r->
stream_offset - dst_offset;
1329 SCLogDebug(
"r %p: target_offset %u", r, target_offset);
1336 FREE(cfg, r,
sizeof(*r));
1356 if (start_is_main &&
dst != &sb->
region) {
1358 SCLogDebug(
"start_is_main && dst != main region");
1373 SCLogDebug(
"returning dst %p stream_offset %" PRIu64
" buf_offset %u buf_size %u",
dst,
1374 dst->stream_offset,
dst->buf_offset,
dst->buf_size);
1387 const uint64_t insert_re =
offset +
len;
1389 uint64_t insert_adjusted_re = insert_re;
1407 uint32_t new_buf_size =
1408 ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->
buf_size);
1418 ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->
buf_size);
1425 sb, cfg, big, start, end,
offset,
len, start_prev, new_buf_size);
1438 SCLogDebug(
"no matching region found, append to %p (%s)", append,
1439 append == &sb->
region ?
"main" :
"aux");
1464 SCLogDebug(
"data_offset %" PRIu64
", data_len %u, re %" PRIu64, data_offset, data_len,
1465 data_offset + data_len);
1468 if (RegionsIntersect(cfg, &sb->
region, data_offset, data_offset + data_len)) {
1469 SCLogDebug(
"data_offset %" PRIu64
", data_len %u intersects with main region (next %p)",
1472 !RegionsIntersect(cfg, sb->
region.
next, data_offset, data_offset + data_len)) {
1474 "data_offset %" PRIu64
1475 ", data_len %u intersects with main region, no next or way before next region",
1476 data_offset, data_len);
1479 if ((r = InitBuffer(sb, cfg)) !=
SC_OK) {
1493 SCLogDebug(
"created new region %p with offset %" PRIu64
", size %u", aux_r,
1510 StreamingBufferSegment *seg,
const uint8_t *data, uint32_t data_len, uint64_t
offset)
1520 if (region == NULL) {
1524 const bool region_is_main = region == &sb->
region;
1526 SCLogDebug(
"inserting %" PRIu64
"/%u using %s region %p",
offset, data_len,
1527 region == &sb->
region ?
"main" :
"aux", region);
1530 int r = DataFitsAtOffset(region, data_len, rel_offset);
1534 }
else if (r == 0) {
1535 if ((r = GrowToSize(sb, cfg, (rel_offset + data_len))) !=
SC_OK)
1540 SCLogDebug(
"offset %" PRIu64
" data_len %u, rel_offset %u into region offset %" PRIu64
1541 ", buf_offset %u, buf_size %u",
1544 memcpy(region->
buf + rel_offset, data, data_len);
1545 seg->stream_offset =
offset;
1546 seg->segment_len = data_len;
1548 SCLogDebug(
"rel_offset %u region->stream_offset %" PRIu64
", buf_offset %u", rel_offset,
1554 if (region_is_main) {
1556 SCLogDebug(
"empty sbb list: block exactly what was expected, fall through");
1562 SCLogDebug(
"empty sbb list: block is within existing main data region");
1569 }
else if (rel_offset < sb->region.
buf_offset) {
1572 SCLogDebug(
"before expected offset: %u < sb->region.buf_offset %u", rel_offset,
1575 SCLogDebug(
"before expected offset, ends after: %u < sb->region.buf_offset "
1583 SCLogDebug(
"beyond expected offset: SBBInit");
1585 if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1589 SCLogDebug(
"empty sbb list: invoking SBBInitLeadingGap");
1590 if ((r = SBBInitLeadingGap(sb, cfg,
offset, data_len)) !=
SC_OK)
1597 SCLogDebug(
"empty sbb list, no data in main: use SBBInit");
1598 if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1602 SCLogDebug(
"empty sbb list: invoking SBBInitLeadingGap");
1603 if ((r = SBBInitLeadingGap(sb, cfg,
offset, data_len)) !=
SC_OK)
1615 if ((r = SBBUpdate(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1629 const StreamingBufferSegment *seg)
1652 if (
offset >= r->stream_offset &&
offset < (r->stream_offset + r->buf_size)) {
1662 const uint8_t **data, uint32_t *data_len)
1665 SCLogDebug(
"first find our region (offset %" PRIu64
") -> %p", sbb->
offset, region);
1678 *data_len = sbb->
len;
1683 if (offset < sbb->
len) {
1684 *data = region->
buf;
1698 const uint8_t **data, uint32_t *data_len,
1715 *data = region->
buf + data_offset;
1716 if (data_offset + sbblen > region->
buf_size)
1717 *data_len = region->
buf_size - data_offset;
1724 if (data_offset < sbblen) {
1725 *data = region->
buf;
1726 *data_len = sbblen - data_offset;
1738 const StreamingBufferSegment *seg,
1739 const uint8_t **data, uint32_t *data_len)
1749 *data_len = seg->segment_len;
1755 *data = region->
buf;
1756 *data_len = seg->segment_len -
offset;
1771 const StreamingBufferSegment *seg,
1772 const uint8_t *rawdata, uint32_t rawdata_len)
1774 const uint8_t *segdata = NULL;
1775 uint32_t segdata_len = 0;
1777 if (segdata && segdata_len &&
1778 segdata_len == rawdata_len &&
1779 memcmp(segdata, rawdata, segdata_len) == 0)
1787 const uint8_t **data, uint32_t *data_len,
1790 if (sb != NULL && sb->
region.
buf != NULL) {
1804 const uint8_t **data, uint32_t *data_len,
1811 *data = region->
buf + skip;
1826 const uint8_t *rawdata, uint32_t rawdata_len)
1828 const uint8_t *sbdata = NULL;
1829 uint32_t sbdata_len = 0;
1833 sbdata && sbdata_len &&
1834 sbdata_len == rawdata_len &&
1835 memcmp(sbdata, rawdata, sbdata_len) == 0)
1842 printf(
"wanted:\n");
1853 static void DumpSegment(
StreamingBuffer *sb, StreamingBufferSegment *seg)
1855 const uint8_t *data = NULL;
1856 uint32_t data_len = 0;
1858 if (data && data_len) {
1863 static int StreamingBufferTest02(
void)
1869 StreamingBufferSegment seg1;
1871 StreamingBufferSegment seg2;
1875 FAIL_IF(seg1.stream_offset != 0);
1876 FAIL_IF(seg2.stream_offset != 8);
1880 DumpSegment(sb, &seg1);
1881 DumpSegment(sb, &seg2);
1889 StreamingBufferSegment seg3;
1893 FAIL_IF(seg3.stream_offset != 16);
1898 DumpSegment(sb, &seg1);
1899 DumpSegment(sb, &seg2);
1900 DumpSegment(sb, &seg3);
1909 DumpSegment(sb, &seg1);
1910 DumpSegment(sb, &seg2);
1911 DumpSegment(sb, &seg3);
1919 static int StreamingBufferTest03(
void)
1925 StreamingBufferSegment seg1;
1927 StreamingBufferSegment seg2;
1931 FAIL_IF(seg1.stream_offset != 0);
1932 FAIL_IF(seg2.stream_offset != 14);
1936 DumpSegment(sb, &seg1);
1937 DumpSegment(sb, &seg2);
1942 StreamingBufferSegment seg3;
1946 FAIL_IF(seg3.stream_offset != 8);
1951 DumpSegment(sb, &seg1);
1952 DumpSegment(sb, &seg2);
1953 DumpSegment(sb, &seg3);
1963 DumpSegment(sb, &seg1);
1964 DumpSegment(sb, &seg2);
1965 DumpSegment(sb, &seg3);
1974 static int StreamingBufferTest04(
void)
1980 StreamingBufferSegment seg1;
1983 StreamingBufferSegment seg2;
1987 FAIL_IF(seg1.stream_offset != 0);
1988 FAIL_IF(seg2.stream_offset != 14);
2003 DumpSegment(sb, &seg1);
2004 DumpSegment(sb, &seg2);
2007 StreamingBufferSegment seg3;
2011 FAIL_IF(seg3.stream_offset != 8);
2022 DumpSegment(sb, &seg1);
2023 DumpSegment(sb, &seg2);
2024 DumpSegment(sb, &seg3);
2030 StreamingBufferSegment seg4;
2035 FAIL_IF(seg4.stream_offset != 124);
2047 DumpSegment(sb, &seg1);
2048 DumpSegment(sb, &seg2);
2049 DumpSegment(sb, &seg3);
2050 DumpSegment(sb, &seg4);
2065 static int StreamingBufferTest06(
void)
2071 StreamingBufferSegment seg1;
2073 StreamingBufferSegment seg2;
2080 StreamingBufferSegment seg3;
2087 StreamingBufferSegment seg4;
2094 StreamingBufferSegment seg5;
2106 StreamingBufferSegment seg6;
2123 static int StreamingBufferTest07(
void)
2129 StreamingBufferSegment seg1;
2131 StreamingBufferSegment seg2;
2138 StreamingBufferSegment seg3;
2145 StreamingBufferSegment seg4;
2152 StreamingBufferSegment seg5;
2164 StreamingBufferSegment seg6;
2181 static int StreamingBufferTest08(
void)
2187 StreamingBufferSegment seg1;
2189 StreamingBufferSegment seg2;
2196 StreamingBufferSegment seg3;
2203 StreamingBufferSegment seg4;
2210 StreamingBufferSegment seg5;
2222 StreamingBufferSegment seg6;
2239 static int StreamingBufferTest09(
void)
2245 StreamingBufferSegment seg1;
2247 StreamingBufferSegment seg2;
2254 StreamingBufferSegment seg3;
2261 StreamingBufferSegment seg4;
2268 StreamingBufferSegment seg5;
2280 StreamingBufferSegment seg6;
2297 static int StreamingBufferTest10(
void)
2303 StreamingBufferSegment seg1;
2306 StreamingBufferSegment seg2;
2309 StreamingBufferSegment seg3;
2315 StreamingBufferSegment seg4;
2318 StreamingBufferSegment seg5;
2321 StreamingBufferSegment seg6;
2328 StreamingBufferSegment seg7;
2339 StreamingBufferSegment seg8;
2355 static int StreamingBufferTest11(
void)
2361 StreamingBufferSegment seg1;
2363 StreamingBufferSegment seg2;
2364 unsigned int data_len = 0xffffffff;
2367 sb, &cfg, &seg2, (
const uint8_t *)
"abcdefghij", data_len, 100000) !=
SC_ELIMIT);
2372 static const char *dummy_conf_string =
"%YAML 1.1\n"
2382 static int StreamingBufferTest12(
void)
2395 StreamingBufferSegment seg1;
2398 StreamingBufferSegment seg2;
2400 (
const uint8_t *)
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ",
2423 UtRegisterTest(
"StreamingBufferTest11 Bug 6903", StreamingBufferTest11);
2424 UtRegisterTest(
"StreamingBufferTest12 Bug 6782", StreamingBufferTest12);