282 Commits

Author SHA1 Message Date
Cœur
5fbc00a3cc event_warn mmap format 2024-06-01 19:29:34 +03:00
Cœur
6d125f5486
Fix evbuffer_file_segment_new 64-bit support on Win32 (#1637)
This fixes the problematic #define fstat _fstat which would only support files up to 2 GB.

Also refactored it as evutil_fd_filesize to avoid exposing stat when not necessary.
2024-05-06 09:28:48 +02:00
Azat Khuzhin
539f73e319 Fix leak in evbuffer_add_file() on empty files
Found by oss-fuzz, after coverage had been improved in google/oss-fuzz#11257
v2: adjust test
v3: fix for windows (_get_osfhandle() crashes when called on closed fd)
v4: fix for EVENT__DISABLE_MM_REPLACEMENT
2024-03-12 09:29:47 +01:00
Ramon Ortega de Voor
ed35b30feb Fix two compiler errors for unused variable and undefined function
In buffer.c a variable "flags" and a label "done" are defined but
never used if "EVENT__HAVEMMAP" is not defined.

The code does not work on platforms which do not provide
the function `socketpair()`. Introduce EVENT__HAVE_SOCKETPAIR flag
which determines if `socketpair()` or `evutil_ersatz_socketpair()`
is used.
2024-02-19 08:19:24 +01:00
Andy Pan
55899c2fcb Use read/write instead of readv/writev for single buffer
The `readv`/`writev` functions are designed for scattered I/O optimally,
their logic in the kernel is more sophisticated, compared to read/write,
which includes extra on-stack `iovec` in the kernel space, importing `iovec`
array from user space to kernel space, reading/writing with `iov_iter`, etc.
As a result, using `readv`/`writev` on single-segment `iovec` will fall into
the special branch in the kernel where it is imported as `ITER_UBUF` differed
from `ITER_IOVEC` for multiple-segments `iovec`.

Thus, it is just not worth calling `readv`/`writev` for single-segment `iovec`,
we should use `read`/`write` instead, to save it from going through the
sophisticated yet unnecessary kernel code path, circumvent a waste of
kernel on-stack memory, copying `iovec` between user space and kernel space, etc.
2024-01-19 00:41:03 +03:00
Sam James
4c38de8cb3
Fix -Walloc-size (#1526)
Co-authored-by: Azat Khuzhin <azat@libevent.org>
2023-11-26 21:52:32 +01:00
Ramon Ortega de Voor
a579b1d77c Fix compile error in evbuffer_read()
The variable chainp is only used if USE_IOVEC_IMPL is defined.
This makes a strict compiler complain about unused variables,
since chainp is declared outside of an USE_IOVEC_IMPL block.
2023-10-21 23:20:47 +03:00
MBeanwenshengming
648ec50e11 Add function evbuffer_add_reference_with_offset()
This is the same as evbuffer_add_reference(), but allows to specify
offset in the @data

v2: rename evbuffer_add_reference_misalign() to evbuffer_add_reference_with_offset()
2023-09-21 22:02:26 +02:00
Dmitry Antipov
0b79a0024f buffer: use pread() for evbuffer_file_segment_materialize()
If pread(2) is available, prefer it over double lseek(2)
and read(2) in evbuffer_file_segment_materialize().

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2023-01-02 17:12:14 +03:00
Dmitry Ilyin
99fd68abde Add check of mmap64 function and use it when available rather that mmap
There can be issues on 32-bit architectures to mmap 2+GiB file, and to
make this portable between different version of glibc, mmap64 was
prefered over _FILE_OFFSET_BITS
2022-08-09 00:18:10 +02:00
cui fliter
1c204d5564
Fix some typos (#1284)
Signed-off-by: cuishuang <imcusg@gmail.com>
2022-06-12 23:58:50 +03:00
Pierce Lopez
fd0ce993e9 buffer: do not round up allocation for reference-type chain objects
Skip rounding up memory allocations for:
  * evbuffer_add_reference()
  * evbuffer_add_buffer_reference()
  * evbuffer_add_file_segment()

These chain objects only store small structs with references to
other things, and these small structs do not themselves grow, so
bumping up the allocation to MIN_BUFFER_SIZE (512 bytes) is wasteful.
2021-09-16 23:21:26 -04:00
Azat Khuzhin
6f139b8711 buffer: fix CreateFileMapping() leak from evbuffer_add_file()
evbuffer_file_segment_materialize() is called twice from
evbuffer_add_file(), and so win32 mapping will leak.

Fixes: #1186
2021-08-01 12:05:43 +03:00
fanquake
78e8541a9e
build: remove no-longer used checks for vasprintf
From what I can tell the last usage was removed in
8d1317d71c46e27c5073d3429a64af69de0351a6.
2021-03-28 13:55:59 +08:00
fanquake
4c29b01c87
build: remove splice implementation fragments
Looks like a `splice` implementation was planned, but has clearly never
eventuated (the TODO comment is from ~12 years ago, in
8b5bd77415fb6634fadf08357676926fecf5f032). For now, it's probably better
to remove the unused code/correct the docs.
2021-03-28 10:57:11 +08:00
Azat Khuzhin
a0c642ac04 buffer: do not pass NULL to memcpy() from evbuffer_pullup()
UBSAN reports:

  evbuffer/remove_buffer_with_empty3: ../buffer.c:1443:3: runtime error: null pointer passed as argument 2, which is declared to never be null
      #0 0x7ffff6cd0410 in evbuffer_pullup ../buffer.c:1443
      #1 0x5555556d68b9 in test_evbuffer_remove_buffer_with_empty3 ../test/regress_buffer.c:408
      #2 0x5555557b95ee in testcase_run_bare_ ../test/tinytest.c:173
      #3 0x5555557ba048 in testcase_run_one ../test/tinytest.c:333
      #4 0x5555557bc0f8 in tinytest_main ../test/tinytest.c:527
      #5 0x555555787702 in main ../test/regress_main.c:528
      #6 0x7ffff606c001 in __libc_start_main (/usr/lib/libc.so.6+0x27001)
      #7 0x55555569436d in _start (/src/le/libevent/.cmake-debug/bin/regress+0x14036d)
2020-06-25 10:15:01 +03:00
yuangongji
114b383675 increase segment refcnt only if evbuffer_add_file_segment() succeeds 2020-02-29 17:47:47 +08:00
Azat Khuzhin
4727150a54
evbuffer_add_file: fix freeing of segment in the error path
if evbuffer_add_file_segment() fails it returns -1, so we should call
evbuffer_file_segment_free() only on error, and this -1 not 0.

Fixes: 6a81b1f5 ("Avoid double-free on error in evbuffer_add_file. Found by coverity.")
Backport-to: 2.1
2019-09-21 23:32:55 +03:00
Azat Khuzhin
bdcade4722
buffer: fix possible NULL dereference in evbuffer_setcb() on ENOMEM
[ @azat:

  - add return heredoc for evbuffer_setcb()
  - add unit test with event_set_mem_functions()
  - look through the report from abi-compliance-checker/abi-dumper
]

Closes: #855
2019-07-31 10:43:57 +03:00
Azat Khuzhin
401bd1c09e
evbuffer: fix last_with_datap after prepend with empty chain
last_with_datap should be adjusted only if it buf->first *was* not
empty, otherwise last_with_datap should point to the prepended chain.
2019-05-16 10:16:21 +03:00
Azat Khuzhin
8c2001e92a
Maximum evbuffer read configuration
Before this patch evbuffer always reads 4K at a time, while this is fine
most of time you can find an example when this will decrease throughput.

So add an API to change default limit:
- evbuffer_set_max_read()
- evbuffer_get_max_read()

And a notice that most of time default is sane.
2019-03-16 17:30:15 +03:00
Azat Khuzhin
c4fbae3ae6
buffer: make evbuffer_prepend() of zero-length array no-op
Refs: #774
2019-03-03 19:05:54 +03:00
Azat Khuzhin
5b19c9f62b
buffer: do not rely on ->off in advance_last_with_data()
advance_last_with_data() adjusts evbuffer.last_with_datap, and if we
will have empty chain in the middle advance_last_with_data() will stop,
while it should not, since while empty chains is not regular thing they
can pops up in various places like, and while I did not look through all
of them the most tricky I would say is:
  evbuffer_reverse_space()/evbuffer_commit_space()
  evbuffer_add_reference()

Test case from:
  https://github.com/envoyproxy/envoy/pull/6062

Fixes: #778

v2: keep last_with_datap really last with data, i.e. update only if
chain has data in it
2019-03-03 18:55:25 +03:00
Azat Khuzhin
fdfabbec00
buffer: fix evbuffer_remove_buffer() with empty chain in front
In case we have empty chain (chain that do not have any data, i.e. ->off
== 0) at the beginning of the buffer, and no more full chains to move to
the dst, we will skip moving of this empty chain, and hence
last_with_datap will not be adjusted, and things will be broken after.

Fix this by not relying on ->off, just count if we have something to
move that's it.

Test case from:
  https://github.com/envoyproxy/envoy/pull/6062

Fixes: #774
2019-03-03 18:00:37 +03:00
Azat Khuzhin
931ec23702
Convert evbuffer_strspn() (internal helper) to use size_t
As pointed by @yankeehacker in #590:
  Signed to Unsigned Conversion Error - buffer.c:1623

  Description: This assignment creates a type mismatch by populating an
  unsigned variable with a signed value. The signed integer will be
  implicitly cast to an unsigned integer, converting negative values into
  positive ones. If an attacker can control the signed value, it may be
  possible to trigger a buffer overflow if the value specifies the length
  of a memory write.

  Remediation: Do not rely on implicit casts between signed and unsigned
  values because the result can take on an unexpected value and violate
  weak assumptions made elsewhere in the program.

Fixes: #590
2018-10-28 16:50:36 +03:00
Azat Khuzhin
f83ac92da9
buffer: add an assert for last_with_datap to suppress static analyzer
../buffer.c:2231:6: warning: Access to field 'flags' results in a dereference of a null pointer
          if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ../buffer.c:130:30: note: expanded from macro 'CHAIN_SPACE_LEN'
  #define CHAIN_SPACE_LEN(ch) ((ch)->flags & EVBUFFER_IMMUTABLE ? \
2018-10-28 15:24:11 +03:00
Jiri Luznicky
95918754d2
Fix missing LIST_HEAD
Despite the presence of 'sys/queue.h' in some stdlib implementations
(i.e. uclibc) 'LIST_HEAD' macro can be missing.  This fix defines this
macro in the same manner as was done previously for 'TAILQ_'.

Fixes: #539
Closes: #639 (cherry-picked)
Backport: 2.1.9
2018-10-20 03:05:53 +03:00
SuckShit
ab3224c3e6 Fix assert() condition in evbuffer_drain() for IOCP
In the case of iocp, in the for loop above, there is a situation where:
  remaining == chain->off == 0

And this happens due to CHAIN_PINNED_R() case (that is used only in
buffer_iocp.c)

Closes: #630 (picked)
2018-04-22 13:27:40 +03:00
Azat Khuzhin
93913da1c4 buffer: fix incorrect unlock of the buffer mutex (for deferred callbacks)
TSAN reports:
  WARNING: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) (pid=17111)
      #0 pthread_mutex_unlock /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:3621 (libtsan.so.0+0x00000003b71c)
      #1 evbuffer_add <null> (libevent_core-2.2.so.1+0x00000000ddb6)
      ...

    Mutex M392 (0x7b0c00000f00) created at:
      #0 pthread_mutex_init /build/gcc/src/gcc/libsanitizer/tsan/tsan_interceptors.cc:1117 (libtsan.so.0+0x0000000291af)
      #1 <null> <null> (libevent_pthreads-2.2.so.1+0x000000000d46)
      ...

$ addr2line -e /lib/libevent_core-2.2.so.1 0x00000000ddb6
/src/libevent/buffer.c:1815 (discriminator 1)

Introduced-in: ae2b84b2575be93d0aebba5c0b78453836f89f3c ("Replace
deferred_cbs with event_callback-based implementation.")
2018-02-12 19:54:27 +03:00
Azat Khuzhin
b26996a05d Fix wrong assert in evbuffer_drain()
"chain" cannot be NULL here because we have at least one chain (we
handle empty buffer separatelly) and hence loop will be executed at
least once.

Link: 841ecbd961 (commitcomment-23631347)
Signed-off-by: Ivan Maidanski <i.maidanski@samsung.com>
Signed-off-by: Azat Khuzhin <a3at.mail@gmail.com>
2017-08-14 23:11:03 +03:00
Azat Khuzhin
f479a00894 Use off_t instead of ev_off_t for sendfile() (fixes android build)
Refs: #475
2017-03-08 13:31:55 +03:00
Azat Khuzhin
2c62062e12 Fix signedness differ for iov_base (solaris) 2016-12-06 12:54:33 +03:00
Azat Khuzhin
8892f4cbef buffer: don't mix code and declarations 2016-07-07 13:15:41 +03:00
Azat Khuzhin
a3f4ccd1a1 buffer: fix overflow check in evbuffer_expand_singlechain()
Refs: #306
Fixes: #340
Fixes: 20d6d4458bee5d88bda1511c225c25b2d3198d6c
2016-06-26 01:59:42 +03:00
Azat Khuzhin
26fd9321cf buffer: evbuffer_add_buffer(): clean empty chains from destination buffer
@EMPanisset reported a problem (#358) with evbuffer_remove_buffer(), but
actually I think that the problem is in evbuffer_add_buffer() which introduces
this empty chain, all other callers (except evbuffer_prepend_buffer(), but it
doesn't have this problem though) should be safe.

And FWIW the only API that allows empty chains is evbuffer_add_reference(), and
we can add check there to avoid such issues, but for now I leaved this without
fixing, since I think that evbuffer_add_reference() with empty chains can be
used as a barrier (but this can be tricky).

Fixes: regress evbuffer/remove_buffer_with_empty2
v2: introduce/fixes evbuffer/add_buffer_with_empty
2016-06-17 17:41:03 +03:00
Azat Khuzhin
0abd0393ea Fix n_add_for_cb in evbuffer_prepend() in case of new buffer required
Signed-off-by: @luoming1224
Fixes: #349
2016-04-21 01:59:30 +03:00
Marcus Sundberg
a8769ef12d evbuffer_add: Use last_with_datap if set, not last.
evbuffer_add() would always put data in the last chain, even if there
was available space in a previous chain, and in doing so it also
failed to update last_with_datap, causing subsequent calls to other
functions that do look at last_with_datap to add data in the middle
of the evbuffer instead of at the end.

Fixes the evbuffer_add() part of issue #335, and the evbuffer/add2 and
evbuffer/add3 tests, and also prevents wasting space available in the
chain pointed to by last_with_datap.
2016-03-26 20:52:07 +01:00
Ed Schouten
fd36647af1 Don't use BSD u_* types.
These types are not part of POSIX. As we only use them in a small number
of places, we'd better replace them by C standard types. This makes a
larger part of the code build for CloudABI.
2015-08-25 19:15:50 +03:00
Nick Mathewson
841ecbd961 Fix CVE-2014-6272 in Libevent 2.1
For this fix, we need to make sure that passing too-large inputs to
the evbuffer functions can't make us do bad things with the heap.

Also, lower the maximum chunk size to the lower of off_t, size_t maximum.

This is necessary since otherwise we could get into an infinite loop
if we make a chunk that 'misalign' cannot index into.
2015-01-05 09:32:53 -05:00
Nick Mathewson
ba59923ab8 Fix evbuffer_peek() with len==-1 and start_at non-NULL. 2014-11-30 11:05:40 -05:00
Nick Mathewson
89c1a3b7fe Fix several memory leaks in the unit tests.
Also add a comment to buffer.c about why we call
evbuffer_file_segment_free on failure to add the segment.
2014-09-18 12:40:38 -04:00
Nick Mathewson
9d6acdc551 Merge remote-tracking branch 'origin/patches-2.0' 2014-08-29 15:00:33 -04:00
Nick Mathewson
60f8f729d3 Consistently check for failure from evbuffer_pullup()
Closes issue #148.
2014-08-29 14:59:17 -04:00
Nick Mathewson
d4095146af Avoid leaking segment mappings when offset is not a page multiple
Found by Bob / Black Hole on the mailing list.
2013-07-31 22:25:28 -04:00
Nick Mathewson
02fbf68770 Use finalization feature so bufferevents can avoid deadlocks
Since the bufferevents' events are now EV_FINALIZE (name pending),
they won't deadlock.  To clean up properly, though, we must use the
finalization feature.

This patch also split bufferevent deallocation into an "unlink" step
that happens fast, and a "destruct" step that happens after
finalization.

More work is needed: there needs to be a way to specify a finalizer
for the bufferevent's argument itself.  Also, this finalizer business
makes lots of the reference counting we were doing unnecessary.

Also, more testing is needed.
2013-04-26 12:18:07 -04:00
yangacer
e9f8febace Add a new callback to get called on evbuffer_file_segment free 2012-11-06 17:42:54 -05:00
Nick Mathewson
56e48c1019 Fix a few mingw64 incompatibilities introduced since 2.0 2012-11-02 10:58:02 -04:00
Nick Mathewson
6810908a5f Fix some warnings found cross-compiling with mingw32 2012-11-01 18:05:27 -04:00
Nick Mathewson
6a81b1f5a4 Avoid double-free on error in evbuffer_add_file. Found by coverity. 2012-07-26 10:45:10 -04:00
Nick Mathewson
9852107f37 Merge remote-tracking branch 'origin/patches-2.0'
Conflicts:
	buffer.c
	http.c
2012-07-26 10:43:13 -04:00