| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
╔══════════════════════════════════════════════════════════════╗ ║ ║ ║ > stackman ║ ║ ┌─┐┌┬┐┌─┐┌─┐┬┌─┌┬┐┌─┐┌┐┌ ║ ║ └─┐ │ ├─┤│ ├┴┐│││├─┤│││ ║ ║ └─┘ ┴ ┴ ┴└─┘┴ ┴┴ ┴┴ ┴┘└┘ ║ ║ ║ ║ Do you have a stack? Stackman is here to help. ║ ║ ║ ║ v1.2.5 • 10 platforms • Stack switching simplified ║ ║ ║ ╚══════════════════════════════════════════════════════════════╝
Simple low-level stack manipulation API and implementation for common platforms
This library aims to provide a basic API to perfom stack manipulation on various platforms. Stack manipulation involves changing the machine stack pointer while optionally saving and restoring the stack contents.
Manipulating the stack pointer allows the implementation of continuations and long jump functionality in C. However, this invariably involves low-level assembly code, to save and restore machine registers and manipulate the stack pointer itself. Each machine platform, and tool suite, will need appropriate customization for it to work.
Various projects perform stack manipulation to provide co-routine functionality:
However, each of these perform things slightly differently and individual platform implementations have to be maintained for each of them.
This library aims to provide a simple api that can be shared among all projects that manipulate the C stack.
Additionally, it provides a set of pre-assembled libraries for the most common platforms so that no assembly steps are required by users.
Simple api
Simple implementation
Assembly support
The straightforward and application-agnostic switching allows the switching function to be implemented in full assembler. This removes the risk of inline-assembler doing any sort of unexpected things such as in-lining the function or otherwise change the assumptions that the function makes about its environment. This assembly code can be created by the in-line assembler in a controlled environment.
No dependencies
The library merely provides stack switching. It consist only of a couple of functions with no dependencies.
Stable
There is no need to add or modify functionality.
Libraries provided.
The aim is to provide pre-assembled libraries for the most popular platforms. This relieves other tools that want to do stack manipulation from doing any sort of assembly or complex linkage. Just include the headers and link to the appropriate library.
The current code is distilled out of other work, with the aim of simplifying and standardizing the api. A number of ABI specifications is supported, meaning architecture and calling convention, plus archive format:
All platforms are automatically built and tested by GitHub Actions CI on every commit.
The BSDs share the sysv_amd64 ABI with Linux and therefore need no separate assembly implementation or pre-built library; they are built and tested from source by the build and test bsd workflow. Note that the Makefile requires GNU make, which on the BSDs is gmake rather than the base system make.
Other platforms can be easily adapted from both existing implementations for other projects as well as from example code provided.
There are two functions that make up the stackman library: stakman_switch() and stackman_call() who both take a stackman_cb_t callback:
typedef void *(*stackman_cb_t)(
void *context, int opcode, void *stack_pointer);
void *stackman_switch(stackman_cb_t callback, void *context);
void *stackman_call(stackman_cb_t callback, void *context, void *stack);This is the main stack manipulation API. When called, it will call callback function twice:
The context pointer is passed as-is to the callback, allowing it access to user-defined data.
Depending on how the callback function is implemented, this API can be used for a number of things, like saving a copy of the stack, perform a stack switch, query the stack pointer, and so on.
This is a helper function to call a callback function, optionally providing it with a different stack to use.
This function is useful for at least three things:
The last feature is useful to bypass any in-lining that a compiler may do, when one really wants a proper function call with stack, for example, when setting up a new stack entry point.
There are two basic ways to add the library to your project: Using a static library or inlining the code.
Note: Most platforms now use separate assembly files (.S or .asm) rather than inline assembly. Inline assembly is only available for some platforms when using GCC (not Clang), and using separate assembly files is generally preferred for reliability and compatibility.
To include the implementation directly in your project:
Platforms with inline assembly support:
Only when compiled with GCC and STACKMAN_INLINE_ASM is not disabled. Clang, RISC-V, macOS, and Windows always use separate assembly files.
In the case of inlined code, it can be specified to prefer in-line assembly and static linkage over separate assembly language source.
The project uses GitHub Actions to automatically:
Tagged versions (e.g., v1.0.0) automatically trigger:
Download stable releases from: https://github.com/kristjanvalur/stackman/releases
See .github/workflows/buildcommit.yml for the complete CI configuration.
Linux on x86-64 can be used to cross compile for x86 and ARM targets. This is most useful to generate assembly code, e.g. when compiling stackman/platform/gen_asm.c
The x86 tools require the gcc-multilib and g++-multilib packages to be installed. They, however, can't co-exist with the gcc-arm-linux-gnueabi or gcc-aarch64-linux-gnu packages on some distributions, and so development for these platforms may need to be done independently.
The Makefile normally auto-detects the ABI via compiler macro probing. In some cross-build wrappers, target intent may not be fully visible to that probe. Set STACKMAN_ABI to force the selected output library ABI deterministically.
Supported values match the ABI names in this document (for example: sysv_i386, sysv_amd64, arm32, aarch64, riscv64, darwin_x86_64, darwin_arm64, win_x86, win_x64, win_arm64). For compatibility, win_aarch64 is also accepted and normalized to win_arm64.
Examples:
Intel's Control-Flow Enforcement Technology is incompatible with stack switching because it employs a secondary Shadow Stack, that the user-mode program cannot modify. Unexpected return flow after a stack switch would cause the processor to fault. Because of this, we need to mark any assembly code as not CET compatible by adding special compiler flags to supporting compilers (currently modern GNU C). Modern compilers are beginning to generate CET compatible objects and once supporting CPUs start to arrive, processes which consist entirely of CET compatible code may be run in such a protected environment. See https://software.intel.com/content/www/us/en/develop/articles/technical-look-control-flow-enforcement-technology.html for more information
This works was originally inspired by Stackless Python by Christian Tismer, where the original switching code was developed.
Later projects, like gevent/greenlet have taken that idea and provided additional platform compatibility but with a different implementation, making the switching code itself incompatible.
Our work on additional stack-manipulating libraries prompted us to try to distill this functionality in its rawest form into a separate, low-level, library. Such that any project, wishing to implement co-routine-like behaviour on the C-stack level, could make use of simple, stable code, that can be easily extended for additional platforms as they come along.
| Back | FazBrowse Home | New Git URL |