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 || reg_re <
offset) {
158 SCLogDebug(
"looking for first region matching %" PRIu64
"/%" PRIu64,
offset, data_re);
161 for (; r != NULL; r = r->
next) {
162 if (RegionsIntersect(cfg, r,
offset, data_re) ==
true) {
176 SCLogDebug(
"starting at %p/%" PRIu64
", offset %" PRIu64
", data_re %" PRIu64, r,
179 for (; r != NULL; r = r->
next) {
182 SCLogDebug(
"checking: %p/%" PRIu64
"/%" PRIu64
", offset %" PRIu64
"/%" PRIu64, r,
185 if (!RegionsIntersect(cfg, r,
offset, data_re))
201 for (; r != NULL; r = r->
next) {
202 if (!RegionsIntersect(cfg, r,
offset, data_re)) {
228 if (aux_r->
buf == NULL) {
229 FREE(cfg, aux_r,
sizeof(*aux_r));
262 int r = InitBuffer(sb, cfg);
286 FREE(cfg, r,
sizeof(*r));
336 (void)SBB_RB_INSERT(&sb->
sbb_tree, sbb);
345 sbb2->
len = data_len;
351 if (SBB_RB_INSERT(&sb->
sbb_tree, sbb2) != NULL) {
352 FREE(cfg, sbb2,
sizeof(*sbb2));
380 (void)SBB_RB_INSERT(&sb->
sbb_tree, sbb);
398 const uint64_t tr_re = tr->
offset + tr->
len;
419 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
421 SBB_RB_REMOVE(tree, tr);
431 }
else if (sa->
offset <= tr->
offset && sa_re >= tr_re) {
432 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
434 SBB_RB_REMOVE(tree, tr);
438 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
445 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
446 " bo %u sz %u BUF_OFFSET UPDATED",
457 sa_re >= tr->
offset && sa_re < tr_re)
460 uint32_t combined_len = sa->
len + tr->
len;
464 SBB_RB_REMOVE(tree, tr);
468 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
475 SCLogDebug(
"-> (fwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
476 " bo %u sz %u BUF_OFFSET UPDATED",
492 const uint64_t tr_re = tr->
offset + tr->
len;
511 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
515 SBB_RB_REMOVE(tree, tr);
525 }
else if (sa->
offset <= tr->
offset && sa_re >= tr_re) {
526 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
530 SBB_RB_REMOVE(tree, tr);
534 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
542 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
543 " bo %u sz %u BUF_OFFSET UPDATED",
556 uint32_t combined_len = sa->
len + tr->
len;
563 SBB_RB_REMOVE(tree, tr);
567 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
" bo %u sz %u", sa,
574 SCLogDebug(
"-> (bwd) tr %p %" PRIu64
"/%u region %p so %" PRIu64
575 " bo %u sz %u BUF_OFFSET UPDATED",
602 SCLogDebug(
"* insert failed: exact match in tree with %p %" PRIu64
"/%u", res, res->
offset,
610 if (SBB_RB_PREV(sbb) == NULL) {
613 ConsolidateBackward(sb, cfg, region, tree, sbb);
615 ConsolidateFwd(sb, cfg, region, tree, sbb);
661 if (sbb->
len >= shrink_by) {
662 sbb->
len -= shrink_by;
689 static inline uint32_t ToNextMultipleOf(
const uint32_t in,
const uint32_t
m)
693 const uint32_t x = in %
m;
701 static thread_local
bool g2s_warn_once =
false;
708 if (!g2s_warn_once) {
709 SCLogWarning(
"StreamingBuffer::GrowRegionToSize() tried to alloc %u bytes, exceeds "
712 g2s_warn_once =
true;
718 const uint32_t grow = ToNextMultipleOf(size, cfg->
buf_size);
720 if (grow <= region->buf_size) {
733 size_t diff = grow - region->
buf_size;
734 void *new_mem = ((
char *)ptr) + region->
buf_size;
735 memset(new_mem, 0, diff);
741 if (region->
buf_size > sb->buf_size_max) {
742 sb->buf_size_max = region->
buf_size;
751 return GrowRegionToSize(sb, cfg, &sb->
region, size);
791 static inline void StreamingBufferSlideToOffsetWithRegions(
797 SCLogDebug(
"slide_offset %" PRIu64, slide_offset);
802 const bool main_is_oow = RegionBeforeOffset(&sb->
region, slide_offset);
823 if (RegionBeforeOffset(r, slide_offset)) {
830 FREE(cfg, r,
sizeof(*r));
833 }
else if (RegionContainsOffset(r, slide_offset)) {
852 if (to_shift && to_shift != &sb->
region) {
864 FREE(cfg, to_shift,
sizeof(*to_shift));
872 SCLogDebug(
"shift start region %p", to_shift);
881 const uint32_t new_data_size = to_shift->
buf_size - s;
882 uint32_t new_mem_size = ToNextMultipleOf(new_data_size, cfg->
buf_size);
893 const uint64_t next_re =
next->stream_offset +
next->buf_size;
894 const uint32_t mem_size = ToNextMultipleOf(next_re - slide_offset, cfg->
buf_size);
898 SCLogDebug(
"replace main with the next bigger region");
900 const uint32_t next_data_offset =
next->stream_offset - slide_offset;
901 const uint32_t prev_buf_size =
next->buf_size;
902 const uint32_t start_data_offset = slide_offset - start->
stream_offset;
904 if (start_data_offset > start->
buf_size) {
905 new_mem_size = new_data_size;
909 if (GrowRegionToSize(sb, cfg,
next, mem_size) != 0) {
910 new_mem_size = new_data_size;
919 memmove(
next->buf + next_data_offset,
next->buf, prev_buf_size);
926 const uint32_t start_data_size = start->
buf_size - start_data_offset;
927 memcpy(
next->buf, start->
buf + start_data_offset, start_data_size);
948 if (GrowRegionToSize(sb, cfg, start, mem_size) != 0) {
949 new_mem_size = new_data_size;
953 SCLogDebug(
"start->buf now size %u", mem_size);
958 SCLogDebug(
"s %u new_data_size %u", s, new_data_size);
959 memmove(start->
buf, start->
buf + s, new_data_size);
965 SCLogDebug(
"copy next->buf %p/%u to start->buf offset %u",
next->buf,
966 next->buf_size, new_data_size);
967 memcpy(start->
buf + new_data_size,
next->buf,
next->buf_size);
982 SCLogDebug(
"s %u new_data_size %u", s, new_data_size);
983 memmove(to_shift->
buf, to_shift->
buf + s, new_data_size);
991 if (s < to_shift->buf_offset)
1019 StreamingBufferSlideToOffsetWithRegions(sb, cfg,
offset);
1037 if (sb->
head != NULL) {
1041 SCLogDebug(
"sliding %u forward, size of original buffer left after slide %u", slide,
1057 SCLogDebug(
"sliding %u forward, size of original buffer left after slide %u", slide,
1079 uint64_t len64 =
len;
1080 if (len64 + buf_offset64 > UINT32_MAX) {
1087 StreamingBufferSegment *seg,
const uint8_t *data, uint32_t data_len)
1093 if (InitBuffer(sb, cfg) == -1)
1097 int r = DataFits(sb, data_len);
1101 }
else if (r == 0) {
1103 if (GrowToSize(sb, cfg, data_len) !=
SC_OK)
1111 if (DataFits(sb, data_len) != 1)
1116 seg->segment_len = data_len;
1121 return SBBUpdate(sb, cfg, &sb->
region, rel_offset, data_len);
1131 const uint8_t *data, uint32_t data_len)
1136 if (InitBuffer(sb, cfg) == -1)
1140 int r = DataFits(sb, data_len);
1144 }
else if (r == 0) {
1146 if (GrowToSize(sb, cfg, data_len) !=
SC_OK)
1160 return SBBUpdate(sb, cfg, &sb->
region, rel_offset, data_len);
1166 static int DataFitsAtOffset(
1169 const uint64_t offset64 =
offset;
1170 const uint64_t len64 =
len;
1171 if (offset64 + len64 > UINT32_MAX)
1173 return (
offset + len <= region->buf_size);
1176 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1209 #if defined(DEBUG) && DUMP_REGIONS == 1
1215 snprintf(gap,
sizeof(gap),
"[ gap:%" PRIu64
" ]",
1219 printf(
"[ %s offset:%" PRIu64
" size:%u offset:%u ]%s", r == &sb->
region ?
"main" :
"aux",
1223 bool at_least_one =
false;
1228 if (last_re != sbb->
offset) {
1229 printf(
"[ gap:%" PRIu64
" ]", sbb->
offset - last_re);
1231 printf(
"[ sbb offset:%" PRIu64
" len:%u ]", sbb->
offset, sbb->
len);
1232 at_least_one =
true;
1238 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1242 const uint8_t *_data = NULL;
1243 uint32_t _data_len = 0;
1258 uint32_t dst_buf_size)
1262 const uint64_t data_re = data_offset + data_len;
1263 SCLogDebug(
"sb %p dst %p src_start %p src_end %p data_offset %" PRIu64
1264 "/data_len %u/data_re %" PRIu64,
1265 sb,
dst, src_start, src_end, data_offset, data_len, data_re);
1271 const uint32_t dst_size = dst_buf_size;
1275 const uint32_t old_size =
dst->buf_size;
1276 const uint32_t dst_copy_offset =
dst->stream_offset - dst_offset;
1278 const uint32_t old_offset =
dst->buf_offset;
1279 SCLogDebug(
"old_size %u, old_offset %u, dst_copy_offset %u", old_size, old_offset,
1282 if ((retval = GrowRegionToSize(sb, cfg,
dst, dst_size)) !=
SC_OK) {
1289 if (dst_copy_offset != 0)
1290 memmove(
dst->buf + dst_copy_offset,
dst->buf, old_size);
1291 if (dst_offset !=
dst->stream_offset) {
1292 dst->stream_offset = dst_offset;
1294 dst->buf_offset = 0;
1299 new_offset += data_len;
1302 bool start_is_main =
false;
1303 if (src_start == &sb->
region) {
1306 start_is_main =
true;
1308 if (src_start !=
dst)
1310 if (src_start == src_end) {
1311 SCLogDebug(
"src_start == src_end == main, we're done");
1316 src_start = src_start->
next;
1326 SCLogDebug(
"skipping r %p as it is 'dst'", r);
1333 const uint32_t target_offset = r->
stream_offset - dst_offset;
1334 SCLogDebug(
"r %p: target_offset %u", r, target_offset);
1341 FREE(cfg, r,
sizeof(*r));
1361 if (start_is_main &&
dst != &sb->
region) {
1363 SCLogDebug(
"start_is_main && dst != main region");
1378 SCLogDebug(
"returning dst %p stream_offset %" PRIu64
" buf_offset %u buf_size %u",
dst,
1379 dst->stream_offset,
dst->buf_offset,
dst->buf_size);
1392 const uint64_t insert_re =
offset +
len;
1394 uint64_t insert_adjusted_re = insert_re;
1412 uint32_t new_buf_size =
1413 ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->
buf_size);
1423 ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->
buf_size);
1430 sb, cfg, big, start, end,
offset,
len, start_prev, new_buf_size);
1443 SCLogDebug(
"no matching region found, append to %p (%s)", append,
1444 append == &sb->
region ?
"main" :
"aux");
1469 SCLogDebug(
"data_offset %" PRIu64
", data_len %u, re %" PRIu64, data_offset, data_len,
1470 data_offset + data_len);
1473 if (RegionsIntersect(cfg, &sb->
region, data_offset, data_offset + data_len)) {
1474 SCLogDebug(
"data_offset %" PRIu64
", data_len %u intersects with main region (next %p)",
1477 !RegionsIntersect(cfg, sb->
region.
next, data_offset, data_offset + data_len)) {
1479 "data_offset %" PRIu64
1480 ", data_len %u intersects with main region, no next or way before next region",
1481 data_offset, data_len);
1484 if ((r = InitBuffer(sb, cfg)) !=
SC_OK) {
1498 SCLogDebug(
"created new region %p with offset %" PRIu64
", size %u", aux_r,
1515 StreamingBufferSegment *seg,
const uint8_t *data, uint32_t data_len, uint64_t
offset)
1525 if (region == NULL) {
1529 const bool region_is_main = region == &sb->
region;
1531 SCLogDebug(
"inserting %" PRIu64
"/%u using %s region %p",
offset, data_len,
1532 region == &sb->
region ?
"main" :
"aux", region);
1535 int r = DataFitsAtOffset(region, data_len, rel_offset);
1539 }
else if (r == 0) {
1540 if ((r = GrowToSize(sb, cfg, (rel_offset + data_len))) !=
SC_OK)
1545 SCLogDebug(
"offset %" PRIu64
" data_len %u, rel_offset %u into region offset %" PRIu64
1546 ", buf_offset %u, buf_size %u",
1549 memcpy(region->
buf + rel_offset, data, data_len);
1550 seg->stream_offset =
offset;
1551 seg->segment_len = data_len;
1553 SCLogDebug(
"rel_offset %u region->stream_offset %" PRIu64
", buf_offset %u", rel_offset,
1559 if (region_is_main) {
1561 SCLogDebug(
"empty sbb list: block exactly what was expected, fall through");
1567 SCLogDebug(
"empty sbb list: block is within existing main data region");
1574 }
else if (rel_offset < sb->region.
buf_offset) {
1577 SCLogDebug(
"before expected offset: %u < sb->region.buf_offset %u", rel_offset,
1580 SCLogDebug(
"before expected offset, ends after: %u < sb->region.buf_offset "
1588 SCLogDebug(
"beyond expected offset: SBBInit");
1590 if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1594 SCLogDebug(
"empty sbb list: invoking SBBInitLeadingGap");
1595 if ((r = SBBInitLeadingGap(sb, cfg,
offset, data_len)) !=
SC_OK)
1602 SCLogDebug(
"empty sbb list, no data in main: use SBBInit");
1603 if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1607 SCLogDebug(
"empty sbb list: invoking SBBInitLeadingGap");
1608 if ((r = SBBInitLeadingGap(sb, cfg,
offset, data_len)) !=
SC_OK)
1620 if ((r = SBBUpdate(sb, cfg, region, rel_offset, data_len)) !=
SC_OK)
1634 const StreamingBufferSegment *seg)
1657 if (
offset >= r->stream_offset &&
offset < (r->stream_offset + r->buf_size)) {
1667 const uint8_t **data, uint32_t *data_len)
1670 SCLogDebug(
"first find our region (offset %" PRIu64
") -> %p", sbb->
offset, region);
1683 *data_len = sbb->
len;
1688 if (offset < sbb->
len) {
1689 *data = region->
buf;
1703 const uint8_t **data, uint32_t *data_len,
1720 *data = region->
buf + data_offset;
1721 if (data_offset + sbblen > region->
buf_size)
1722 *data_len = region->
buf_size - data_offset;
1729 if (data_offset < sbblen) {
1730 *data = region->
buf;
1731 *data_len = sbblen - data_offset;
1743 const StreamingBufferSegment *seg,
1744 const uint8_t **data, uint32_t *data_len)
1754 *data_len = seg->segment_len;
1760 *data = region->
buf;
1761 *data_len = seg->segment_len -
offset;
1776 const StreamingBufferSegment *seg,
1777 const uint8_t *rawdata, uint32_t rawdata_len)
1779 const uint8_t *segdata = NULL;
1780 uint32_t segdata_len = 0;
1782 if (segdata && segdata_len &&
1783 segdata_len == rawdata_len &&
1784 memcmp(segdata, rawdata, segdata_len) == 0)
1792 const uint8_t **data, uint32_t *data_len,
1795 if (sb != NULL && sb->
region.
buf != NULL) {
1809 const uint8_t **data, uint32_t *data_len,
1816 *data = region->
buf + skip;
1831 const uint8_t *rawdata, uint32_t rawdata_len)
1833 const uint8_t *sbdata = NULL;
1834 uint32_t sbdata_len = 0;
1838 sbdata && sbdata_len &&
1839 sbdata_len == rawdata_len &&
1840 memcmp(sbdata, rawdata, sbdata_len) == 0)
1847 printf(
"wanted:\n");
1858 static void DumpSegment(
StreamingBuffer *sb, StreamingBufferSegment *seg)
1860 const uint8_t *data = NULL;
1861 uint32_t data_len = 0;
1863 if (data && data_len) {
1868 static int StreamingBufferTest02(
void)
1874 StreamingBufferSegment seg1;
1876 StreamingBufferSegment seg2;
1880 FAIL_IF(seg1.stream_offset != 0);
1881 FAIL_IF(seg2.stream_offset != 8);
1885 DumpSegment(sb, &seg1);
1886 DumpSegment(sb, &seg2);
1894 StreamingBufferSegment seg3;
1898 FAIL_IF(seg3.stream_offset != 16);
1903 DumpSegment(sb, &seg1);
1904 DumpSegment(sb, &seg2);
1905 DumpSegment(sb, &seg3);
1914 DumpSegment(sb, &seg1);
1915 DumpSegment(sb, &seg2);
1916 DumpSegment(sb, &seg3);
1924 static int StreamingBufferTest03(
void)
1930 StreamingBufferSegment seg1;
1932 StreamingBufferSegment seg2;
1936 FAIL_IF(seg1.stream_offset != 0);
1937 FAIL_IF(seg2.stream_offset != 14);
1941 DumpSegment(sb, &seg1);
1942 DumpSegment(sb, &seg2);
1947 StreamingBufferSegment seg3;
1951 FAIL_IF(seg3.stream_offset != 8);
1956 DumpSegment(sb, &seg1);
1957 DumpSegment(sb, &seg2);
1958 DumpSegment(sb, &seg3);
1968 DumpSegment(sb, &seg1);
1969 DumpSegment(sb, &seg2);
1970 DumpSegment(sb, &seg3);
1979 static int StreamingBufferTest04(
void)
1985 StreamingBufferSegment seg1;
1988 StreamingBufferSegment seg2;
1992 FAIL_IF(seg1.stream_offset != 0);
1993 FAIL_IF(seg2.stream_offset != 14);
2008 DumpSegment(sb, &seg1);
2009 DumpSegment(sb, &seg2);
2012 StreamingBufferSegment seg3;
2016 FAIL_IF(seg3.stream_offset != 8);
2027 DumpSegment(sb, &seg1);
2028 DumpSegment(sb, &seg2);
2029 DumpSegment(sb, &seg3);
2035 StreamingBufferSegment seg4;
2040 FAIL_IF(seg4.stream_offset != 124);
2052 DumpSegment(sb, &seg1);
2053 DumpSegment(sb, &seg2);
2054 DumpSegment(sb, &seg3);
2055 DumpSegment(sb, &seg4);
2070 static int StreamingBufferTest06(
void)
2076 StreamingBufferSegment seg1;
2078 StreamingBufferSegment seg2;
2085 StreamingBufferSegment seg3;
2092 StreamingBufferSegment seg4;
2099 StreamingBufferSegment seg5;
2111 StreamingBufferSegment seg6;
2128 static int StreamingBufferTest07(
void)
2134 StreamingBufferSegment seg1;
2136 StreamingBufferSegment seg2;
2143 StreamingBufferSegment seg3;
2150 StreamingBufferSegment seg4;
2157 StreamingBufferSegment seg5;
2169 StreamingBufferSegment seg6;
2186 static int StreamingBufferTest08(
void)
2192 StreamingBufferSegment seg1;
2194 StreamingBufferSegment seg2;
2201 StreamingBufferSegment seg3;
2208 StreamingBufferSegment seg4;
2215 StreamingBufferSegment seg5;
2227 StreamingBufferSegment seg6;
2244 static int StreamingBufferTest09(
void)
2250 StreamingBufferSegment seg1;
2252 StreamingBufferSegment seg2;
2259 StreamingBufferSegment seg3;
2266 StreamingBufferSegment seg4;
2273 StreamingBufferSegment seg5;
2285 StreamingBufferSegment seg6;
2302 static int StreamingBufferTest10(
void)
2308 StreamingBufferSegment seg1;
2311 StreamingBufferSegment seg2;
2314 StreamingBufferSegment seg3;
2320 StreamingBufferSegment seg4;
2323 StreamingBufferSegment seg5;
2326 StreamingBufferSegment seg6;
2333 StreamingBufferSegment seg7;
2344 StreamingBufferSegment seg8;
2360 static int StreamingBufferTest11(
void)
2366 StreamingBufferSegment seg1;
2368 StreamingBufferSegment seg2;
2369 unsigned int data_len = 0xffffffff;
2372 sb, &cfg, &seg2, (
const uint8_t *)
"abcdefghij", data_len, 100000) !=
SC_ELIMIT);
2377 static const char *dummy_conf_string =
"%YAML 1.1\n"
2387 static int StreamingBufferTest12(
void)
2400 StreamingBufferSegment seg1;
2403 StreamingBufferSegment seg2;
2405 (
const uint8_t *)
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ",
2428 UtRegisterTest(
"StreamingBufferTest11 Bug 6903", StreamingBufferTest11);
2429 UtRegisterTest(
"StreamingBufferTest12 Bug 6782", StreamingBufferTest12);