Skip to contentSkip to conversation

examples/ucontext-cp.c: cope with variable SIGSTKSZ

PATCH In review Stefan Hajnoczi 5 participants 7 messages Apr 13, 2021 – Apr 23, 2021

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

PATCH 1/1 examples/ucontext-cp.c: cope with variable SIGSTKSZ

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

CC ucontext-cp
ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope

  31 |         unsigned char stack_buf[SIGSTKSZ];
     |                       ^~~~~~~~~

make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

commit 6c57d320484988e87e446e2e60ce42816bf51d53
Author: H.J. Lu <[email protected]>
Date: Mon Feb 1 11:00:38 2021 -0800

    sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]

...
+# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

---
 examples/ucontext-cp.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
examples/ucontext-cp.c +9 -3
index 0b2a6b5..ea0c934 100644
@@ -28,7 +28,7 @@
 
 typedef struct {
 	struct io_uring *ring;
-	unsigned char stack_buf[SIGSTKSZ];
+	unsigned char *stack_buf;
 	ucontext_t ctx_main, ctx_fnew;
 } async_context;
 
@@ -115,8 +115,13 @@ static int setup_context(async_context *pctx, struct io_uring *ring)
 		perror("getcontext");
 		return -1;
 	}
-	pctx->ctx_fnew.uc_stack.ss_sp = &pctx->stack_buf;
-	pctx->ctx_fnew.uc_stack.ss_size = sizeof(pctx->stack_buf);
+	pctx->stack_buf = malloc(SIGSTKSZ);
+	if (!pctx->stack_buf) {
+		perror("malloc");
+		return -1;
+	}
+	pctx->ctx_fnew.uc_stack.ss_sp = pctx->stack_buf;
+	pctx->ctx_fnew.uc_stack.ss_size = SIGSTKSZ;
 	pctx->ctx_fnew.uc_link = &pctx->ctx_main;
 
 	return 0;
@@ -174,6 +179,7 @@ static void copy_file_wrapper(arguments_bundle *pbundle)
 	free(iov.iov_base);
 	close(pbundle->infd);
 	close(pbundle->outfd);
+	free(pbundle->pctx->stack_buf);
 	free(pbundle->pctx);
 	free(pbundle);
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 23 lines · Stefan Hajnoczi

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

CC ucontext-cp
ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope

  31 |         unsigned char stack_buf[SIGSTKSZ];
     |                       ^~~~~~~~~

make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

commit 6c57d320484988e87e446e2e60ce42816bf51d53
Author: H.J. Lu <[email protected]>
Date: Mon Feb 1 11:00:38 2021 -0800

    sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]

...
+# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

Quoted 7 lines

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

glibc folks, please take a look. The commit referenced above broke compilation of liburing's tests. It's possible that applications will hit similar issues. Can you check whether the SIGSTKSZ change needs to be reverted/fixed before releasing glibc 2.34?

Thanks,
Stefan

Quoted 44 lines
---
 examples/ucontext-cp.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
examples/ucontext-cp.c +9 -3
index 0b2a6b5..ea0c934 100644
@@ -28,7 +28,7 @@
 
 typedef struct {
 	struct io_uring *ring;
-	unsigned char stack_buf[SIGSTKSZ];
+	unsigned char *stack_buf;
 	ucontext_t ctx_main, ctx_fnew;
 } async_context;
 
@@ -115,8 +115,13 @@ static int setup_context(async_context *pctx, struct io_uring *ring)
 		perror("getcontext");
 		return -1;
 	}
-	pctx->ctx_fnew.uc_stack.ss_sp = &pctx->stack_buf;
-	pctx->ctx_fnew.uc_stack.ss_size = sizeof(pctx->stack_buf);
+	pctx->stack_buf = malloc(SIGSTKSZ);
+	if (!pctx->stack_buf) {
+		perror("malloc");
+		return -1;
+	}
+	pctx->ctx_fnew.uc_stack.ss_sp = pctx->stack_buf;
+	pctx->ctx_fnew.uc_stack.ss_size = SIGSTKSZ;
 	pctx->ctx_fnew.uc_link = &pctx->ctx_main;
 
 	return 0;
@@ -174,6 +179,7 @@ static void copy_file_wrapper(arguments_bundle *pbundle)
 	free(iov.iov_base);
 	close(pbundle->infd);
 	close(pbundle->outfd);
+	free(pbundle->pctx->stack_buf);
 	free(pbundle->pctx);
 	free(pbundle);
 
Signature · 1 line
2.30.2
#2 lore raw
Quoted 33 lines · Stefan Hajnoczi
Quoted 23 lines · Stefan Hajnoczi

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

CC ucontext-cp
ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope

  31 |         unsigned char stack_buf[SIGSTKSZ];
     |                       ^~~~~~~~~

make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

commit 6c57d320484988e87e446e2e60ce42816bf51d53
Author: H.J. Lu <[email protected]>
Date: Mon Feb 1 11:00:38 2021 -0800

    sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]

