Skip to contentSkip to conversation

selftests/net: fix a few small compiler warnings

PATCH Applied Willem de Bruijn 4 participants 10 messages Nov 24, 2023 – Nov 28, 2023

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover selftests/net: fix a few small compiler warnings

From: Willem de Bruijn <[email protected]>

Observed a clang warning when backporting cmsg_sender. Ran the same build against all the .c files under selftests/net.

This is clang-14 with -Wall
Which is what tools/testing/selftests/net/Makefile also enables.

Willem de Bruijn (4):
selftests/net: ipsec: fix constant out of range selftests/net: fix a char signedness issue selftests/net: unix: fix unused variable compiler warning selftests/net: mptcp: fix uninitialized variable warnings

 tools/testing/selftests/net/af_unix/diag_uid.c    |  1 -
 tools/testing/selftests/net/cmsg_sender.c         |  2 +-
 tools/testing/selftests/net/ipsec.c               |  4 ++--
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 11 ++++-------
 tools/testing/selftests/net/mptcp/mptcp_inq.c     | 11 ++++-------
 5 files changed, 11 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#1 lore raw

PATCH 1/4 selftests/net: ipsec: fix constant out of range

From: Willem de Bruijn <[email protected]>

Fix a small compiler warning.

nr_process must be a signed long: it is assigned a signed long by strtol() and is compared against LONG_MIN and LONG_MAX.

