Bug Summary

File:pr/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/pr/tests/../../../pr/tests/servr_uk.c
Warning:line 404, column 9
Value stored to 'rv' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name servr_uk.c -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nspr/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/pr/tests -fcoverage-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nspr/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/pr/tests -resource-dir /usr/lib/llvm-18/lib/clang/18 -U NDEBUG -D DEBUG_jenkins -D PACKAGE_NAME="" -D PACKAGE_TARNAME="" -D PACKAGE_VERSION="" -D PACKAGE_STRING="" -D PACKAGE_BUGREPORT="" -D PACKAGE_URL="" -D DEBUG=1 -D HAVE_VISIBILITY_HIDDEN_ATTRIBUTE=1 -D HAVE_VISIBILITY_PRAGMA=1 -D XP_UNIX=1 -D _GNU_SOURCE=1 -D HAVE_FCNTL_FILE_LOCKING=1 -D HAVE_POINTER_LOCALTIME_R=1 -D LINUX=1 -D HAVE_DLADDR=1 -D HAVE_GETTID=1 -D HAVE_LCHOWN=1 -D HAVE_SETPRIORITY=1 -D HAVE_STRERROR=1 -D HAVE_SYSCALL=1 -D HAVE_SECURE_GETENV=1 -D _REENTRANT=1 -D FORCE_PR_LOG -D _PR_PTHREADS -U HAVE_CVAR_BUILT_ON_SEM -I /var/lib/jenkins/workspace/nss-scan-build/nss/../dist/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/include -I ../../../pr/include -I ../../../pr/include/private -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -fno-inline -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-05-18-082241-28900-1 -x c ../../../pr/tests/servr_uk.c
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6/***********************************************************************
7**
8** This server simulates a server running in loopback mode.
9**
10** The idea is that a single server is created. The server initially creates
11** a number of worker threads. Then, with the server running, a number of
12** clients are created which start requesting service from the server.
13**
14**
15** Modification History:
16** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
17** The debug mode will print all of the printfs associated with this test.
18** The regress mode will be the default mode. Since the regress tool limits
19** the output to a one line status:PASS or FAIL,all of the printf statements
20** have been handled with an if (debug_mode) statement.
21** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
22** recognize the return code from tha main program.
23***********************************************************************/
24
25/***********************************************************************
26** Includes
27***********************************************************************/
28/* Used to get the command line option */
29#include "plgetopt.h"
30
31#include "nspr.h"
32#include "pprthred.h"
33
34#include <string.h>
35
36#define PORT15004 15004
37#define THREAD_STACKSIZE0 0
38
39static int _iterations = 1000;
40static int _clients = 1;
41static int _client_data = 250;
42static int _server_data = (8*1024);
43
44static PRThreadScope ServerScope, ClientScope;
45
46#define SERVER"Server" "Server"
47#define MAIN"Main" "Main"
48
49#define SERVER_STATE_STARTUP0 0
50#define SERVER_STATE_READY1 1
51#define SERVER_STATE_DYING2 2
52#define SERVER_STATE_DEAD4 4
53int ServerState;
54PRLock *ServerStateCVLock;
55PRCondVar *ServerStateCV;
56
57#ifdef DEBUGPRINTS
58#define DPRINTF printf
59#else
60#define DPRINTF
61#endif
62
63PRIntn failed_already=0;
64PRIntn debug_mode;
65
66
67
68static void do_work(void);
69
70/* --- Server state functions --------------------------------------------- */
71void
72SetServerState(char *waiter, PRInt32 state)
73{
74 PR_Lock(ServerStateCVLock);
75 ServerState = state;
76 PR_NotifyCondVar(ServerStateCV);
77
78 if (debug_mode) {
79 DPRINTF("\t%s changed state to %d\n", waiter, state);
80 }
81
82 PR_Unlock(ServerStateCVLock);
83}
84
85int
86WaitServerState(char *waiter, PRInt32 state)
87{
88 PRInt32 rv;
89
90 PR_Lock(ServerStateCVLock);
91
92 if (debug_mode) {
93 DPRINTF("\t%s waiting for state %d\n", waiter, state);
94 }
95
96 while(!(ServerState & state)) {
97 PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
98 }
99 rv = ServerState;
100
101 if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
102 waiter, state, ServerState);
103 PR_Unlock(ServerStateCVLock);
104
105 return rv;
106}
107
108/* --- Server Functions ------------------------------------------- */
109
110PRLock *workerThreadsLock;
111PRInt32 workerThreads;
112PRInt32 workerThreadsBusy;
113
114void
115WorkerThreadFunc(void *_listenSock)
116{
117 PRFileDesc *listenSock = (PRFileDesc *)_listenSock;
118 PRInt32 bytesRead;
119 PRInt32 bytesWritten;
120 char *dataBuf;
121 char *sendBuf;
122
123 if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
124 _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32);
125 dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32)(PR_Malloc((_client_data + 2*sizeof(PRNetAddr) + 32)));
126 if (!dataBuf)
127 if (debug_mode) {
128 printf("\tServer could not malloc space!?\n");
129 }
130 sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char))(PR_Malloc((_server_data *sizeof(char))));
131 if (!sendBuf)
132 if (debug_mode) {
133 printf("\tServer could not malloc space!?\n");
134 }
135
136 if (debug_mode) {
137 DPRINTF("\tServer worker thread running\n");
138 }
139
140 while(1) {
141 PRInt32 bytesToRead = _client_data;
142 PRInt32 bytesToWrite = _server_data;
143 PRFileDesc *newSock;
144 PRNetAddr *rAddr;
145 PRInt32 loops = 0;
146
147 loops++;
148
149 if (debug_mode) {
150 DPRINTF("\tServer thread going into accept\n");
151 }
152
153 bytesRead = PR_AcceptRead(listenSock,
154 &newSock,
155 &rAddr,
156 dataBuf,
157 bytesToRead,
158 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
159
160 if (bytesRead < 0) {
161 if (debug_mode) {
162 printf("\tServer error in accept (%d)\n", bytesRead);
163 }
164 continue;
165 }
166
167 if (debug_mode) {
168 DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead);
169 }
170
171 PR_AtomicIncrement(&workerThreadsBusy);
172 if (workerThreadsBusy == workerThreads) {
173 PR_Lock(workerThreadsLock);
174 if (workerThreadsBusy == workerThreads) {
175 PRThread *WorkerThread;
176
177 WorkerThread = PR_CreateThread(
178 PR_SYSTEM_THREAD,
179 WorkerThreadFunc,
180 listenSock,
181 PR_PRIORITY_NORMAL,
182 ServerScope,
183 PR_UNJOINABLE_THREAD,
184 THREAD_STACKSIZE0);
185
186 if (!WorkerThread) {
187 if (debug_mode) {
188 printf("Error creating client thread %d\n", workerThreads);
189 }
190 } else {
191 PR_AtomicIncrement(&workerThreads);
192 if (debug_mode) {
193 DPRINTF("\tServer creates worker (%d)\n", workerThreads);
194 }
195 }
196 }
197 PR_Unlock(workerThreadsLock);
198 }
199
200 bytesToRead -= bytesRead;
201 while (bytesToRead) {
202 bytesRead = PR_Recv(newSock,
203 dataBuf,
204 bytesToRead,
205 0,
206 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
207 if (bytesRead < 0) {
208 if (debug_mode) {
209 printf("\tServer error receiving data (%d)\n", bytesRead);
210 }
211 continue;
212 }
213 if (debug_mode) {
214 DPRINTF("\tServer received %d bytes\n", bytesRead);
215 }
216 }
217
218 bytesWritten = PR_Send(newSock,
219 sendBuf,
220 bytesToWrite,
221 0,
222 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
223 if (bytesWritten != _server_data) {
224 if (debug_mode) printf("\tError sending data to client (%d, %d)\n",
225 bytesWritten, PR_GetOSError());
226 } else {
227 if (debug_mode) {
228 DPRINTF("\tServer sent %d bytes\n", bytesWritten);
229 }
230 }
231
232 PR_Close(newSock);
233 PR_AtomicDecrement(&workerThreadsBusy);
234 }
235}
236
237PRFileDesc *
238ServerSetup(void)
239{
240 PRFileDesc *listenSocket;
241 PRSocketOptionData sockOpt;
242 PRNetAddr serverAddr;
243 PRThread *WorkerThread;
244
245 if ( (listenSocket = PR_NewTCPSocket()) == NULL((void*)0)) {
246 if (debug_mode) {
247 printf("\tServer error creating listen socket\n");
248 }
249 else {
250 return NULL((void*)0);
251 }
252 }
253
254 sockOpt.option = PR_SockOpt_Reuseaddr;
255 sockOpt.value.reuse_addr = PR_TRUE1;
256 if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) {
257 if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
258 PR_GetOSError());
259 else {
260 failed_already=1;
261 }
262 PR_Close(listenSocket);
263 return NULL((void*)0);
264 }
265
266 memset(&serverAddr, 0, sizeof(PRNetAddr));
267 serverAddr.inet.family = PR_AF_INET2;
268 serverAddr.inet.port = PR_htons(PORT15004);
269 serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY((in_addr_t) 0x00000000));
270
271 if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
272 if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
273 PR_GetOSError());
274 else {
275 failed_already=1;
276 }
277 PR_Close(listenSocket);
278 return NULL((void*)0);
279 }
280
281 if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
282 if (debug_mode) {
283 printf("\tServer error listening to server socket\n");
284 }
285 else {
286 failed_already=1;
287 }
288 PR_Close(listenSocket);
289
290 return NULL((void*)0);
291 }
292
293 /* Create Clients */
294 workerThreads = 0;
295 workerThreadsBusy = 0;
296
297 workerThreadsLock = PR_NewLock();
298
299 WorkerThread = PR_CreateThread(
300 PR_SYSTEM_THREAD,
301 WorkerThreadFunc,
302 listenSocket,
303 PR_PRIORITY_NORMAL,
304 ServerScope,
305 PR_UNJOINABLE_THREAD,
306 THREAD_STACKSIZE0);
307
308 if (!WorkerThread) {
309 if (debug_mode) {
310 printf("error creating working thread\n");
311 }
312 PR_Close(listenSocket);
313 return NULL((void*)0);
314 }
315 PR_AtomicIncrement(&workerThreads);
316 if (debug_mode) {
317 DPRINTF("\tServer created primordial worker thread\n");
318 }
319
320 return listenSocket;
321}
322
323/* The main server loop */
324void
325ServerThreadFunc(void *unused)
326{
327 PRFileDesc *listenSocket;
328
329 /* Do setup */
330 listenSocket = ServerSetup();
331
332 if (!listenSocket) {
333 SetServerState(SERVER"Server", SERVER_STATE_DEAD4);
334 } else {
335
336 if (debug_mode) {
337 DPRINTF("\tServer up\n");
338 }
339
340 /* Tell clients they can start now. */
341 SetServerState(SERVER"Server", SERVER_STATE_READY1);
342
343 /* Now wait for server death signal */
344 WaitServerState(SERVER"Server", SERVER_STATE_DYING2);
345
346 /* Cleanup */
347 SetServerState(SERVER"Server", SERVER_STATE_DEAD4);
348 }
349}
350
351/* --- Client Functions ------------------------------------------- */
352
353PRInt32 numRequests;
354PRInt32 numClients;
355PRMonitor *clientMonitor;
356
357void
358ClientThreadFunc(void *unused)
359{
360 PRNetAddr serverAddr;
361 PRFileDesc *clientSocket;
362 char *sendBuf;
363 char *recvBuf;
364 PRInt32 rv;
365 PRInt32 bytesNeeded;
366
367 sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char))(PR_Malloc((_client_data * sizeof(char))));
368 if (!sendBuf)
369 if (debug_mode) {
370 printf("\tClient could not malloc space!?\n");
371 }
372 recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char))(PR_Malloc((_server_data * sizeof(char))));
373 if (!recvBuf)
374 if (debug_mode) {
375 printf("\tClient could not malloc space!?\n");
376 }
377
378 memset(&serverAddr, 0, sizeof(PRNetAddr));
379 serverAddr.inet.family = PR_AF_INET2;
380 serverAddr.inet.port = PR_htons(PORT15004);
381 serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK((in_addr_t) 0x7f000001));
382
383 while(numRequests > 0) {
384
385 if ( (numRequests % 10) == 0 )
386 if (debug_mode) {
387 printf(".");
388 }
389 if (debug_mode) {
390 DPRINTF("\tClient starting request %d\n", numRequests);
391 }
392
393 clientSocket = PR_NewTCPSocket();
394 if (!clientSocket) {
395 if (debug_mode) printf("Client error creating socket: OS error %d\n",
396 PR_GetOSError());
397 continue;
398 }
399
400 if (debug_mode) {
401 DPRINTF("\tClient connecting\n");
402 }
403
404 rv = PR_Connect(clientSocket,
Value stored to 'rv' is never read
405 &serverAddr,
406 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
407 if (!clientSocket) {
408 if (debug_mode) {
409 printf("\tClient error connecting\n");
410 }
411 continue;
412 }
413
414 if (debug_mode) {
415 DPRINTF("\tClient connected\n");
416 }
417
418 rv = PR_Send(clientSocket,
419 sendBuf,
420 _client_data,
421 0,
422 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
423 if (rv != _client_data) {
424 if (debug_mode) {
425 printf("Client error sending data (%d)\n", rv);
426 }
427 PR_Close(clientSocket);
428 continue;
429 }
430
431 if (debug_mode) {
432 DPRINTF("\tClient sent %d bytes\n", rv);
433 }
434
435 bytesNeeded = _server_data;
436 while(bytesNeeded) {
437 rv = PR_Recv(clientSocket,
438 recvBuf,
439 bytesNeeded,
440 0,
441 PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
442 if (rv <= 0) {
443 if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
444 rv, (_server_data - bytesNeeded), _server_data);
445 break;
446 }
447 if (debug_mode) {
448 DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
449 }
450 bytesNeeded -= rv;
451 }
452
453 PR_Close(clientSocket);
454
455 PR_AtomicDecrement(&numRequests);
456 }
457
458 PR_EnterMonitor(clientMonitor);
459 --numClients;
460 PR_Notify(clientMonitor);
461 PR_ExitMonitor(clientMonitor);
462
463 PR_DELETE(sendBuf){ PR_Free(sendBuf); (sendBuf) = ((void*)0); };
464 PR_DELETE(recvBuf){ PR_Free(recvBuf); (recvBuf) = ((void*)0); };
465}
466
467void
468RunClients(void)
469{
470 PRInt32 index;
471
472 numRequests = _iterations;
473 numClients = _clients;
474 clientMonitor = PR_NewMonitor();
475
476 for (index=0; index<_clients; index++) {
477 PRThread *clientThread;
478
479
480 clientThread = PR_CreateThread(
481 PR_USER_THREAD,
482 ClientThreadFunc,
483 NULL((void*)0),
484 PR_PRIORITY_NORMAL,
485 ClientScope,
486 PR_UNJOINABLE_THREAD,
487 THREAD_STACKSIZE0);
488
489 if (!clientThread) {
490 if (debug_mode) {
491 printf("\terror creating client thread %d\n", index);
492 }
493 } else if (debug_mode) {
494 DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
495 }
496
497 }
498
499 PR_EnterMonitor(clientMonitor);
500 while(numClients) {
501 PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT0xffffffffUL);
502 }
503 PR_ExitMonitor(clientMonitor);
504}
505
506/* --- Main Function ---------------------------------------------- */
507
508static
509void do_work()
510{
511 PRThread *ServerThread;
512 PRInt32 state;
513
514 SetServerState(MAIN"Main", SERVER_STATE_STARTUP0);
515 ServerThread = PR_CreateThread(
516 PR_USER_THREAD,
517 ServerThreadFunc,
518 NULL((void*)0),
519 PR_PRIORITY_NORMAL,
520 ServerScope,
521 PR_JOINABLE_THREAD,
522 THREAD_STACKSIZE0);
523 if (!ServerThread) {
524 if (debug_mode) {
525 printf("error creating main server thread\n");
526 }
527 return;
528 }
529
530 /* Wait for server to be ready */
531 state = WaitServerState(MAIN"Main", SERVER_STATE_READY1|SERVER_STATE_DEAD4);
532
533 if (!(state & SERVER_STATE_DEAD4)) {
534 /* Run Test Clients */
535 RunClients();
536
537 /* Send death signal to server */
538 SetServerState(MAIN"Main", SERVER_STATE_DYING2);
539 }
540
541 PR_JoinThread(ServerThread);
542}
543
544
545static void do_workUK(void)
546{
547 ServerScope = PR_LOCAL_THREAD;
548 ClientScope = PR_GLOBAL_THREAD;
549 do_work();
550}
551
552
553
554static void Measure(void (*func)(void), const char *msg)
555{
556 PRIntervalTime start, stop;
557 double d;
558
559 start = PR_IntervalNow();
560 (*func)();
561 stop = PR_IntervalNow();
562
563 d = (double)PR_IntervalToMicroseconds(stop - start);
564
565 if (debug_mode) {
566 printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
567 }
568}
569
570
571int main(int argc, char **argv)
572{
573 /* The command line argument: -d is used to determine if the test is being run
574 in debug mode. The regress tool requires only one line output:PASS or FAIL.
575 All of the printfs associated with this test has been handled with a if (debug_mode)
576 test.
577 Usage: test_name -d
578 */
579 PLOptStatus os;
580 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
581 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
582 {
583 if (PL_OPT_BAD == os) {
584 continue;
585 }
586 switch (opt->option)
587 {
588 case 'd': /* debug mode */
589 debug_mode = 1;
590 break;
591 default:
592 break;
593 }
594 }
595 PL_DestroyOptState(opt);
596
597 /* main test */
598 if (debug_mode) {
599 printf("Enter number of iterations: \n");
600 scanf("%d", &_iterations);
601 printf("Enter number of clients : \n");
602 scanf("%d", &_clients);
603 printf("Enter size of client data : \n");
604 scanf("%d", &_client_data);
605 printf("Enter size of server data : \n");
606 scanf("%d", &_server_data);
607 }
608 else
609 {
610 _iterations = 7;
611 _clients = 7;
612 _client_data = 100;
613 _server_data = 100;
614 }
615
616 if (debug_mode) {
617 printf("\n\n%d iterations with %d client threads.\n",
618 _iterations, _clients);
619 printf("Sending %d bytes of client data and %d bytes of server data\n",
620 _client_data, _server_data);
621 }
622 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
623 PR_STDIO_INIT();
624
625 PR_SetThreadRecycleMode(64);
626
627 ServerStateCVLock = PR_NewLock();
628 ServerStateCV = PR_NewCondVar(ServerStateCVLock);
629
630 Measure(do_workUK, "server loop user/kernel");
631
632 PR_Cleanup();
633
634 if(failed_already) {
635 return 1;
636 }
637 else {
638 return 0;
639 }
640}