Skip to content
Snippets Groups Projects
Commit c40459d5 authored by j2-tulloch's avatar j2-tulloch :speech_balloon:
Browse files

Add new file

parent 3b95cd0e
Branches
No related tags found
No related merge requests found
#include <iostream>
#include <array>
#include "context.h"
void foo() {
std::cout << "you called foo" << std::endl;
exit(0); // Exit to prevent returning to a different stack
}
void goo() {
std::cout << "you called goo" << std::endl;
exit(0);
}
int main() {
// Allocate space for stacks
std::array<char, 4096> stackFoo, stackGoo;
// Set up stack pointers, aligned to 16 bytes and accounting for the Red Zone
char* spFoo = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stackFoo.data() + 4096) & -16L) - 128);
char* spGoo = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stackGoo.data() + 4096) & -16L) - 128);
Context cFoo, cGoo;
// Set up contexts
get_context(&cFoo);
cFoo.rip = reinterpret_cast<void*>(&foo);
cFoo.rsp = spFoo;
get_context(&cGoo);
cGoo.rip = reinterpret_cast<void*>(&goo);
cGoo.rsp = spGoo;
// Use set_context to switch to either foo or goo
set_context(&cFoo); // This will transfer control to foo
return 0; // Return an integer from main
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment