From: Justin Cinkelj <justin.cink...@xlab.si>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

wrk: allow larger file descriptor value

Commit c8b1906cf59d73a7cb39926c53dc4e136611d4f5 was broken, as it
allowed use of allocated array beyond its end. File descriptor value is
used as index into array. Array is allocated to be setsize large, and
check for "fd > setsize" prevents out-of-bound usage. Removing the check
was rather dumb :/

With this patch, the check remains in place. Instead, some extra memory
is allocated, so wrk is less likely to fail.

Signed-off-by: Justin Cinkelj <justin.cink...@xlab.si>
Message-Id: <20170904201649.13341-3-justin.cink...@xlab.si>

---
diff --git a/wrk/0003-wrk-allow-larger-file-descriptor-values.patch b/wrk/0003-wrk-allow-larger-file-descriptor-values.patch
--- a/wrk/0003-wrk-allow-larger-file-descriptor-values.patch
+++ b/wrk/0003-wrk-allow-larger-file-descriptor-values.patch
@@ -0,0 +1,53 @@
+From 40a8b2fac59623a7833d04bd9fc094f1a34f0310 Mon Sep 17 00:00:00 2001
+From: Justin Cinkelj <justin.cink...@xlab.si>
+Date: Mon, 4 Sep 2017 21:39:00 +0200
+Subject: [PATCH] wrk: allow larger file descriptor values
+
+Wrk uses socket FD number as index into dynamicaly allocated arrays.
+With say 1 conection, max expected FD was 10+3*1.
+
+On OSv, once other apps consume some FDs, arbitrary large FDs can be seen.
+Wrk than fails to start. Avoid this by allowing significantly higher
+FD values.
+
+Signed-off-by: Justin Cinkelj <justin.cink...@xlab.si>
+---
+ src/ae.c  | 5 +++++
+ src/wrk.c | 4 +++-
+ 2 files changed, 8 insertions(+), 1 deletion(-)
+
+diff --git a/src/ae.c b/src/ae.c
+index 90be4e2..f473626 100644
+--- a/src/ae.c
++++ b/src/ae.c
+@@ -105,7 +105,12 @@ void aeStop(aeEventLoop *eventLoop) {
+ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
+         aeFileProc *proc, void *clientData)
+ {
++    // The eventLoop->setsize is set by:
++ // ./src/wrk.c:145: t->loop = aeCreateEventLoop(10 + cfg.connections * 3);
++    // This is problematic on OSv. FD numbers are shared by all apps,
++    // and can get arbitrary large.
+     if (fd >= eventLoop->setsize) {
++ fprintf(stderr, "ERROR: wrk fd=%d > %d, increase setsize in wrk.c, aeCreateFileEvent()\n", fd, eventLoop->setsize);
+         errno = ERANGE;
+         return AE_ERR;
+     }
+diff --git a/src/wrk.c b/src/wrk.c
+index e64acae..14bd5e0 100644
+--- a/src/wrk.c
++++ b/src/wrk.c
+@@ -137,7 +137,9 @@ int main(int argc, char **argv) {
+
+     for (uint64_t i = 0; i < cfg.threads; i++) {
+         thread *t = &threads[i];
+-        t->loop        = aeCreateEventLoop(10 + cfg.connections * 3);
++        // "10 + cfg.connections * 3" is OK for Linux, and
++        // "+ 200" is required for OSv to allocate some extra space.
++ t->loop = aeCreateEventLoop(10 + cfg.connections * 3 + 200);
+         t->connections = connections;
+         t->stop_at     = stop_at;
+
+--
+2.9.5
+
diff --git a/wrk/Makefile b/wrk/Makefile
--- a/wrk/Makefile
+++ b/wrk/Makefile
@@ -13,6 +13,7 @@ wrk: $(tarball)
        mv wrk-$(version) wrk
        cd wrk && patch -p1 < ../0001-make-build-as-DSO.patch
        cd wrk && patch -p1 < ../wrk-fix-uninit-connection.patch
+ cd wrk && patch -p1 < ../0003-wrk-allow-larger-file-descriptor-values.patch

 clean:
        rm -rf wrk $(tarball)

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to