ipsec.c:2280:65:
error: result of comparison of constant -9223372036854775808 with expression of type 'unsigned int' is always false [-Werror,-Wtautological-constant-out-of-range-compare]

  if ((errno == ERANGE && (nr_process == LONG_MAX || nr_process == LONG_MIN))
Fixes bc2652b7ae1e ("selftest/net/xfrm: Add test for ipsec tunnel")
Cc Dmitry Safonov <[email protected]>
---
 tools/testing/selftests/net/ipsec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/net/ipsec.c +2 -2
index 9a8229abfa026..be4a30a0d02ae 100644
@@ -2263,7 +2263,7 @@ static int check_results(void)
 
 int main(int argc, char **argv)
 {
-	unsigned int nr_process = 1;
+	long nr_process = 1;
 	int route_sock = -1, ret = KSFT_SKIP;
 	int test_desc_fd[2];
 	uint32_t route_seq;
@@ -2284,7 +2284,7 @@ int main(int argc, char **argv)
 			exit_usage(argv);
 		}
 
-		if (nr_process > MAX_PROCESSES || !nr_process) {
+		if (nr_process > MAX_PROCESSES || nr_process < 1) {
 			printk("nr_process should be between [1; %u]",
 					MAX_PROCESSES);
 			exit_usage(argv);
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH net 1/4] selftests/net: ipsec: fix constant out of range

Quoted 12 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Fix a small compiler warning.

nr_process must be a signed long: it is assigned a signed long by strtol() and is compared against LONG_MIN and LONG_MAX.

ipsec.c:2280:65:
error: result of comparison of constant -9223372036854775808 with expression of type 'unsigned int' is always false [-Werror,-Wtautological-constant-out-of-range-compare]

Quoted 5 lines
  if ((errno == ERANGE && (nr_process == LONG_MAX || nr_process == LONG_MIN))
Fixes bc2652b7ae1e ("selftest/net/xfrm: Add test for ipsec tunnel")
Cc Dmitry Safonov <[email protected]>

LGTM, thanks!

Quoted 26 lines
---
 tools/testing/selftests/net/ipsec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/net/ipsec.c +2 -2
index 9a8229abfa026..be4a30a0d02ae 100644
@@ -2263,7 +2263,7 @@ static int check_results(void)
 
 int main(int argc, char **argv)
 {
-	unsigned int nr_process = 1;
+	long nr_process = 1;
 	int route_sock = -1, ret = KSFT_SKIP;
 	int test_desc_fd[2];
 	uint32_t route_seq;
@@ -2284,7 +2284,7 @@ int main(int argc, char **argv)
 			exit_usage(argv);
 		}
 
-		if (nr_process > MAX_PROCESSES || !nr_process) {
+		if (nr_process > MAX_PROCESSES || nr_process < 1) {
 			printk("nr_process should be between [1; %u]",
 					MAX_PROCESSES);
 			exit_usage(argv);
Signature · 1 line
Dmitry
#3 lore raw

PATCH 2/4 selftests/net: fix a char signedness issue

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Signedness of char is signed on x86_64, but unsigned on arm64.

Fix the warning building cmsg_sender.c on signed platforms or forced with -fsigned-char:

msg_sender.c:455:12:
error: implicit conversion from 'int' to 'char' changes value from 128 to -128 [-Werror,-Wconstant-conversion]
buf[0] = ICMPV6_ECHO_REQUEST;

constant ICMPV6_ECHO_REQUEST is 128.

Fixes de17e305a810 ("selftests: net: cmsg_sender: support icmp and raw sockets")
---
 tools/testing/selftests/net/cmsg_sender.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/cmsg_sender.c +1 -1
index 24b21b15ed3fb..6ff3e732f449f 100644
@@ -416,9 +416,9 @@ int main(int argc, char *argv[])
 {
 	struct addrinfo hints, *ai;
 	struct iovec iov[1];
+	unsigned char *buf;
 	struct msghdr msg;
 	char cbuf[1024];
-	char *buf;
 	int err;
 	int fd;
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#4 lore raw

PATCH 3/4 selftests/net: unix: fix unused variable compiler warning

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Remove an unused variable.

diag_uid.c:151:24:
error: unused variable 'udr'
[-Werror,-Wunused-variable]

Fixes ac011361bd4f ("af_unix: Add test for sock_diag and UDIAG_SHOW_UID.")
---
 tools/testing/selftests/net/af_unix/diag_uid.c | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/net/af_unix/diag_uid.c +0 -1
index 5b88f7129fea4..79a3dd75590e8 100644
@@ -148,7 +148,6 @@ void receive_response(struct __test_metadata *_metadata,
 		.msg_iov = &iov,
 		.msg_iovlen = 1
 	};
-	struct unix_diag_req *udr;
 	struct nlmsghdr *nlh;
 	int ret;
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#5 lore raw

PATCH 4/4 selftests/net: mptcp: fix uninitialized variable warnings

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Same init_rng() in both tests. The function reads /dev/urandom to initialize srand(). In case of failure, it falls back onto the entropy in the uninitialized variable. Not sure if this is on purpose. But failure reading urandom should be rare, so just fail hard. While at it, convert to getrandom(). Which man 4 random suggests is simpler and more robust.

mptcp_inq.c:525:6:
mptcp_connect.c:1131:6:

error: variable 'foo' is used uninitialized
whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]

Fixes 048d19d444be ("mptcp: add basic kselftest for mptcp")
Fixes b51880568f20 ("selftests: mptcp: add inq test case")
Cc Florian Westphal <[email protected]>

----

When input is randomized because this is expected to meaningfully explore edge cases, should we also add
1. logging the random seed to stdout and
2. adding a command line argument to replay from a specific seed I can do this in net-next, if authors find it useful in this case.

---
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 11 ++++-------
 tools/testing/selftests/net/mptcp/mptcp_inq.c     | 11 ++++-------
 2 files changed, 8 insertions(+), 14 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +4 -7
index c7f9ebeebc2c5..d2043ec3bf6d6 100644
@@ -18,6 +18,7 @@
 
 #include <sys/ioctl.h>
 #include <sys/poll.h>
+#include <sys/random.h>
 #include <sys/sendfile.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
@@ -1125,15 +1126,11 @@ int main_loop_s(int listensock)
 
 static void init_rng(void)
 {
-	int fd = open("/dev/urandom", O_RDONLY);
 	unsigned int foo;
 
-	if (fd > 0) {
-		int ret = read(fd, &foo, sizeof(foo));
-
-		if (ret < 0)
-			srand(fd + foo);
-		close(fd);
+	if (getrandom(&foo, sizeof(foo), 0) == -1) {
+		perror("getrandom");
+		exit(1);
 	}
 
 	srand(foo);
tools/testing/selftests/net/mptcp/mptcp_inq.c +4 -7
index 8672d898f8cda..218aac4673212 100644
@@ -18,6 +18,7 @@
 #include <time.h>
 
 #include <sys/ioctl.h>
+#include <sys/random.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -519,15 +520,11 @@ static int client(int unixfd)
 
 static void init_rng(void)
 {
-	int fd = open("/dev/urandom", O_RDONLY);
 	unsigned int foo;
 
-	if (fd > 0) {
-		int ret = read(fd, &foo, sizeof(foo));
-
-		if (ret < 0)
-			srand(fd + foo);
-		close(fd);
+	if (getrandom(&foo, sizeof(foo), 0) == -1) {
+		perror("getrandom");
+		exit(1);
 	}
 
 	srand(foo);
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
maintainer Reviewed-by on v1 4/4
#6 lore raw

Re: [PATCH net 4/4] selftests/net: mptcp: fix uninitialized variable warnings

Hi Willem,

(+ cc MPTCP list)

Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Same init_rng() in both tests. The function reads /dev/urandom to initialize srand(). In case of failure, it falls back onto the entropy in the uninitialized variable. Not sure if this is on purpose. But failure reading urandom should be rare, so just fail hard. While at it, convert to getrandom(). Which man 4 random suggests is simpler and more robust.

Quoted 6 lines

mptcp_inq.c:525:6:
mptcp_connect.c:1131:6:

error: variable 'foo' is used uninitialized
whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]

Thank you for the patch!

It looks good to me:

Quoted 5 lines
Fixes 048d19d444be ("mptcp: add basic kselftest for mptcp")
Fixes b51880568f20 ("selftests: mptcp: add inq test case")
Cc Florian Westphal <[email protected]>
Quoted 7 lines

----

When input is randomized because this is expected to meaningfully explore edge cases, should we also add
1. logging the random seed to stdout and
2. adding a command line argument to replay from a specific seed I can do this in net-next, if authors find it useful in this case.

I think we should have done that from the beginning, otherwise we cannot easily reproduce these edge cases. To be honest, I don't think this technique helped to find bugs, and it was probably used here as a good habit to increase the coverage. But on the other hand, we might not realise some inputs are randomised and can cause instabilities in the tests because we don't print anything about that.

So I would say that the minimal thing to do is to log the random seed. But it might not be that easy to do, for example 'mptcp_connect' is used a lot of time by the .sh scripts: printing this seed number each time 'mptcp_connect' is started will "flood" the logs. Maybe we should only print that at the end, in case of errors: e.g. in xerror() and die_perror() for example, but I see 'exit(1)' is directly used in other places...

That's more code to change, but if it is still OK for you to do that, please also note that you will need to log this to stderr: mptcp_connect prints what has been received from the other peer to stdout.

Because it is more than just adding a 'printf()', I just created a ticket in our bug tracker, so anybody can look at that and check all the details about that:

https://github.com/multipath-tcp/mptcp_net-next/issues/462

Quoted 9 lines
---
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 11 ++++-------
 tools/testing/selftests/net/mptcp/mptcp_inq.c     | 11 ++++-------
 2 files changed, 8 insertions(+), 14 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -0
index c7f9ebeebc2c5..d2043ec3bf6d6 100644

(...)

tools/testing/selftests/net/mptcp/mptcp_connect.c int main_loop_s(int listensock) · line 1131

Quoted 8 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -2
@@ -1125,15 +1126,11 @@ int main_loop_s(int listensock)
 
 static void init_rng(void)
 {
-	int fd = open("/dev/urandom", O_RDONLY);
 	unsigned int foo;
 
-	if (fd > 0) {

I just realised that here, we could have fd == 0 which is a valid value. I don't think we would have that when executing the selftests, but that's another reason to change this :)

tools/testing/selftests/net/mptcp/mptcp_connect.c int main_loop_s(int listensock) · line 1136

Quoted 11 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +3 -5
-		int ret = read(fd, &foo, sizeof(foo));
-
-		if (ret < 0)
-			srand(fd + foo);
-		close(fd);
+	if (getrandom(&foo, sizeof(foo), 0) == -1) {
+		perror("getrandom");
+		exit(1);
 	}
 
 	srand(foo);

Cheers,
Matt

#7 lore raw

Re: [PATCH net 4/4] selftests/net: mptcp: fix uninitialized variable warnings

Quoted 59 lines · Matthieu Baerts

Hi Willem,

(+ cc MPTCP list)

Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Same init_rng() in both tests. The function reads /dev/urandom to initialize srand(). In case of failure, it falls back onto the entropy in the uninitialized variable. Not sure if this is on purpose. But failure reading urandom should be rare, so just fail hard. While at it, convert to getrandom(). Which man 4 random suggests is simpler and more robust.

Quoted 6 lines

mptcp_inq.c:525:6:
mptcp_connect.c:1131:6:

error: variable 'foo' is used uninitialized
whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]

Thank you for the patch!

It looks good to me:

Quoted 5 lines
Fixes 048d19d444be ("mptcp: add basic kselftest for mptcp")
Fixes b51880568f20 ("selftests: mptcp: add inq test case")
Cc Florian Westphal <[email protected]>
Quoted 7 lines

----

When input is randomized because this is expected to meaningfully explore edge cases, should we also add
1. logging the random seed to stdout and
2. adding a command line argument to replay from a specific seed I can do this in net-next, if authors find it useful in this case.

I think we should have done that from the beginning, otherwise we cannot easily reproduce these edge cases. To be honest, I don't think this technique helped to find bugs, and it was probably used here as a good habit to increase the coverage. But on the other hand, we might not realise some inputs are randomised and can cause instabilities in the tests because we don't print anything about that.

So I would say that the minimal thing to do is to log the random seed. But it might not be that easy to do, for example 'mptcp_connect' is used a lot of time by the .sh scripts: printing this seed number each time 'mptcp_connect' is started will "flood" the logs. Maybe we should only print that at the end, in case of errors: e.g. in xerror() and die_perror() for example, but I see 'exit(1)' is directly used in other places...

That's more code to change, but if it is still OK for you to do that, please also note that you will need to log this to stderr: mptcp_connect prints what has been received from the other peer to stdout.

Quoted 5 lines

Because it is more than just adding a 'printf()', I just created a ticket in our bug tracker, so anybody can look at that and check all the details about that:

https://github.com/multipath-tcp/mptcp_net-next/issues/462

Thanks for the detailed feedback, Matthieu!

Another option to avoid flooding the logs might be to choose a pseudo random number in the script and pass the explicit value mptcp_connect.

I haven't looked closely, but for transport layer tests it is likely that the payload is entirely ignored. Bar perhaps checksum coverage. If it does not increase code coverage, randomization can also just be turned off.

Quoted 39 lines
Quoted 9 lines
---
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 11 ++++-------
 tools/testing/selftests/net/mptcp/mptcp_inq.c     | 11 ++++-------
 2 files changed, 8 insertions(+), 14 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -0
index c7f9ebeebc2c5..d2043ec3bf6d6 100644

(...)

tools/testing/selftests/net/mptcp/mptcp_connect.c int main_loop_s(int listensock) · line 1131

Quoted 8 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -2
@@ -1125,15 +1126,11 @@ int main_loop_s(int listensock)
 
 static void init_rng(void)
 {
-	int fd = open("/dev/urandom", O_RDONLY);
 	unsigned int foo;
 
-	if (fd > 0) {

I just realised that here, we could have fd == 0 which is a valid value. I don't think we would have that when executing the selftests, but that's another reason to change this :)

tools/testing/selftests/net/mptcp/mptcp_connect.c int main_loop_s(int listensock) · line 1136

Quoted 11 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +3 -5
-		int ret = read(fd, &foo, sizeof(foo));
-
-		if (ret < 0)
-			srand(fd + foo);
-		close(fd);
+	if (getrandom(&foo, sizeof(foo), 0) == -1) {
+		perror("getrandom");
+		exit(1);
 	}
 
 	srand(foo);

Cheers,
Matt

maintainer
#8 lore raw

Re: [PATCH net 4/4] selftests/net: mptcp: fix uninitialized variable warnings

Quoted 64 lines · Willem de Bruijn
Quoted 63 lines · Matthieu Baerts

Hi Willem,

(+ cc MPTCP list)

Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Same init_rng() in both tests. The function reads /dev/urandom to initialize srand(). In case of failure, it falls back onto the entropy in the uninitialized variable. Not sure if this is on purpose. But failure reading urandom should be rare, so just fail hard. While at it, convert to getrandom(). Which man 4 random suggests is simpler and more robust.

Quoted 6 lines

mptcp_inq.c:525:6:
mptcp_connect.c:1131:6:

error: variable 'foo' is used uninitialized
whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]

Thank you for the patch!

It looks good to me:

Quoted 5 lines
Fixes 048d19d444be ("mptcp: add basic kselftest for mptcp")
Fixes b51880568f20 ("selftests: mptcp: add inq test case")
Cc Florian Westphal <[email protected]>
Quoted 7 lines

----

When input is randomized because this is expected to meaningfully explore edge cases, should we also add
1. logging the random seed to stdout and
2. adding a command line argument to replay from a specific seed I can do this in net-next, if authors find it useful in this case.

I think we should have done that from the beginning, otherwise we cannot easily reproduce these edge cases. To be honest, I don't think this technique helped to find bugs, and it was probably used here as a good habit to increase the coverage. But on the other hand, we might not realise some inputs are randomised and can cause instabilities in the tests because we don't print anything about that.

So I would say that the minimal thing to do is to log the random seed. But it might not be that easy to do, for example 'mptcp_connect' is used a lot of time by the .sh scripts: printing this seed number each time 'mptcp_connect' is started will "flood" the logs. Maybe we should only print that at the end, in case of errors: e.g. in xerror() and die_perror() for example, but I see 'exit(1)' is directly used in other places...

That's more code to change, but if it is still OK for you to do that, please also note that you will need to log this to stderr: mptcp_connect prints what has been received from the other peer to stdout.

Because it is more than just adding a 'printf()', I just created a ticket in our bug tracker, so anybody can look at that and check all the details about that:

Quoted 6 lines
Quoted 1 line

https://github.com/multipath-tcp/mptcp_net-next/issues/462

Thanks for the detailed feedback, Matthieu!

Another option to avoid flooding the logs might be to choose a pseudo random number in the script and pass the explicit value mptcp_connect.

Good idea!

If I understood correctly, from the .c file, we can check if an env var is set (e.g. MPTCP_RND_SEED) and use it. If not, we generate a random one like before. The .sh scripts should generate a random number if the env var is not already set. In any case, this seed should be printed by the scripts.

Quoted 4 lines

I haven't looked closely, but for transport layer tests it is likely that the payload is entirely ignored. Bar perhaps checksum coverage. If it does not increase code coverage, randomization can also just be turned off.

Here the randomisation is used to change the length of the data that are exchanged or to do some actions in different orders. I think it still makes sense to have randomisation. But in case of issues around that, it might not be clear what the userspace was exactly doing. That's what can be improved later in net-next.

Cheers,
Matt

#9 lore raw
In reply to Willem de Bruijn
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 24 Nov 2023 12:15:18 -0500 you wrote:

From: Willem de Bruijn <[email protected]>

Observed a clang warning when backporting cmsg_sender. Ran the same build against all the .c files under selftests/net.

This is clang-14 with -Wall
Which is what tools/testing/selftests/net/Makefile also enables.

[...]

Here is the summary with links:
- [net,1/4] selftests/net: ipsec: fix constant out of range

    https://git.kernel.org/netdev/net/c/088559815477
  - [net,2/4] selftests/net: fix a char signedness issue
    https://git.kernel.org/netdev/net/c/7b29828c5af6
  - [net,3/4] selftests/net: unix: fix unused variable compiler warning
    https://git.kernel.org/netdev/net/c/59fef379d453
  - [net,4/4] selftests/net: mptcp: fix uninitialized variable warnings
    https://git.kernel.org/netdev/net/c/00a4f8fd9c75

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html