Skip to content

Instantly share code, notes, and snippets.

@enfiskutensykkel
Created July 9, 2019 21:23
Show Gist options
  • Save enfiskutensykkel/df299dfc631cde0cfb85bac787fd1992 to your computer and use it in GitHub Desktop.
Save enfiskutensykkel/df299dfc631cde0cfb85bac787fd1992 to your computer and use it in GitHub Desktop.
Dijkstra would not approve...
#include <stddef.h>
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
static jmp_buf env;
static const char* shc =
"\x55\x48\x89\xe5\x48\x89\x7d\xe8\x48\x8b\x45\xe8"
"\xc7\x00\x08\x00\x00\x00\x48\xc7\x45\xf8\x00\x00"
"\x00\x00\x48\x8b\x45\x08\x48\x89\x45\xf8\x48\x8b"
"\x45\xf8\x5d\xc3\x69\x05\x05\x01\xed\x0b\x00\xb4"
"\x47\xfe\x09\x06\xfb\x00\x9b\xf6\xde\xad\xbe\xef";
static int calculate(int a, int b)
{
do
{
a ^= b;
b = (a ^ b) & b;
b <<= 1;
}
while (b);
return a & 255;
}
static int showstr(const char* str)
{
size_t n = 0;
char buf[64];
buf[0] = *str;
for (n = 1; buf[n - 1] != 0; ++n)
{
buf[n] = calculate(buf[n - 1], str[n]);
}
asm volatile (
"movq $1, %%rax;"
"movq $1, %%rdi;"
"movq %0, %%rsi;"
"movq %1, %%rdx;"
"syscall"
:: "r" (buf), "r" (n)
: "rax", "rdi", "rsi", "rdx");
return calculate(n, -n);
}
static void dealwithit(int s)
{
longjmp(env, s);
}
int main()
{
int i = EXIT_SUCCESS;
int s;
void (*bad)();
bad = ((void (*(*)())(int*)) shc)(&s);
if (s == 010)
{
signal(s, dealwithit);
if (!setjmp(env))
{
int a = 2;
printf("The result of %d / %d is %d\n", a, i, a / i);
goto leave;
}
puts("goto considered harmful");
}
else
{
s = showstr(shc + 40);
goto leave;
}
--s;
bad();
puts("EWD would not approve!");
leave:
exit(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment