Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when compiling c++ code with glibc, MINSIGSTKSZ gets incorrect value of -1 #10713

Closed
andrewrk opened this issue Jan 28, 2022 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Jan 28, 2022

Zig Version

0.10.0-dev.445+d7deffee8

Steps to Reproduce

$ cat test.cpp
#include <csignal>
#include <iostream>

int main() {
      std::cout << "Hello World!" << MINSIGSTKSZ << "\n";
      return 0;
}
$ zig run -lc++ test.cpp -target native-native-musl
Hello World!2048
$ zig run -lc++ test.cpp -target native-native-gnu
Hello World!-1
$ cat test.c
#include <signal.h>
#include <stdio.h>
int main(int argc, char **argv) {
    printf("%d\n", MINSIGSTKSZ);
    return 0;
}
$ zig run -lc test.c -target native-native-gnu
2048

Expected Behavior

Expected the same result (2048) for musl and glibc, and expected the same result (2048) for C++ and C.

Actual Behavior

When compiling C++ code with glibc, MINSIGSTKSZ has incorrect value of -1.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior os-linux labels Jan 28, 2022
@andrewrk andrewrk added this to the 0.9.1 milestone Jan 28, 2022
@andrewrk
Copy link
Member Author

andrewrk commented Jan 28, 2022

Here's what I figured out so far:

When using -target with gnu as the C ABI, the default -std flag used by clang++ is -std=gnu++14. This sets the _GNU_SOURCE preprocessor definition. In turn, this causes:

/* If _GNU_SOURCE was defined by the user, turn on all the other features.  */
#ifdef _GNU_SOURCE
# undef  _ISOC95_SOURCE
# define _ISOC95_SOURCE	1
# undef  _ISOC99_SOURCE
# define _ISOC99_SOURCE	1
# undef  _ISOC11_SOURCE
# define _ISOC11_SOURCE	1
# undef  _ISOC2X_SOURCE
# define _ISOC2X_SOURCE	1
# undef  _POSIX_SOURCE
# define _POSIX_SOURCE	1
# undef  _POSIX_C_SOURCE
# define _POSIX_C_SOURCE	200809L
# undef  _XOPEN_SOURCE
# define _XOPEN_SOURCE	700
# undef  _XOPEN_SOURCE_EXTENDED
# define _XOPEN_SOURCE_EXTENDED	1
# undef	 _LARGEFILE64_SOURCE
# define _LARGEFILE64_SOURCE	1
# undef  _DEFAULT_SOURCE
# define _DEFAULT_SOURCE	1
# undef  _ATFILE_SOURCE
# define _ATFILE_SOURCE	1
# undef  _DYNAMIC_STACK_SIZE_SOURCE
# define _DYNAMIC_STACK_SIZE_SOURCE 1
#endif

Emphasis on _DYNAMIC_STACK_SIZE_SOURCE. This gets used like this:

#if defined __USE_DYNAMIC_STACK_SIZE && __USE_DYNAMIC_STACK_SIZE
# include <unistd.h>

/* Default stack size for a signal handler: sysconf (SC_SIGSTKSZ).  */
# undef SIGSTKSZ
# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

/* Minimum stack size for a signal handler: SIGSTKSZ.  */
# undef MINSIGSTKSZ
# define MINSIGSTKSZ SIGSTKSZ
#endif

So turns out MINSIGSTKSZ actually ends up being a call to sysconf which is apparently returning -1 (error) for some reason.

@andrewrk
Copy link
Member Author

andrewrk added a commit that referenced this issue Feb 3, 2022
This is a patch to glibc features.h which makes
_DYNAMIC_STACK_SIZE_SOURCE undefined unless the version is >= 2.34.

This feature was introduced with glibc 2.34 and without this patch, code
built against these headers but then run on an older glibc will end up
making a call to sysconf() that returns -1 for the value of SIGSTKSZ
and MINSIGSTKSZ.

Closes #10713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Projects
None yet
Development

No branches or pull requests

1 participant