vsock/test: fix wrong setsockopt() parameters
Subsystems (4)
v4 cover vsock/test: fix wrong setsockopt() parameters
Parameters were created using wrong C types, which caused them to be of wrong size on some architectures, causing problems.
The problem with SO_RCVLOWAT was found on s390 (big endian), while x86-64 didn't show it. After the fix, all tests pass on s390.
Then Stefano Garzarella pointed out that SO_VM_SOCKETS_* calls might have a similar problem, which turned out to be true, hence, the second patch.
Changes for v4:
- add "Reviewed-by:" to the first patch, and add a second patch fixing SO_VM_SOCKETS_* calls, which depends on the first one (hence, it's now a patch series.)
Changes for v3:
- fix the same problem in vsock_perf and update commit message Changes for v2:
- add "Fixes:" lines to the commit message
Konstantin Shkolnyy (2):
vsock/test: fix failures due to wrong SO_RCVLOWAT parameter vsock/test: fix parameter types in SO_VM_SOCKETS_* calls
tools/testing/vsock/vsock_perf.c | 8 ++++---- tools/testing/vsock/vsock_test.c | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
PATCH v4 1/2 vsock/test: fix failures due to wrong SO_RCVLOWAT parameter
This happens on 64-bit big-endian machines.SO_RCVLOWAT requires an int parameter. However, instead of int, the test uses unsigned long in one place and size_t in another. Both are 8 bytes long on 64-bit machines. The kernel, having received the 8 bytes, doesn't test for the exact size of the parameter, it only cares that it's >= sizeof(int), and casts the 4 lower-addressed bytes to an int, which, on a big-endian machine, contains 0. 0 doesn't trigger an error, SO_RCVLOWAT returns with success and the socket stays with the default SO_RCVLOWAT = 1, which results in vsock_test failures, while vsock_perf doesn't even notice that it's failed to change it.
--- tools/testing/vsock/vsock_perf.c | 6 +++--- tools/testing/vsock/vsock_test.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/vsock/vsock_perf.c +3 -3
@@ -133,7 +133,7 @@ static float get_gbps(unsigned long bits, time_t ns_delta) ((float )ns_delta /NSEC_PER_SEC ); } -static void run_receiver (unsigned long rcvlowat_bytes) +static void run_receiver (int rcvlowat_bytes) {unsigned int read_cnt;time_t rx_begin_ns;
@@ -163,7 +163,7 @@ static void run_receiver(unsigned long rcvlowat_bytes)printf ("Listen port %u\n" , port);printf ("RX buffer %lu bytes\n" , buf_size_bytes);printf ("vsock buffer %lu bytes\n" , vsock_buf_bytes); -printf ("SO_RCVLOWAT %lu bytes\n" , rcvlowat_bytes); +printf ("SO_RCVLOWAT %d bytes\n" , rcvlowat_bytes); fd =socket (AF_VSOCK ,SOCK_STREAM ,0 );
@@ -439,7 +439,7 @@ static long strtolx(const char *arg)int main (int argc,char **argv) {unsigned long to_send_bytes =DEFAULT_TO_SEND_BYTES ; -unsigned long rcvlowat_bytes =DEFAULT_RCVLOWAT_BYTES ; +int rcvlowat_bytes =DEFAULT_RCVLOWAT_BYTES ;int peer_cid =-1 ;bool sender = false;
tools/testing/vsock/vsock_test.c +2 -2
@@ -835,7 +835,7 @@ static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)static void test_stream_poll_rcvlowat_client (const struct test_opts *opts) { -unsigned long lowat_val =RCVLOWAT_BUF_SIZE ; +int lowat_val =RCVLOWAT_BUF_SIZE ;char buf[RCVLOWAT_BUF_SIZE ];struct pollfd fds;short poll_flags;
@@ -1357,7 +1357,7 @@ static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *optstatic void test_stream_credit_update_test (const struct test_opts *opts,bool low_rx_bytes_test) { -size_t recv_buf_size; +int recv_buf_size;struct pollfd fds;size_t buf_size;void *buf;
Signature · 1 line
2.34.1
PATCH v4 2/2 vsock/test: fix parameter types in SO_VM_SOCKETS_* calls
In reply to Konstantin ShkolnyyChange parameters of SO_VM_SOCKETS_* to uint64_t so that they are always 64-bit, because the corresponding kernel code requires them to be at least that large, no matter what architecture.
--- tools/testing/vsock/vsock_perf.c | 2 +- tools/testing/vsock/vsock_test.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-)
tools/testing/vsock/vsock_perf.c +1 -1
@@ -33,7 +33,7 @@static unsigned int port =DEFAULT_PORT ;static unsigned long buf_size_bytes =DEFAULT_BUF_SIZE_BYTES ; -static unsigned long vsock_buf_bytes =DEFAULT_VSOCK_BUF_BYTES ; +static uint64_t vsock_buf_bytes =DEFAULT_VSOCK_BUF_BYTES ;static bool zerocopy;static void error (const char *s)
tools/testing/vsock/vsock_test.c +14 -5
@@ -429,7 +429,7 @@ static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)static void test_seqpacket_msg_bounds_server (const struct test_opts *opts) { -unsigned long sock_buf_size; +uint64_t sock_buf_size;unsigned long remote_hash;unsigned long curr_hash;int fd;
@@ -634,7 +634,8 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)static void test_seqpacket_bigmsg_client (const struct test_opts *opts) { -unsigned long sock_buf_size; +uint64_t sock_buf_size; +size_t buf_size;socklen_t len;void *data;int fd;
@@ -655,13 +656,19 @@ static void test_seqpacket_bigmsg_client(const struct test_opts *opts) sock_buf_size++; - data =malloc (sock_buf_size); + buf_size = (size_t ) sock_buf_size;/* size_t can be < uint64_t */ +if (buf_size != sock_buf_size) { +fprintf (stderr,"Returned BUFFER_SIZE too large\n" ); +exit (EXIT_FAILURE ); + } + + data =malloc (buf_size);if (!data) {perror ("malloc" );exit (EXIT_FAILURE ); } -send_buf (fd, data, sock_buf_size,0 , -EMSGSIZE ); +send_buf (fd, data, buf_size,0 , -EMSGSIZE );control_writeln ("CLISENT" );
@@ -1360,6 +1367,7 @@ static void test_stream_credit_update_test(const struct test_opts *opts,int recv_buf_size;struct pollfd fds;size_t buf_size; +uint64_t sock_buf_size;void *buf;int fd;
@@ -1370,9 +1378,10 @@ static void test_stream_credit_update_test(const struct test_opts *opts, } buf_size =RCVLOWAT_CREDIT_UPD_BUF_SIZE ; + sock_buf_size = buf_size;/* size_t can be < uint64_t */ if (setsockopt (fd,AF_VSOCK ,SO_VM_SOCKETS_BUFFER_SIZE , - &buf_size,sizeof (buf_size))) { + &sock_buf_size,sizeof (sock_buf_size))) {perror ("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)" );exit (EXIT_FAILURE ); }
Signature · 1 line
2.34.1
Stefano Garzarella
maintainerRe: [PATCH v4 2/2] vsock/test: fix parameter types in SO_VM_SOCKETS_* calls
Quoted 1 line · Konstantin Shkolnyy
Change parameters of SO_VM_SOCKETS_* to
uint64_tso that they are always
In include/uapi/linux/vm_sockets.h we talk about "unsigned long long",
but in the kernel code we use u64. IIUC "unsigned long long" should be u64 on every architecture, at least till we will have some 128-bit cpu, right?
Quoted 18 lines
64-bit, because the corresponding kernel code requires them to be at least that large, no matter what architecture.
Fixes 5c338112e48a ("test/vsock: rework message bounds test")Fixes 685a21c314a8 ("test/vsock: add big message test")Fixes 542e893fbadc ("vsock/test: two tests to check credit update logic")Fixes 8abbffd27ced ("test/vsock: vsock_perf utility")Signed-off-by Konstantin Shkolnyy <[email protected]>---
tools/testing/vsock/vsock_perf.c| 2 +-tools/testing/vsock/vsock_test.c| 19 ++++++++++++++-----2 files changed, 15 insertions(+), 6 deletions(-)tools/testing/vsock/vsock_perf.c +0
-0
tools/testing/vsock/vsock_perf.c line 36
Quoted 5 lines
tools/testing/vsock/vsock_perf.c +1
-1@@ -33,7 +33,7 @@static unsigned int port =DEFAULT_PORT ;static unsigned long buf_size_bytes =DEFAULT_BUF_SIZE_BYTES ; -static unsigned long vsock_buf_bytes =DEFAULT_VSOCK_BUF_BYTES ; +static uint64_t vsock_buf_bytes =DEFAULT_VSOCK_BUF_BYTES ;
What about using unsigned long long as documented in the vm_sockets.h?
Thanks,
Stefano
Quoted 71 lines
tools/testing/vsock/vsock_perf.c +0
-0static bool zerocopy;static void error (const char *s)tools/testing/vsock/vsock_test.c +14
-5@@ -429,7 +429,7 @@ static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)static void test_seqpacket_msg_bounds_server (const struct test_opts *opts) { -unsigned long sock_buf_size; +uint64_t sock_buf_size;unsigned long remote_hash;unsigned long curr_hash;int fd;@@ -634,7 +634,8 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)static void test_seqpacket_bigmsg_client (const struct test_opts *opts) { -unsigned long sock_buf_size; +uint64_t sock_buf_size; +size_t buf_size;socklen_t len;void *data;int fd;@@ -655,13 +656,19 @@ static void test_seqpacket_bigmsg_client(const struct test_opts *opts) sock_buf_size++; - data =malloc (sock_buf_size); + buf_size = (size_t ) sock_buf_size;/* size_t can be < uint64_t */ +if (buf_size != sock_buf_size) { +fprintf (stderr,"Returned BUFFER_SIZE too large\n" ); +exit (EXIT_FAILURE ); + } + + data =malloc (buf_size);if (!data) {perror ("malloc" );exit (EXIT_FAILURE ); } -send_buf (fd, data, sock_buf_size,0 , -EMSGSIZE ); +send_buf (fd, data, buf_size,0 , -EMSGSIZE );control_writeln ("CLISENT" );@@ -1360,6 +1367,7 @@ static void test_stream_credit_update_test(const struct test_opts *opts,int recv_buf_size;struct pollfd fds;size_t buf_size; +uint64_t sock_buf_size;void *buf;int fd;@@ -1370,9 +1378,10 @@ static void test_stream_credit_update_test(const struct test_opts *opts, } buf_size =RCVLOWAT_CREDIT_UPD_BUF_SIZE ; + sock_buf_size = buf_size;/* size_t can be < uint64_t */ if (setsockopt (fd,AF_VSOCK ,SO_VM_SOCKETS_BUFFER_SIZE , - &buf_size,sizeof (buf_size))) { + &sock_buf_size,sizeof (sock_buf_size))) {perror ("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)" );exit (EXIT_FAILURE ); }Signature · 1 line
2.34.1
Re: [PATCH v4 2/2] vsock/test: fix parameter types in SO_VM_SOCKETS_* calls
Quoted 5 lines · Stefano Garzarella
Quoted 1 line · Konstantin Shkolnyy
Change parameters of SO_VM_SOCKETS_* to
uint64_tso that they are alwaysIn
include/uapi/linux/vm_sockets.hwe talk about "unsigned long long",
but in the kernel code we use u64. IIUC "unsigned long long" should be u64 on every architecture, at least till we will have some 128-bit cpu, right?
I'm not sure what "unsigned long long" would be on a 128-bit machine.
Quoted 1 line
What about using
unsigned long longas documented in thevm_sockets.h?
I use uint64_t because the kernel uses u64. I think, this way the code isn't vulnerable to potential variability of "unsigned long long". If we change to "unsigned long long" should we also change the kernel to "unsigned long long"?
Stefano Garzarella
maintainerRe: [PATCH v4 2/2] vsock/test: fix parameter types in SO_VM_SOCKETS_* calls
Quoted 7 lines · Konstantin Shkolnyy
Quoted 5 lines · Stefano Garzarella
Quoted 1 line · Konstantin Shkolnyy
Change parameters of SO_VM_SOCKETS_* to
uint64_tso that they are alwaysIn
include/uapi/linux/vm_sockets.hwe talk about "unsigned long long",
but in the kernel code we use u64. IIUC "unsigned long long" should be u64 on every architecture, at least till we will have some 128-bit cpu, right?
Quoted 6 lines
I'm not sure what "unsigned long long" would be on a 128-bit machine.
Quoted 1 line
What about using
unsigned long longas documented in thevm_sockets.h?I use
uint64_tbecause the kernel uses u64. I think, this way the code isn't vulnerable to potential variability of "unsigned long long".
IMHO the test should look more at UAPI than implementation.
Since we document to use "unsigned long long" I think we should use that in the test to check that our implementation behaves well with what we suggest the user to do.
Quoted 3 lines
If we change to "unsigned long long" should we also change the kernel to "unsigned long long"?
For now, it should not change much to use u64 or unsigned long long, but I agree that it would be better to change it. I would do it in a separate series, though.
Thanks,
Stefano