Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code explanation

Name: Anonymous 2012-01-11 12:49

Can somebody explain why this code outputs what it does?

// tested with Core 2 Duo, Core 2 Quad and Xeon
// tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
// compile with: gcc -O0 -m32
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

jmp_buf p;
void (*q)();

const char *data =
    "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
    "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
    "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
    "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
    "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
    "\x55\x90\x90\x89\xe5\x90\x90\x90"
    "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
    "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
    "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
    "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
    "\x6e\x73\x69\x64\x65\x72\x65\x64"
    "\x20\x68\x61\x72\x6d\x66\x75\x6c"
    "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";

int f(int x)
{
    static int b = 0; static int s = 0;
    int a = 0, t;
    if (!s) {
        a = b; b = x;
    } else {
        a = x; t = b;
        do {
            a ^= b;
            b = (a^b) & b;
            b <<= 1;
        } while (b);
        b = t;
    }
    s = (s+1) % 2;
    return a;
}

int g(int i, int *j)
{
    *j = i;
    i = (int) putchar;
    if (*j == (48 << 1)) 
        __asm volatile (
                "movl 8(%ebp),%eax;"
                "leave;"
                "ret"
                );
    return (int) puts;
}

void h(int i)
{
    int b;
    q = (void(*)()) g(i++[data],&b);
    for (f(b);*(data+i)!=b;++i,f(b))
        q(f(i[data])%0xff);
}

void sh(int s)
{
    if (s == 010)
        ((void(*)())g(s,&s))("F");
    longjmp(p,s);
}

int main(void)
{
    int base, addr = 0xffffffff, offs = 16;
    int a = 11, b = 32, i = 25;
    int s = 8, t = 1, u = 4;
    ((void(*)()) data)(&a,&b);
    ((void(*)()) data)(&b,&t);
    ((void(*)()) data)(&t,&s);
    addr ^= a;
       a ^= addr;
    addr ^= a;
    base = ((int(*)())data+addr)();
    if (a == -1)
        goto over;
    puts("A");

    base = (1<<3) | ((f(addr) + f(offs)) & ~0xff);
    h(base+addr+offs);
    exit(0);

over:
    signal(t,sh);signal(s,sh);signal(u,sh);

    if (!(s = setjmp(p))) {
        q = (void(*)()) g(0x30, &a);
        q(data + a + i);
        s = a / (b-1);
        puts("B");
    } else if (s == 0xb) {
        puts("C");
        ((int(*)(int)) data+addr+(offs/2))(base);
    } else {
        puts("D");
        *((int*) base+s) = 0xffffffff;
    }
   
    puts("E");
    return 1;
}

Name: Anonymous 2012-01-11 12:52

I see no code in [code] tags.

Name: Anonymous 2012-01-11 12:53

>>2
Sorry, I'm new to the text boards.

http://pastebin[dot]com/uPHf2wZU

Name: Anonymous 2012-01-11 12:53

It outputs Segmentation fault, I guess this has something to do with an address being mishandled.

Name: Anonymous 2012-01-11 12:53


const char *data =
    "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
    "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
    "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
    "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
    "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
    "\x55\x90\x90\x89\xe5\x90\x90\x90"
    "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
    "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
    "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
    "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
    "\x6e\x73\x69\x64\x65\x72\x65\x64"
    "\x20\x68\x61\x72\x6d\x66\x75\x6c"
    "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";


\x90Nop

Stopped reading right there. Is this a fancy stack smasher?

Name: Anonymous 2012-01-11 12:54


// tested with Core 2 Duo, Core 2 Quad and Xeon
// tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
// compile with: gcc -O0 -m32
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

jmp_buf p;
void (*q)();

const char *data =
    "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
    "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
    "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
    "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
    "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
    "\x55\x90\x90\x89\xe5\x90\x90\x90"
    "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
    "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
    "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
    "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
    "\x6e\x73\x69\x64\x65\x72\x65\x64"
    "\x20\x68\x61\x72\x6d\x66\x75\x6c"
    "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";

int f(int x)
{
    static int b = 0; static int s = 0;
    int a = 0, t;
    if (!s) {
        a = b; b = x;
    } else {
        a = x; t = b;
        do {
            a ^= b;
            b = (a^b) & b;
            b <<= 1;
        } while (b);
        b = t;
    }
    s = (s+1) % 2;
    return a;
}

int g(int i, int *j)
{
    *j = i;
    i = (int) putchar;
    if (*j == (48 << 1))
        __asm volatile (
                "movl 8(%ebp),%eax;"
                "leave;"
                "ret"
                );
    return (int) puts;
}

void h(int i)
{
    int b;
    q = (void(*)()) g(i++[data],&b);
    for (f(b);*(data+i)!=b;++i,f(b))
        q(f(i[data])%0xff);
}