...
+# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

Quoted 7 lines

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

Quoted 5 lines

glibc folks, please take a look. The commit referenced above broke compilation of liburing's tests. It's possible that applications will hit similar issues. Can you check whether the SIGSTKSZ change needs to be reverted/fixed before releasing glibc 2.34?

It won't be changed for glibc 2.34.

Signature · 1 line
H.J.
#3 lore raw
Quoted 34 lines · H.J. Lu
Quoted 33 lines · Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

CC ucontext-cp
ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope

  31 |         unsigned char stack_buf[SIGSTKSZ];
     |                       ^~~~~~~~~

make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

commit 6c57d320484988e87e446e2e60ce42816bf51d53
Author: H.J. Lu <[email protected]>
Date: Mon Feb 1 11:00:38 2021 -0800

    sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]

...
+# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

Quoted 7 lines
Quoted 5 lines

glibc folks, please take a look. The commit referenced above broke compilation of liburing's tests. It's possible that applications will hit similar issues. Can you check whether the SIGSTKSZ change needs to be reverted/fixed before releasing glibc 2.34?

It won't be changed for glibc 2.34.

Thanks for the response, H.J. and Paul.

In that case liburing needs this patch.

Stefan

Reviewed-by
#4 lore raw

+Cc: [email protected]
+Cc: Pavel Begunkov <[email protected]>

Original message: https://www.spinics.net/lists/linux-block/msg67077.html

Quoted 41 lines · Stefan Hajnoczi
Quoted 40 lines · H.J. Lu
Quoted 38 lines · Stefan Hajnoczi
Quoted 23 lines · Stefan Hajnoczi

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

CC ucontext-cp
ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope

  31 |         unsigned char stack_buf[SIGSTKSZ];
     |                       ^~~~~~~~~

make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

commit 6c57d320484988e87e446e2e60ce42816bf51d53
Author: H.J. Lu <[email protected]>
Date: Mon Feb 1 11:00:38 2021 -0800

    sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]

...
+# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

Quoted 7 lines

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

glibc folks, please take a look. The commit referenced above broke compilation of liburing's tests. It's possible that applications will hit similar issues. Can you check whether the SIGSTKSZ change needs to be reverted/fixed before releasing glibc 2.34?

Quoted 6 lines
Quoted 1 line

It won't be changed for glibc 2.34.

Thanks for the response, H.J. and Paul.

In that case liburing needs this patch.

I think so:

#5 lore raw
Quoted 49 lines · Stefano Garzarella
(quoted diff) +2 -0
+Cc: [email protected]
+Cc: Pavel Begunkov <[email protected]>

Original message: https://www.spinics.net/lists/linux-block/msg67077.html

Quoted 43 lines · Stefan Hajnoczi
Quoted 41 lines · H.J. Lu
Quoted 33 lines · Stefan Hajnoczi
Quoted 23 lines · Stefan Hajnoczi

The size of C arrays at file scope must be constant. The following compiler error occurs with recent upstream glibc (2.33.9000):

   CC ucontext-cp
   ucontext-cp.c:31:23: error: variably modified ‘stack_buf’ at file scope 31 |         unsigned char stack_buf[SIGSTKSZ];
      |                       ^~~~~~~~~
   make[1]: *** [Makefile:26: ucontext-cp] Error 1

The following glibc commit changed SIGSTKSZ from a constant value to a variable:

   commit 6c57d320484988e87e446e2e60ce42816bf51d53
   Author: H.J. Lu <[email protected]> Date:   Mon Feb 1 11:00:38 2021 -0800

     sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]
   ...
   +# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

Allocate the stack buffer explicitly to avoid declaring an array at file scope.

Quoted 7 lines

---
Perhaps the glibc change needs to be revised before releasing glibc 2.34 since it might break applications. That's up to the glibc folks. It doesn't hurt for liburing to take a safer approach that copes with the SIGSTKSZ change in any case.

Quoted 5 lines

glibc folks, please take a look. The commit referenced above broke compilation of liburing's tests. It's possible that applications will hit similar issues. Can you check whether the SIGSTKSZ change needs to be reverted/fixed before releasing glibc 2.34?

It won't be changed for glibc 2.34.

Quoted 8 lines
Quoted 4 lines

Thanks for the response, H.J. and Paul.

In that case liburing needs this patch.

I think so:

Right, and there are already people complaining
https://github.com/axboe/liburing/issues/320

Signature · 1 line
Pavel Begunkov
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 3 lines · Stefan Hajnoczi via Libc-alpha

The commit referenced above broke
compilation of liburing's tests. It's possible that applications will hit similar issues.

Yes, other applications have already had similar issues, and we've fixed that by recoding them to not assume that SIGSTKSZ is an integer constant expression. See, for example, this patch to Gnulib last September:

https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=f9e2b20a12a230efa30f1d479563ae07d276a94b

This patch appears in the latest GNU grep, for example.