Bug Summary

File:lwan-thread.c
Warning:line 770, column 31
Array subscript is undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lwan-thread.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -fno-plt -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/clang/10.0.1 -include /home/buildbot/lwan-worker/clang-analyze/build/lwan-build-config.h -D _FILE_OFFSET_BITS=64 -D _TIME_BITS=64 -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib/missing -I /usr/include/luajit-2.0 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -I /home/buildbot/lwan-worker/clang-analyze/build -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/10.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu99 -fdebug-compilation-dir /home/buildbot/lwan-worker/clang-analyze/build/src/lib -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -faddrsig -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2020-10-24-174413-2651349-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c
1/*
2 * lwan - simple web server
3 * Copyright (c) 2012, 2013 Leandro A. F. Pereira <leandro@hardinfo.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 * USA.
19 */
20
21#define _GNU_SOURCE
22#include <assert.h>
23#include <errno(*__errno_location ()).h>
24#include <fcntl.h>
25#include <pthread.h>
26#include <sched.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/epoll.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <unistd.h>
33
34#if defined(HAVE_EVENTFD)
35#include <sys/eventfd.h>
36#endif
37
38#include "lwan-private.h"
39#include "lwan-tq.h"
40#include "list.h"
41
42static void lwan_strbuf_free_defer(void *data)
43{
44 lwan_strbuf_free((struct lwan_strbuf *)data);
45}
46
47static void graceful_close(struct lwan *l,
48 struct lwan_connection *conn,
49 char buffer[static DEFAULT_BUFFER_SIZE4096])
50{
51 int fd = lwan_connection_get_fd(l, conn);
52
53 while (TIOCOUTQ0x5411) {
54 /* This ioctl isn't probably doing what it says on the tin; the details
55 * are subtle, but it seems to do the trick to allow gracefully closing
56 * the connection in some cases with minimal system calls. */
57 int bytes_waiting;
58 int r = ioctl(fd, TIOCOUTQ0x5411, &bytes_waiting);
59
60 if (!r && !bytes_waiting) /* See note about close(2) below. */
61 return;
62 if (r < 0 && errno(*__errno_location ()) == EINTR4)
63 continue;
64
65 break;
66 }
67
68 if (UNLIKELY(shutdown(fd, SHUT_WR) < 0)__builtin_expect(((shutdown(fd, SHUT_WR) < 0)), (0))) {
69 if (UNLIKELY(errno == ENOTCONN)__builtin_expect((((*__errno_location ()) == 107)), (0)))
70 return;
71 }
72
73 for (int tries = 0; tries < 20; tries++) {
74 ssize_t r = read(fd, buffer, DEFAULT_BUFFER_SIZE4096);
75
76 if (!r)
77 break;
78
79 if (r < 0) {
80 switch (errno(*__errno_location ())) {
81 case EAGAIN11:
82 break;
83 case EINTR4:
84 continue;
85 default:
86 return;
87 }
88 }
89
90 coro_yield(conn->coro, CONN_CORO_WANT_READ);
91 }
92
93 /* close(2) will be called when the coroutine yields with CONN_CORO_ABORT */
94}
95
96__attribute__((noreturn)) static int process_request_coro(struct coro *coro,
97 void *data)
98{
99 /* NOTE: This function should not return; coro_yield should be used
100 * instead. This ensures the storage for `strbuf` is alive when the
101 * coroutine ends and lwan_strbuf_free() is called. */
102 struct lwan_connection *conn = data;
103 struct lwan *lwan = conn->thread->lwan;
104 int fd = lwan_connection_get_fd(lwan, conn);
105 enum lwan_request_flags flags = lwan->config.request_flags;
106 struct lwan_strbuf strbuf = LWAN_STRBUF_STATIC_INIT(struct lwan_strbuf) { .buffer = "" };
107 char request_buffer[DEFAULT_BUFFER_SIZE4096];
108 struct lwan_value buffer = {.value = request_buffer, .len = 0};
109 char *next_request = NULL((void*)0);
110 char *header_start[N_HEADER_START64];
111 struct lwan_proxy proxy;
112 const int error_when_n_packets = lwan_calculate_n_packets(DEFAULT_BUFFER_SIZE4096);
113
114 coro_defer(coro, lwan_strbuf_free_defer, &strbuf);
115
116 const size_t init_gen = 1; /* 1 call to coro_defer() */
117 assert(init_gen == coro_deferred_get_generation(coro))((void) sizeof ((init_gen == coro_deferred_get_generation(coro
)) ? 1 : 0), __extension__ ({ if (init_gen == coro_deferred_get_generation
(coro)) ; else __assert_fail ("init_gen == coro_deferred_get_generation(coro)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 117, __extension__ __PRETTY_FUNCTION__); }))
;
118
119 while (true1) {
120 struct lwan_request_parser_helper helper = {
121 .buffer = &buffer,
122 .next_request = next_request,
123 .error_when_n_packets = error_when_n_packets,
124 .header_start = header_start,
125 };
126 struct lwan_request request = {.conn = conn,
127 .global_response_headers = &lwan->headers,
128 .fd = fd,
129 .response = {.buffer = &strbuf},
130 .flags = flags,
131 .proxy = &proxy,
132 .helper = &helper};
133
134 lwan_process_request(lwan, &request);
135
136 /* Run the deferred instructions now (except those used to initialize
137 * the coroutine), so that if the connection is gracefully closed,
138 * the storage for ``helper'' is still there. */
139 coro_deferred_run(coro, init_gen);
140
141 if (UNLIKELY(!(conn->flags & CONN_IS_KEEP_ALIVE))__builtin_expect(((!(conn->flags & CONN_IS_KEEP_ALIVE)
)), (0))
) {
142 graceful_close(lwan, conn, request_buffer);
143 break;
144 }
145
146 if (next_request && *next_request) {
147 conn->flags |= CONN_CORK;
148
149 if (!(conn->flags & CONN_EVENTS_WRITE))
150 coro_yield(coro, CONN_CORO_WANT_WRITE);
151 } else {
152 conn->flags &= ~CONN_CORK;
153 coro_yield(coro, CONN_CORO_WANT_READ);
154 }
155
156 lwan_strbuf_reset(&strbuf);
157
158 /* Only allow flags from config. */
159 flags = request.flags & (REQUEST_PROXIED | REQUEST_ALLOW_CORS);
160 next_request = helper.next_request;
161 }
162
163 coro_yield(coro, CONN_CORO_ABORT);
164 __builtin_unreachable();
165}
166
167static ALWAYS_INLINEinline __attribute__((always_inline)) uint32_t
168conn_flags_to_epoll_events(enum lwan_connection_flags flags)
169{
170 static const uint32_t map[CONN_EVENTS_MASK + 1] = {
171 [0 /* Suspended (timer or await) */] = EPOLLRDHUPEPOLLRDHUP,
172 [CONN_EVENTS_WRITE] = EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP,
173 [CONN_EVENTS_READ] = EPOLLINEPOLLIN | EPOLLRDHUPEPOLLRDHUP,
174 [CONN_EVENTS_READ_WRITE] = EPOLLINEPOLLIN | EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP,
175 };
176
177 return map[flags & CONN_EVENTS_MASK];
178}
179
180static void update_epoll_flags(int fd,
181 struct lwan_connection *conn,
182 int epoll_fd,
183 enum lwan_connection_coro_yield yield_result)
184{
185 static const enum lwan_connection_flags or_mask[CONN_CORO_MAX] = {
186 [CONN_CORO_YIELD] = 0,
187
188 [CONN_CORO_WANT_READ_WRITE] = CONN_EVENTS_READ_WRITE,
189 [CONN_CORO_WANT_READ] = CONN_EVENTS_READ,
190 [CONN_CORO_WANT_WRITE] = CONN_EVENTS_WRITE,
191
192 /* While the coro is suspended, we're not interested in either EPOLLIN
193 * or EPOLLOUT events. We still want to track this fd in epoll, though,
194 * so unset both so that only EPOLLRDHUP (plus the implicitly-set ones)
195 * are set. */
196 [CONN_CORO_SUSPEND_TIMER] = CONN_SUSPENDED_TIMER,
197 [CONN_CORO_SUSPEND_ASYNC_AWAIT] = CONN_SUSPENDED_ASYNC_AWAIT,
198
199 /* Ideally, when suspending a coroutine, the current flags&CONN_EVENTS_MASK
200 * would have to be stored and restored -- however, resuming as if the
201 * client coroutine is interested in a write event always guarantees that
202 * they'll be resumed as they're TCP sockets. There's a good chance that
203 * trying to read from a socket after resuming a coroutine will succeed,
204 * but if it doesn't because read() returns -EAGAIN, the I/O wrappers will
205 * yield with CONN_CORO_WANT_READ anyway. */
206 [CONN_CORO_RESUME] = CONN_EVENTS_WRITE,
207 };
208 static const enum lwan_connection_flags and_mask[CONN_CORO_MAX] = {
209 [CONN_CORO_YIELD] = ~0,
210
211 [CONN_CORO_WANT_READ_WRITE] = ~0,
212 [CONN_CORO_WANT_READ] = ~CONN_EVENTS_WRITE,
213 [CONN_CORO_WANT_WRITE] = ~CONN_EVENTS_READ,
214
215 [CONN_CORO_SUSPEND_TIMER] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_ASYNC_AWAIT),
216 [CONN_CORO_SUSPEND_ASYNC_AWAIT] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_TIMER),
217 [CONN_CORO_RESUME] = ~CONN_SUSPENDED,
218 };
219 enum lwan_connection_flags prev_flags = conn->flags;
220
221 conn->flags |= or_mask[yield_result];
222 conn->flags &= and_mask[yield_result];
223
224 if (conn->flags == prev_flags)
225 return;
226
227 struct epoll_event event = {
228 .events = conn_flags_to_epoll_events(conn->flags),
229 .data.ptr = conn,
230 };
231
232 if (UNLIKELY(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0)__builtin_expect(((epoll_ctl(epoll_fd, 3, fd, &event) <
0)), (0))
)
233 lwan_status_perror("epoll_ctl")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 233, __FUNCTION__, "epoll_ctl")
;
234}
235
236static void clear_async_await_flag(void *data)
237{
238 struct lwan_connection *async_fd_conn = data;
239
240 async_fd_conn->flags &= ~CONN_ASYNC_AWAIT;
241}
242
243static enum lwan_connection_coro_yield
244resume_async(struct timeout_queue *tq,
245 enum lwan_connection_coro_yield yield_result,
246 int64_t from_coro,
247 struct lwan_connection *conn,
248 int epoll_fd)
249{
250 static const enum lwan_connection_flags to_connection_flags[] = {
251 [CONN_CORO_ASYNC_AWAIT_READ] = CONN_EVENTS_READ,
252 [CONN_CORO_ASYNC_AWAIT_WRITE] = CONN_EVENTS_WRITE,
253 [CONN_CORO_ASYNC_AWAIT_READ_WRITE] = CONN_EVENTS_READ_WRITE,
254 };
255 int await_fd = (int)((uint64_t)from_coro >> 32);
256 enum lwan_connection_flags flags;
257 int op;
258
259 assert(await_fd >= 0)((void) sizeof ((await_fd >= 0) ? 1 : 0), __extension__ ({
if (await_fd >= 0) ; else __assert_fail ("await_fd >= 0"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 259, __extension__ __PRETTY_FUNCTION__); }))
;
260 assert(yield_result >= CONN_CORO_ASYNC_AWAIT_READ &&((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 261, __extension__ __PRETTY_FUNCTION__); }))
261 yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE)((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 261, __extension__ __PRETTY_FUNCTION__); }))
;
262
263 flags = to_connection_flags[yield_result];
264
265 struct lwan_connection *await_fd_conn = &tq->lwan->conns[await_fd];
266 if (LIKELY(await_fd_conn->flags & CONN_ASYNC_AWAIT)__builtin_expect((!!(await_fd_conn->flags & CONN_ASYNC_AWAIT
)), (1))
) {
267 if (LIKELY((await_fd_conn->flags & CONN_EVENTS_MASK) == flags)__builtin_expect((!!((await_fd_conn->flags & CONN_EVENTS_MASK
) == flags)), (1))
)
268 return CONN_CORO_SUSPEND_ASYNC_AWAIT;
269
270 op = EPOLL_CTL_MOD3;
271 } else {
272 op = EPOLL_CTL_ADD1;
273 flags |= CONN_ASYNC_AWAIT;
274 coro_defer(conn->coro, clear_async_await_flag, await_fd_conn);
275 }
276
277 struct epoll_event event = {.events = conn_flags_to_epoll_events(flags),
278 .data.ptr = conn};
279 if (LIKELY(!epoll_ctl(epoll_fd, op, await_fd, &event))__builtin_expect((!!(!epoll_ctl(epoll_fd, op, await_fd, &
event))), (1))
) {
280 await_fd_conn->flags &= ~CONN_EVENTS_MASK;
281 await_fd_conn->flags |= flags;
282 return CONN_CORO_SUSPEND_ASYNC_AWAIT;
283 }
284
285 return CONN_CORO_ABORT;
286}
287
288static ALWAYS_INLINEinline __attribute__((always_inline)) void resume_coro(struct timeout_queue *tq,
289 struct lwan_connection *conn,
290 int epoll_fd)
291{
292 assert(conn->coro)((void) sizeof ((conn->coro) ? 1 : 0), __extension__ ({ if
(conn->coro) ; else __assert_fail ("conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 292, __extension__ __PRETTY_FUNCTION__); }))
;
293
294 int64_t from_coro = coro_resume(conn->coro);
295 enum lwan_connection_coro_yield yield_result = from_coro & 0xffffffff;
296
297 if (UNLIKELY(yield_result >= CONN_CORO_ASYNC)__builtin_expect(((yield_result >= CONN_CORO_ASYNC)), (0)))
298 yield_result = resume_async(tq, yield_result, from_coro, conn, epoll_fd);
299
300 if (UNLIKELY(yield_result == CONN_CORO_ABORT)__builtin_expect(((yield_result == CONN_CORO_ABORT)), (0)))
301 return timeout_queue_expire(tq, conn);
302
303 return update_epoll_flags(lwan_connection_get_fd(tq->lwan, conn), conn,
304 epoll_fd, yield_result);
305}
306
307static void update_date_cache(struct lwan_thread *thread)
308{
309 time_t now = time(NULL((void*)0));
310
311 lwan_format_rfc_time(now, thread->date.date);
312 lwan_format_rfc_time(now + (time_t)thread->lwan->config.expires,
313 thread->date.expires);
314}
315
316static ALWAYS_INLINEinline __attribute__((always_inline)) void spawn_coro(struct lwan_connection *conn,
317 struct coro_switcher *switcher,
318 struct timeout_queue *tq)
319{
320 struct lwan_thread *t = conn->thread;
321
322 assert(!conn->coro)((void) sizeof ((!conn->coro) ? 1 : 0), __extension__ ({ if
(!conn->coro) ; else __assert_fail ("!conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 322, __extension__ __PRETTY_FUNCTION__); }))
;
323 assert(t)((void) sizeof ((t) ? 1 : 0), __extension__ ({ if (t) ; else __assert_fail
("t", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 323, __extension__ __PRETTY_FUNCTION__); }))
;
324 assert((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads)((void) sizeof (((uintptr_t)t >= (uintptr_t)tq->lwan->
thread.threads) ? 1 : 0), __extension__ ({ if ((uintptr_t)t >=
(uintptr_t)tq->lwan->thread.threads) ; else __assert_fail
("(uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 324, __extension__ __PRETTY_FUNCTION__); }))
;
325 assert((uintptr_t)t <((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan->
thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__
({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread.
threads + tq->lwan->thread.count)) ; else __assert_fail
("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 326, __extension__ __PRETTY_FUNCTION__); }))
326 (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count))((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan->
thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__
({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread.
threads + tq->lwan->thread.count)) ; else __assert_fail
("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 326, __extension__ __PRETTY_FUNCTION__); }))
;
327
328 *conn = (struct lwan_connection) {
329 .coro = coro_new(switcher, process_request_coro, conn),
330 .flags = CONN_EVENTS_READ,
331 .time_to_expire = tq->current_time + tq->move_to_last_bump,
332 .thread = t,
333 };
334 if (UNLIKELY(!conn->coro)__builtin_expect(((!conn->coro)), (0))) {
335 conn->flags = 0;
336 lwan_status_error("Could not create coroutine")lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 336, __FUNCTION__, "Could not create coroutine")
;
337 return;
338 }
339
340 timeout_queue_insert(tq, conn);
341}
342
343static void accept_nudge(int pipe_fd,
344 struct lwan_thread *t,
345 struct lwan_connection *conns,
346 struct timeout_queue *tq,
347 struct coro_switcher *switcher,
348 int epoll_fd)
349{
350 uint64_t event;
351 int new_fd;
352
353 /* Errors are ignored here as pipe_fd serves just as a way to wake the
354 * thread from epoll_wait(). It's fine to consume the queue at this
355 * point, regardless of the error type. */
356 (void)read(pipe_fd, &event, sizeof(event));
357
358 while (spsc_queue_pop(&t->pending_fds, &new_fd)) {
359 struct lwan_connection *conn = &conns[new_fd];
360 struct epoll_event ev = {
361 .data.ptr = conn,
362 .events = conn_flags_to_epoll_events(CONN_EVENTS_READ),
363 };
364
365 if (LIKELY(!epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_fd, &ev))__builtin_expect((!!(!epoll_ctl(epoll_fd, 1, new_fd, &ev)
)), (1))
)
366 spawn_coro(conn, switcher, tq);
367 }
368
369 timeouts_add(t->wheel, &tq->timeout, 1000);
370}
371
372static bool_Bool process_pending_timers(struct timeout_queue *tq,
373 struct lwan_thread *t,
374 int epoll_fd)
375{
376 struct timeout *timeout;
377 bool_Bool should_expire_timers = false0;
378
379 while ((timeout = timeouts_get(t->wheel))) {
380 struct lwan_request *request;
381
382 if (timeout == &tq->timeout) {
383 should_expire_timers = true1;
384 continue;
385 }
386
387 request = container_of(timeout, struct lwan_request, timeout)((struct lwan_request *) ((char *)(timeout) - __builtin_offsetof
(struct lwan_request, timeout)) + ((typeof(*(timeout)) *)0 !=
(typeof(((struct lwan_request *)0)->timeout) *)0))
;
388
389 update_epoll_flags(request->fd, request->conn, epoll_fd,
390 CONN_CORO_RESUME);
391 }
392
393 if (should_expire_timers) {
394 timeout_queue_expire_waiting(tq);
395
396 /* tq timeout expires every 1000ms if there are connections, so
397 * update the date cache at this point as well. */
398 update_date_cache(t);
399
400 if (!timeout_queue_empty(tq)) {
401 timeouts_add(t->wheel, &tq->timeout, 1000);
402 return true1;
403 }
404
405 timeouts_del(t->wheel, &tq->timeout);
406 }
407
408 return false0;
409}
410
411static int
412turn_timer_wheel(struct timeout_queue *tq, struct lwan_thread *t, int epoll_fd)
413{
414 timeout_t wheel_timeout;
415 struct timespec now;
416
417 if (UNLIKELY(clock_gettime(monotonic_clock_id, &now) < 0)__builtin_expect(((clock_gettime(monotonic_clock_id, &now
) < 0)), (0))
)
418 lwan_status_critical("Could not get monotonic time")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 418, __FUNCTION__, "Could not get monotonic time")
;
419
420 timeouts_update(t->wheel,
421 (timeout_t)(now.tv_sec * 1000 + now.tv_nsec / 1000000));
422
423 wheel_timeout = timeouts_timeout(t->wheel);
424 if (UNLIKELY((int64_t)wheel_timeout < 0)__builtin_expect((((int64_t)wheel_timeout < 0)), (0)))
425 goto infinite_timeout;
426
427 if (wheel_timeout == 0) {
428 if (!process_pending_timers(tq, t, epoll_fd))
429 goto infinite_timeout;
430
431 wheel_timeout = timeouts_timeout(t->wheel);
432 if (wheel_timeout == 0)
433 goto infinite_timeout;
434 }
435
436 return (int)wheel_timeout;
437
438infinite_timeout:
439 return -1;
440}
441
442static void *thread_io_loop(void *data)
443{
444 struct lwan_thread *t = data;
445 int epoll_fd = t->epoll_fd;
446 const int read_pipe_fd = t->pipe_fd[0];
447 const int max_events = LWAN_MIN((int)t->lwan->thread.max_fd, 1024)({ const __typeof__(((int)t->lwan->thread.max_fd) + 0) lwan_tmp_id10
= ((int)t->lwan->thread.max_fd); const __typeof__((1024
) + 0) lwan_tmp_id11 = (1024); lwan_tmp_id10 > lwan_tmp_id11
? lwan_tmp_id11 : lwan_tmp_id10; })
;
448 struct lwan *lwan = t->lwan;
449 struct epoll_event *events;
450 struct coro_switcher switcher;
451 struct timeout_queue tq;
452
453 lwan_status_debug("Worker thread #%zd starting",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 454, __FUNCTION__, "Worker thread #%zd starting", t - t->
lwan->thread.threads + 1)
454 t - t->lwan->thread.threads + 1)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 454, __FUNCTION__, "Worker thread #%zd starting", t - t->
lwan->thread.threads + 1)
;
455 lwan_set_thread_name("worker");
456
457 events = calloc((size_t)max_events, sizeof(*events));
458 if (UNLIKELY(!events)__builtin_expect(((!events)), (0)))
459 lwan_status_critical("Could not allocate memory for events")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 459, __FUNCTION__, "Could not allocate memory for events")
;
460
461 update_date_cache(t);
462
463 timeout_queue_init(&tq, lwan);
464
465 pthread_barrier_wait(&lwan->thread.barrier);
466
467 for (;;) {
468 int timeout = turn_timer_wheel(&tq, t, epoll_fd);
469 int n_fds = epoll_wait(epoll_fd, events, max_events, timeout);
470
471 if (UNLIKELY(n_fds < 0)__builtin_expect(((n_fds < 0)), (0))) {
472 if (errno(*__errno_location ()) == EBADF9 || errno(*__errno_location ()) == EINVAL22)
473 break;
474 continue;
475 }
476
477 for (struct epoll_event *event = events; n_fds--; event++) {
478 struct lwan_connection *conn;
479
480 if (UNLIKELY(!event->data.ptr)__builtin_expect(((!event->data.ptr)), (0))) {
481 accept_nudge(read_pipe_fd, t, lwan->conns, &tq, &switcher,
482 epoll_fd);
483 continue;
484 }
485
486 conn = event->data.ptr;
487
488 if (UNLIKELY(event->events & (EPOLLRDHUP | EPOLLHUP))__builtin_expect(((event->events & (EPOLLRDHUP | EPOLLHUP
))), (0))
) {
489 timeout_queue_expire(&tq, conn);
490 continue;
491 }
492
493 resume_coro(&tq, conn, epoll_fd);
494 timeout_queue_move_to_last(&tq, conn);
495 }
496 }
497
498 pthread_barrier_wait(&lwan->thread.barrier);
499
500 timeout_queue_expire_all(&tq);
501 free(events);
502
503 return NULL((void*)0);
504}
505
506static void create_thread(struct lwan *l, struct lwan_thread *thread,
507 const size_t n_queue_fds)
508{
509 int ignore;
510 pthread_attr_t attr;
511
512 memset(thread, 0, sizeof(*thread));
513 thread->lwan = l;
514
515 thread->wheel = timeouts_open(&ignore);
516 if (!thread->wheel)
517 lwan_status_critical("Could not create timer wheel")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 517, __FUNCTION__, "Could not create timer wheel")
;
518
519 if ((thread->epoll_fd = epoll_create1(EPOLL_CLOEXECEPOLL_CLOEXEC)) < 0)
520 lwan_status_critical_perror("epoll_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 520, __FUNCTION__, "epoll_create")
;
521
522 if (pthread_attr_init(&attr))
523 lwan_status_critical_perror("pthread_attr_init")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 523, __FUNCTION__, "pthread_attr_init")
;
524
525 if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEMPTHREAD_SCOPE_SYSTEM))
526 lwan_status_critical_perror("pthread_attr_setscope")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 526, __FUNCTION__, "pthread_attr_setscope")
;
527
528 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLEPTHREAD_CREATE_JOINABLE))
529 lwan_status_critical_perror("pthread_attr_setdetachstate")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 529, __FUNCTION__, "pthread_attr_setdetachstate")
;
530
531#if defined(HAVE_EVENTFD)
532 int efd = eventfd(0, EFD_NONBLOCKEFD_NONBLOCK | EFD_SEMAPHOREEFD_SEMAPHORE | EFD_CLOEXECEFD_CLOEXEC);
533 if (efd < 0)
534 lwan_status_critical_perror("eventfd")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 534, __FUNCTION__, "eventfd")
;
535
536 thread->pipe_fd[0] = thread->pipe_fd[1] = efd;
537#else
538 if (pipe2(thread->pipe_fd, O_NONBLOCK04000 | O_CLOEXEC02000000) < 0)
539 lwan_status_critical_perror("pipe")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 539, __FUNCTION__, "pipe")
;
540#endif
541
542 struct epoll_event event = { .events = EPOLLINEPOLLIN, .data.ptr = NULL((void*)0) };
543 if (epoll_ctl(thread->epoll_fd, EPOLL_CTL_ADD1, thread->pipe_fd[0], &event) < 0)
544 lwan_status_critical_perror("epoll_ctl")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 544, __FUNCTION__, "epoll_ctl")
;
545
546 if (pthread_create(&thread->self, &attr, thread_io_loop, thread))
547 lwan_status_critical_perror("pthread_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 547, __FUNCTION__, "pthread_create")
;
548
549 if (pthread_attr_destroy(&attr))
550 lwan_status_critical_perror("pthread_attr_destroy")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 550, __FUNCTION__, "pthread_attr_destroy")
;
551
552 if (spsc_queue_init(&thread->pending_fds, n_queue_fds) < 0) {
553 lwan_status_critical("Could not initialize pending fd "lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 554, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements"
, n_queue_fds)
554 "queue width %zu elements", n_queue_fds)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 554, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements"
, n_queue_fds)
;
555 }
556}
557
558void lwan_thread_nudge(struct lwan_thread *t)
559{
560 uint64_t event = 1;
561
562 if (UNLIKELY(write(t->pipe_fd[1], &event, sizeof(event)) < 0)__builtin_expect(((write(t->pipe_fd[1], &event, sizeof
(event)) < 0)), (0))
)
563 lwan_status_perror("write")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 563, __FUNCTION__, "write")
;
564}
565
566void lwan_thread_add_client(struct lwan_thread *t, int fd)
567{
568 for (int i = 0; i < 10; i++) {
569 bool_Bool pushed = spsc_queue_push(&t->pending_fds, fd);
570
571 if (LIKELY(pushed)__builtin_expect((!!(pushed)), (1)))
572 return;
573
574 /* Queue is full; nudge the thread to consume it. */
575 lwan_thread_nudge(t);
576 }
577
578 lwan_status_error("Dropping connection %d", fd)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 578, __FUNCTION__, "Dropping connection %d", fd)
;
579 /* FIXME: send "busy" response now, even without receiving request? */
580 close(fd);
581}
582
583#if defined(__linux__1) && defined(__x86_64__1)
584static bool_Bool read_cpu_topology(struct lwan *l, uint32_t siblings[])
585{
586 char path[PATH_MAX4096];
587
588 for (uint32_t i = 0; i < l->available_cpus; i++)
589 siblings[i] = 0xbebacafe;
590
591 for (unsigned int i = 0; i < l->available_cpus; i++) {
592 FILE *sib;
593 uint32_t id, sibling;
594 char separator;
595
596 snprintf(path, sizeof(path),
597 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
598 i);
599
600 sib = fopen(path, "re");
601 if (!sib) {
602 lwan_status_warning("Could not open `%s` to determine CPU topology",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 603, __FUNCTION__, "Could not open `%s` to determine CPU topology"
, path)
603 path)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 603, __FUNCTION__, "Could not open `%s` to determine CPU topology"
, path)
;
604 return false0;
605 }
606
607 switch (fscanf(sib, "%u%c%u", &id, &separator, &sibling)) {
608 case 2: /* No SMT */
609 siblings[i] = id;
610 break;
611 case 3: /* SMT */
612 if (!(separator == ',' || separator == '-')) {
613 lwan_status_critical("Expecting either ',' or '-' for sibling separator")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 613, __FUNCTION__, "Expecting either ',' or '-' for sibling separator"
)
;
614 __builtin_unreachable();
615 }
616
617 siblings[i] = sibling;
618 break;
619 default:
620 lwan_status_critical("%s has invalid format", path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 620, __FUNCTION__, "%s has invalid format", path)
;
621 __builtin_unreachable();
622 }
623
624 fclose(sib);
625 }
626
627 /* Perform a sanity check here, as some systems seem to filter out the
628 * result of sysconf() to obtain the number of configured and online
629 * CPUs but don't bother changing what's available through sysfs as far
630 * as the CPU topology information goes. It's better to fall back to a
631 * possibly non-optimal setup than just crash during startup while
632 * trying to perform an out-of-bounds array access. */
633 for (unsigned int i = 0; i < l->available_cpus; i++) {
634 if (siblings[i] == 0xbebacafe) {
635 lwan_status_warning("Could not determine sibling for CPU %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 635, __FUNCTION__, "Could not determine sibling for CPU %d"
, i)
;
636 return false0;
637 }
638
639 if (siblings[i] >= l->available_cpus) {
640 lwan_status_warning("CPU information topology says CPU %d exists, "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 643, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
641 "but max available CPUs is %d (online CPUs: %d). "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 643, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
642 "Is Lwan running in a (broken) container?",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 643, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
643 siblings[i], l->available_cpus, l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 643, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
;
644 return false0;
645 }
646 }
647
648 return true1;
649}
650
651static void
652siblings_to_schedtbl(struct lwan *l, uint32_t siblings[], uint32_t schedtbl[])
653{
654 int *seen = alloca(l->available_cpus * sizeof(int))__builtin_alloca (l->available_cpus * sizeof(int));
655 unsigned int n_schedtbl = 0;
656
657 for (uint32_t i = 0; i < l->available_cpus; i++)
658 seen[i] = -1;
659
660 for (uint32_t i = 0; i < l->available_cpus; i++) {
661 if (seen[siblings[i]] < 0) {
662 seen[siblings[i]] = (int)i;
663 } else {
664 schedtbl[n_schedtbl++] = (uint32_t)seen[siblings[i]];
665 schedtbl[n_schedtbl++] = i;
666 }
667 }
668
669 if (n_schedtbl != l->available_cpus)
670 memcpy(schedtbl, seen, l->available_cpus * sizeof(int));
671}
672
673static bool_Bool
674topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads)
675{
676 uint32_t *siblings = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t));
677
678 if (read_cpu_topology(l, siblings)) {
11
Assuming the condition is false
12
Taking false branch
679 uint32_t *affinity = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t));
680
681 siblings_to_schedtbl(l, siblings, affinity);
682
683 for (uint32_t i = 0; i < n_threads; i++)
684 schedtbl[i] = affinity[i % l->available_cpus];
685 return true1;
686 }
687
688 for (uint32_t i = 0; i < n_threads; i++)
13
Assuming 'i' is >= 'n_threads'
14
Loop condition is false. Execution continues on line 690
689 schedtbl[i] = (i / 2) % l->thread.count;
690 return false0;
691}
692
693static void
694adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t mask)
695{
696 for (uint32_t i = 0; i < l->thread.count; i++) {
697 cpu_set_t set;
698
699 CPU_ZERO(&set)do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while
(0)
;
700 CPU_SET(schedtbl[i & mask], &set)(__extension__ ({ size_t __cpu = (schedtbl[i & mask]); __cpu
/ 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)
->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask
) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }))
;
701
702 if (pthread_setaffinity_np(l->thread.threads[i].self, sizeof(set),
703 &set))
704 lwan_status_warning("Could not set affinity for thread %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 704, __FUNCTION__, "Could not set affinity for thread %d", i
)
;
705 }
706}
707#elif defined(__x86_64__1)
708static bool_Bool
709topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads)
710{
711 for (uint32_t i = 0; i < n_threads; i++)
712 schedtbl[i] = (i / 2) % l->thread.count;
713 return false0;
714}
715
716static void
717adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t n)
718{
719}
720#endif
721
722void lwan_thread_init(struct lwan *l)
723{
724 if (pthread_barrier_init(&l->thread.barrier, NULL((void*)0),
1
Assuming the condition is false
2
Taking false branch
725 (unsigned)l->thread.count + 1))
726 lwan_status_critical("Could not create barrier")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 726, __FUNCTION__, "Could not create barrier")
;
727
728 lwan_status_debug("Initializing threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 728, __FUNCTION__, "Initializing threads")
;
729
730 l->thread.threads =
731 calloc((size_t)l->thread.count, sizeof(struct lwan_thread));
732 if (!l->thread.threads)
3
Assuming field 'threads' is non-null
4
Taking false branch
733 lwan_status_critical("Could not allocate memory for threads")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 733, __FUNCTION__, "Could not allocate memory for threads")
;
734
735 const size_t n_queue_fds = LWAN_MIN(l->thread.max_fd / l->thread.count,({ const __typeof__((l->thread.max_fd / l->thread.count
) + 0) lwan_tmp_id12 = (l->thread.max_fd / l->thread.count
); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size
())) + 0) lwan_tmp_id13 = ((size_t)(2 * lwan_socket_get_backlog_size
())); lwan_tmp_id12 > lwan_tmp_id13 ? lwan_tmp_id13 : lwan_tmp_id12
; })
5
Assuming 'lwan_tmp_id4' is <= 'lwan_tmp_id5'
6
'?' condition is false
736 (size_t)(2 * lwan_socket_get_backlog_size()))({ const __typeof__((l->thread.max_fd / l->thread.count
) + 0) lwan_tmp_id12 = (l->thread.max_fd / l->thread.count
); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size
())) + 0) lwan_tmp_id13 = ((size_t)(2 * lwan_socket_get_backlog_size
())); lwan_tmp_id12 > lwan_tmp_id13 ? lwan_tmp_id13 : lwan_tmp_id12
; })
;
737 lwan_status_debug("Pending client file descriptor queue has %zu items", n_queue_fds)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 737, __FUNCTION__, "Pending client file descriptor queue has %zu items"
, n_queue_fds)
;
738 for (unsigned int i = 0; i
6.1
'i' is < field 'count'
< l->thread.count
; i++)
7
Loop condition is true. Entering loop body
8
Assuming 'i' is >= field 'count'
9
Loop condition is false. Execution continues on line 741
739 create_thread(l, &l->thread.threads[i], n_queue_fds);
740
741 const unsigned int total_conns = l->thread.max_fd * l->thread.count;
742#ifdef __x86_64__1
743 static_assert(sizeof(struct lwan_connection) == 32,extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(struct lwan_connection) ==
32) ? 2 : -1; })]
744 "Two connections per cache line")extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(struct lwan_connection) ==
32) ? 2 : -1; })]
;
745
746 lwan_status_debug("%d CPUs of %d are online. "lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 748, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
747 "Reading topology to pre-schedule clients",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 748, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
748 l->online_cpus, l->available_cpus)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 748, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
;
749
750 /*
751 * Pre-schedule each file descriptor, to reduce some operations in the
752 * fast path.
753 *
754 * Since struct lwan_connection is guaranteed to be 32-byte long, two of
755 * them can fill up a cache line. Assume siblings share cache lines and
756 * use the CPU topology to group two connections per cache line in such
757 * a way that false sharing is avoided.
758 */
759 uint32_t n_threads = (uint32_t)lwan_nextpow2((size_t)((l->thread.count - 1) * 2));
760 uint32_t *schedtbl = alloca(n_threads * sizeof(uint32_t))__builtin_alloca (n_threads * sizeof(uint32_t));
761
762 bool_Bool adj_affinity = topology_to_schedtbl(l, schedtbl, n_threads);
10
Calling 'topology_to_schedtbl'
15
Returning from 'topology_to_schedtbl'
763
764 n_threads--; /* Transform count into mask for AND below */
765
766 if (adj_affinity
15.1
'adj_affinity' is false
)
16
Taking false branch
767 adjust_threads_affinity(l, schedtbl, n_threads);
768
769 for (unsigned int i = 0; i < total_conns; i++)
17
Assuming 'i' is < 'total_conns'
18
Loop condition is true. Entering loop body
770 l->conns[i].thread = &l->thread.threads[schedtbl[i & n_threads]];
19
Array subscript is undefined
771#else
772 for (unsigned int i = 0; i < total_conns; i++)
773 l->conns[i].thread = &l->thread.threads[i % l->thread.count];
774#endif
775
776 pthread_barrier_wait(&l->thread.barrier);
777
778 lwan_status_debug("Worker threads created and ready to serve")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 778, __FUNCTION__, "Worker threads created and ready to serve"
)
;
779}
780
781void lwan_thread_shutdown(struct lwan *l)
782{
783 lwan_status_debug("Shutting down threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 783, __FUNCTION__, "Shutting down threads")
;
784
785 for (unsigned int i = 0; i < l->thread.count; i++) {
786 struct lwan_thread *t = &l->thread.threads[i];
787
788 close(t->epoll_fd);
789 lwan_thread_nudge(t);
790 }
791
792 pthread_barrier_wait(&l->thread.barrier);
793 pthread_barrier_destroy(&l->thread.barrier);
794
795 for (unsigned int i = 0; i < l->thread.count; i++) {
796 struct lwan_thread *t = &l->thread.threads[i];
797
798 close(t->pipe_fd[0]);
799#if !defined(HAVE_EVENTFD)
800 close(t->pipe_fd[1]);
801#endif
802
803 pthread_join(l->thread.threads[i].self, NULL((void*)0));
804 spsc_queue_free(&t->pending_fds);
805 timeouts_close(t->wheel);
806 }
807
808 free(l->thread.threads);
809}