void sh(int s)
{
    if (s == 010)
        ((void(*)())g(s,&s))("F");
    longjmp(p,s);
}

int main(void)
{
    int base, addr = 0xffffffff, offs = 16;
    int a = 11, b = 32, i = 25;
    int s = 8, t = 1, u = 4;
    ((void(*)()) data)(&a,&b);
    ((void(*)()) data)(&b,&t);
    ((void(*)()) data)(&t,&s);
    addr ^= a;
       a ^= addr;
    addr ^= a;
    base = ((int(*)())data+addr)();
    if (a == -1)
        goto over;
    puts("A");

    base = (1<<3) | ((f(addr) + f(offs)) & ~0xff);
    h(base+addr+offs);
    exit(0);

over:
    signal(t,sh);signal(s,sh);signal(u,sh);

    if (!(s = setjmp(p))) {
        q = (void(*)()) g(0x30, &a);
        q(data + a + i);
        s = a / (b-1);
        puts("B");
    } else if (s == 0xb) {
        puts("C");
        ((int(*)(int)) data+addr+(offs/2))(base);
    } else {
        puts("D");
        *((int*) base+s) = 0xffffffff;
    }
  
    puts("E");
    return 1;
}

Name: Anonymous 2012-01-11 12:54

The output is undefined as it uses a lot of non-portable conventions.

Name: Anonymous 2012-01-11 12:55

>>5
>XOR, XOR, XOR
It looks like a swap function to me

Name: Anonymous 2012-01-11 12:56

>>7
It says in the comments what to compile it with on where

Name: Anonymous 2012-01-11 12:56

I'm not running that.

Name: Anonymous 2012-01-11 12:57

Lots of people who can't read the comments here.

>>10
>>9
>>8
>>7
>>6
>>5
>>4
goto considered harmfull
F
D
C
A
install gentoo

Name: Anonymous 2012-01-11 12:58

>>9
Really? Where does it say where it should be compiled?

Name: Anonymous 2012-01-11 12:59

>core 2 duo, core 2 quad, xeon
Pretty clear you need to run it on a IA32/x86_64 of some sort.

Name: Anonymous 2012-01-11 12:59

>>9
Well you are a dumb Jew.

Name: Anonymous 2012-01-11 13:00

On my machine it outputs Segmentation fault, I guess that's because it relies on undefined behavior.

Name: Anonymous 2012-01-11 13:01

>>9
I specialize in the standard, not some obscure toy subset language.

Name: Anonymous 2012-01-11 13:02

>>16
>not doing hw specific coding ever
why are you even here

Name: Anonymous 2012-01-11 13:02

>>15
unable to compile with gcc -m32 -O0

Name: [code] tags advocate 2012-01-11 13:03


/ tested with Core 2 Duo, Core 2 Quad and Xeon
 // tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
 // compile with: gcc -O0 -m32
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
 #include <setjmp.h>

 jmp_buf p;
 void (*q)();

 const char *data =
     "\x8b\x44\x24\x04\x8b\x5c\x24\x08"
     "\x8b\x00\x8b\x1b\x31\xc3\x31\xd8"
     "\x31\xc3\x8b\x4c\x24\x04\x89\x01"
     "\x8b\x4c\x24\x08\x89\x19\xc3\x90"
     "\x55\x89\xe5\x8b\x45\x04\xc9\xc3"
     "\x55\x90\x90\x89\xe5\x90\x90\x90"
     "\x8b\x45\x08\x89\x45\x04\xc9\xc3"
     "\x60\x09\x0e\x13\x14\x01\x0c\x0c"
     "\xc0\x07\x05\x0e\x14\x0f\x0f\x60"
     "\x00\x67\x6f\x74\x6f\x20\x63\x6f"
     "\x6e\x73\x69\x64\x65\x72\x65\x64"
     "\x20\x68\x61\x72\x6d\x66\x75\x6c"
     "\x6c\x00\x90\x90\x1c\x1b\x0a\x20";

 int f(int x)
 {
     static int b = 0; static int s = 0;
     int a = 0, t;
     if (!s) {
         a = b; b = x;
     } else {
         a = x; t = b;
         do {
             a ^= b;
             b = (a^b) & b;
             b <<= 1;
         } while (b);
         b = t;
     }
     s = (s+1) % 2;
     return a;
 }

 int g(int i, int *j)
 {
     *j = i;
     i = (int) putchar;
     if (*j == (48 << 1)) 
         __asm volatile (
                 "movl 8(%ebp),%eax;"
                 "leave;"
                 "ret"
                 );
     return (int) puts;
 }

 void h(int i)
 {
     int b;
     q = (void(*)()) g(i++[data],&b);
     for (f(b);*(data+i)!=b;++i,f(b))
         q(f(i[data])%0xff);
 }

 void sh(int s)
 {
     if (s == 010)
         ((void(*)())g(s,&s))("F");
     longjmp(p,s);
 }

 int main(void)
 {
     int base, addr = 0xffffffff, offs = 16;
     int a = 11, b = 32, i = 25;
     int s = 8, t = 1, u = 4;
     ((void(*)()) data)(&a,&b);
     ((void(*)()) data)(&b,&t);
     ((void(*)()) data)(&t,&s);
     addr ^= a;
        a ^= addr;
     addr ^= a;
     base = ((int(*)())data+addr)();
     if (a == -1)
         goto over;
     puts("A");

     base = (1<<3) | ((f(addr) + f(offs)) & ~0xff);
     h(base+addr+offs);
     exit(0);

 over:
     signal(t,sh);signal(s,sh);signal(u,sh);

     if (!(s = setjmp(p))) {
         q = (void(*)()) g(0x30, &a);
         q(data + a + i);
         s = a / (b-1);
         puts("B");
     } else if (s == 0xb) {
         puts("C");
         ((int(*)(int)) data+addr+(offs/2))(base);
     } else {
         puts("D");
         *((int*) base+s) = 0xffffffff;
     }
    
     puts("E");
     return 1;
 }

