Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use -fsplit-stack on a large project

I recently posted a question about stack segmentation and boost coroutines but it seems like the -fsplit-stack approach only works with source files that are compiled with that flag, the runtime breaks down when you branch to another function that has not been compiled with -fsplit-stack. For example

This implies that the runtime uses a function local technique to detect when the current stack has been surpassed. And not a "guard page signal" trick, where the end of the stack always has a guard page which will raise a signal on write or read, telling the runtime to allocate a new stack frame and branch to that.

Then what is the use of this flag? If I link to any other library that has not been built with this, code will break (even libstdc++ and libc), then how is this something people use practically with big projects?


From reading the gcc wiki about split stacks it seems like calling a non split stack function from a split stack function results in an allocation of a 64KB stack frame. Good.

But it seems like calling a non split stack function from a function pointer has not yet been implemented to follow the above scheme.

What use is this flag then? If I proceed to call any virtual function will my program break?

Further from the answer below it seems like clang has not implemented split stacks?

like image 800
Curious Avatar asked Sep 20 '17 04:09

Curious


People also ask

What is an example of a large project?

The obvious example of a large project to most people is when major infrastructure projects are undertaken such as roads, bridges and large-buildings are constructed.

How do you manage large scale projects?

Large scale projects need special management. Linear strategies and waterfall for example will not work here. You need something more dynamic, overall. An efficient solution would also be to work on an end-to-end basis.

What are the core project management practices for large scale projects?

4. Core Project Management practices Large scale projects need special management. Linear strategies and waterfall for example will not work here. You need something more dynamic, overall. An efficient solution would also be to work on an end-to-end basis. It means that the team works on the project from A to Z.

What is the best way to manage resources in a project?

- Assigning specific resources (i.e., project team members) to planning roles for a 4- to 6-week horizon. - Pooled resources versus team-based resources. Leverage resources across the program when possible.

What are megaprojects and why do they matter?

Recent research on megaprojects — defined as projects costing more than $1 billion — reveals five lessons that can help executives manage any large-scale project more effectively. Already a member?


2 Answers

You have to compile boost (at least boost.context and boost.coroutine) with segmeented-stacks support AND your application.

  • compile boost (boost.context and boost.coroutine) with b2 property segmented-stacks=on (enables special code inside boost.coroutine and boost.context).

  • your app has to be compiled with -DBOOST_USE_SEGMENTED_STACKS and -fsplit-stack (required by boost.coroutines headers).

see boost.coroutine documentation

boost.coroutine contains an example that demonstrates segmented stacks (in directory coroutine/example/asymmetric/ call b2 toolset=gcc segmented-stacks=on).

regarding your last question GCC Wiki states:

For calls from split-stack code to non-split-stack code, the linker will change the initial instructions in the split-stack (caller) function. This means that the linker will have to have special knowledge of the instructions that the compiler emits. The effect of the changes will be to increase the required framesize by a number large enough to reasonably work for a non-split-stack. This will be a target dependent number; the default will be something like 64K. Note that this large stack will be released when the split-stack function returns. Note that I'm disregarding the case of split-stack code in a shared library calling non-split-stack code in the main executable; that seems like an unlikely problem.

please note: while llvm supports segmented stacks, clang seams not to provide the __splitstack_<xyz> functions.

like image 98
xlrg Avatar answered Sep 22 '22 13:09

xlrg


First I'd say split stack support is somewhat experimental in nature to begin with. It is not a widely supported thing nor has a single implementation become accepted as the way to go. As such, part of the purpose of it existing in the compiler is to enable research in real use.

That said, one generally wants to use such a feature to enable lots of threads with small stacks, but which can get bigger if they need to. In some applications, the code that runs in these threads can be tightly controlled. E.g. fairly specialized request handlers that do not call general purpose libraries such as Boost. High performance systems work often involves tightening down the constraints on what code is used in a given path and this would be an example thereof. It certainly limits the applicability of the feature, but I wouldn't be surprised if someone is using it in production this way.

Note that similar issues exist with flags such as -fno-exceptions and -fno-rtti . Generally C++ requires compiling everything that goes into an executable with a compatible set of flags. Sometimes one can mix and match, but it is often fragile. This is part of the motivation of building everything from source and hermetic build tools like bazel. Other languages have different approaches to non-source components, especially virtual machine based languages such as Java and the .NET family. In those worlds things like split stacks are decided at a lower-level of compilation, but typically one would not have any control over or awareness of them at the source code level.

like image 34
Zalman Stern Avatar answered Sep 24 '22 13:09

Zalman Stern