Skip to content

Instantly share code, notes, and snippets.

@Milek7

Milek7/test.cpp Secret

Created December 4, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Milek7/1100114e5fbe2864f6ee15acb06a2c29 to your computer and use it in GitHub Desktop.
Save Milek7/1100114e5fbe2864f6ee15acb06a2c29 to your computer and use it in GitHub Desktop.
#include <stddef.h>
struct SaveLoad {
bool global;
void *address;
};
static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
{
return const_cast<unsigned char *>((const unsigned char*)(sld->global ? nullptr : object) + (ptrdiff_t)sld->address);
}
struct Settings {
int a;
int b;
int c;
int d;
};
Settings settings;
void setup1() __attribute__ ((optnone));
struct SaveLoad *setup2() __attribute__ ((optnone));
void setup1() {
settings.a = 11;
settings.b = 22;
settings.c = 33;
settings.d = 44;
}
struct SaveLoad *setup2() {
SaveLoad *sl = new SaveLoad();
sl->global = true;
sl->address = &settings.c;
return sl;
}
int test1() {
setup1();
SaveLoad *sl = setup2();
void *ptr1 = GetVariableAddress(nullptr, sl);
return *((int*)ptr1);
}
int test2() {
setup1();
SaveLoad *sl = setup2();
void *ptr1 = GetVariableAddress(&settings, sl);
return *((int*)ptr1);
}
#include <stdio.h>
int main()
{
printf("%d, %d\n", test1(), test2());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment