4

I'm building a test program with g++ 9.3.0 on Ubuntu 18.04 x64 using this linker command:

/usr/bin/g++-9  -O3 -DNDEBUG  -Wl,-z,stack-size=268435456 CMakeFiles/af.dir/main.cpp.o  -o af

I'm testing increased stack requirements with this function:

float f() {
  float a[3'000'000];
  return a[42];
}

and results indicate a standard 8MB stack regardless of the stack-size linker option value. What is the way to increase the stack size?

EDIT

The test is as follows:

  • float a[3'000'000]; causes Segmentation fault (core dumped)
  • float a[2'000'000]; returns 0 as expected
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72

1 Answers1

2

It seems that setting stack size with g++ 9.3.0 linker options on Ubuntu 18.04 x64 is not possible. As suggested in comments, I settled for a run-time option:

#include <sys/resource.h>
#include <fmt/format.h>

int main() {
  if (struct rlimit rl{1<<28, 1l<<33}; setrlimit(RLIMIT_STACK, &rl))
    fmt::print("Can not set stack size! (errno = {})\n", errno);
  else
    fmt::print("Stack size: {}MiB up to {}GiB\n", rl.rlim_cur/(1<<20), rl.rlim_max/(1<<30));
...
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • Could you report a bug in Launchpad (or maybe Binutils Bugzilla)? – yugr May 17 '20 at 08:32
  • @yugr I will if you point me to the documentation, which claims that `stack-size` option is functional in my environment. I searched and couldn't find a clear statement to this effect. – Paul Jurczak May 17 '20 at 11:06
  • Even if it doesn't, `ld` should emit a warning. – yugr May 17 '20 at 11:16
  • @yugr Can you post a link to appropriate bug reporting page? I don't know where linkers are made. – Paul Jurczak May 17 '20 at 12:37
  • 1
    I suggest to first file a bug in Ubuntu's own tracker, Launchpad ([here](https://bugs.launchpad.net/ubuntu/+source/binutils) is the page for Binutils bugs). They'll redirect it to https://sourceware.org/bugzilla/ if needed. – yugr May 17 '20 at 12:40
  • @yugr Done: https://bugs.launchpad.net/ubuntu/+source/binutils/+bug/1879154 – Paul Jurczak May 17 '20 at 13:15