Name: Anonymous 2012-01-11 13:04

>>15
>>7

 // tested with Core 2 Duo, Core 2 Quad and Xeon
 // tested with gcc4.1.2 gcc4.4.3 and gcc4.6.1
 // compile with: gcc -O0 -m32

Name: Anonymous 2012-01-11 13:06

Seriously, /prog/? You disappoint me. Even /g/ managed to compile and run it at least

Name: Anonymous 2012-01-11 13:08

__asm is a reserved identifier, so your code isn't even C, the parts of the code that might C is full of undefined behavior.

In short you are a fucking retard who should fuck off back to /g/.

Name: Anonymous 2012-01-11 13:10

>>21
Well, that's prove that we're superior to /g/ as we will not compile and run suspiciously looking code(and I'm not going to setup new virtual machine just because I got dubs.)

Only /g/ can be so stupid. And /b/, speaking of which /polecat kebabs/.

Name: Anonymous 2012-01-11 13:10

>>22
>hurr i have never done any hw specific code

Name: Anonymous 2012-01-11 13:12

>>23
you can run it in a jail or you can even do a hex dump, no running involved

Name: Anonymous 2012-01-11 13:12

And since >>22 stole dubs while I was writing reply, I'm inclined to setup vm even less now.

Name: Anonymous 2012-01-11 13:13

>>26
just set up a jail, vm isn't required

Name: Anonymous 2012-01-11 13:14

>>21
I ain't gonna switch off from my super ricer desktop to my
obscure gcc compilin desktop just to see a stack smashing warning.

Name: Anonymous 2012-01-11 13:15

>>28
>stack smash
>implying you need to reboot after one
lulwut

Name: Anonymous 2012-01-11 13:17

>>24
I have but I assure you it was a lot better than this undefined piece of shit.

Name: Anonymous 2012-01-11 13:17

~/prog$ gcc -m32 -O0 obscure.c && ./a.out
goto considered harmfull
F
D
C
A
install gentoo

Name: Anonymous 2012-01-11 13:18

>>30
The code is intentionally retarded fuckwit

Name: Anonymous 2012-01-11 13:18

>>30
But you're still not clever enough to figure it out

Name: Anonymous 2012-01-11 13:22

>>32,33
Okay listen you fucking retard, read this and understand it.

__asm is a reserved identifier so the code is not C

The parts of the code that might be C, is full of undefined behavior, so there is nothing to figure out, what it outputs is undefined.

There you go fucknut now fuck off back to /g/ you stupid piece of shit.

Name: Anonymous 2012-01-11 13:23

>>34
>__asm is a reserved identifier so the code is not C
And exactly what is your point? Nowhere did OP claim that it was C.

Name: Anonymous 2012-01-11 13:24

>>34
Dude, no where does anyone claim that it is C. OP asked for the program flow. It certainly does compile and run. You're just being an ignorant dick.

Name: Anonymous 2012-01-11 13:24

>>35
That's implicit by asking us to compile it with a C compiler, are you mentally challenged?

Name: Anonymous 2012-01-11 13:25

>>36
It doesn't compile on my machine.

Name: Anonymous 2012-01-11 13:26

>>37
GCC compiles much more than C, you moron. GCC compiles both C and C++, and it also assembles and links. It even supports Java for fuck sake.

GCC is a compiler suite, not a C compiler. You are a fucking idiot and should kill yourself

Name: Anonymous 2012-01-11 13:27

>>37
GCC isn't a C compiler, it is the GNU compiler suite. It supports everything from Ada to Fortran to Java. It is also a assembler, if you haven't noticed.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List