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 13:29

>>38
Are you using gcc?

>>37
Fucking moron. See >>39 and >>40

Name: Anonymous 2012-01-11 13:31

>>39-41
So you're saying that language is gnu89? The program is still then undefined you fucking retards.

Name: Anonymous 2012-01-11 13:32

41 posts in 40 minutes. What the hell, /prog/.

Name: Anonymous 2012-01-11 13:32

>>42
Just gtfo asspie

Name: Anonymous 2012-01-11 13:32

>>44
Are you angry because your code is shit?

Name: Anonymous 2012-01-11 13:32

>>43
>and no answer to OP
;_;

Name: Anonymous 2012-01-11 13:32

>>46
The answer is undefined you fucking retard are you blind?

Name: Anonymous 2012-01-11 13:33

>>45
Of course it is shit, it is intentionally shit. That is the point. YOU ARE TOO RETARDED TO FIGURE IT OUT.

Name: Anonymous 2012-01-11 13:34

>>48
I have already figured it out you fucking retard, it's UNDEFINED.

Name: Anonymous 2012-01-11 13:35

>>49
facepalm

Name: Anonymous 2012-01-11 13:37

>>49
Not sure if troll, but
>what is the output of this code WHEN COMPILED WITH GCC AND RAN ON AN INTEL ARCHITECTURE
It is kind of implicit in the question.

Name: Anonymous 2012-01-11 13:37

Name: Anonymous 2012-01-11 13:38

>>51
It's still undefined.

Name: Anonymous 2012-01-11 13:39

>>53
Dude. Stop. Just stop.

Name: Anonymous 2012-01-11 13:40

>>53
What the hell is wrong with you dude?

Name: Anonymous 2012-01-11 13:41

>>54,55
There is nothing wrong with me you dumb Jew, what the fuck is wrong with you? How the hell is undefined a hard concept to get?

Name: Anonymous 2012-01-11 13:42

>>56
Confirmed for autistic faggot

Name: Anonymous 2012-01-11 13:43

>>57
You mad that your code is undefined?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-11 13:43

And you all think that I'm out of control....

Name: Anonymous 2012-01-11 13:44

>>58
The only thing you're accomplishing is to confirm that you aren't clever enough to actually objdump the code and step through it to figure what is going on.

Forever a code monkey. Now go back to your scripting languages.

Name: Anonymous 2012-01-11 13:44

>>59
For all we know, this is you.

Name: Anonymous 2012-01-11 13:45

>>60
How may I objdump something that doesn't compile?

Name: Anonymous 2012-01-11 13:45

>>61
It's not Kodak, I can assure you.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-11 13:46

>>61
Nah. I don't write like that.

Name: Anonymous 2012-01-11 13:46

>>62
By compiling it with the correct compiler, e.g. one proposed in the comments.

Name: Anonymous 2012-01-11 13:50

>>60
Why wouldn't you just use gdb and step through the source code?

Name: Anonymous 2012-01-11 13:52

>>66
Of course, you could always use gdb. But gdb gets a bit confused when the symbols are stripped and the function is outside the .text section

Name: Anonymous 2012-01-11 13:53

>>67
So why are you stripping the symbols?

Name: Anonymous 2012-01-11 13:54

So this is OS specific, compiler specific, processor specific, compiler flags specific code?

Name: Anonymous 2012-01-11 13:55

>>69
Yep. Not so much OS specific as architecture specific, compiler and compiler specific code though.

Name: Anonymous 2012-01-11 13:56

>>69
Not OS specific, but processor and compiler specific. Yes. Or, actually, to be more correct, processor and assembler specific code.

Name: Anonymous 2012-01-11 13:58

>>52
This.

The first 32 characters of the char data is a swap function. Actually identical to the link, are you copypastaing OP?

Name: Anonymous 2012-01-11 13:59

>>71
So those signals are not OS specific?

Name: Anonymous 2012-01-11 14:01

~/prog$ gcc -o a.out -m32 -O0 -c obscure.c && strings a.out
D$0
D$,1D$<
D$,3D$<
D$,1D$<
goto considered harmfull

Name: Anonymous 2012-01-11 14:01

>>73
No, those are POSIX. You're correct though, but any POSIX OS should be able to run it.

Name: Anonymous 2012-01-11 14:08

>>75
Mac OS X doesn't allow executing the const char * data.

Name: Anonymous 2012-01-11 14:10

>>76
Processor?

Also, does Mac OS X support NX? In that case, it won't run.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-11 14:17

>>76
Mac OS X doesn't allow executing the const char * data

Uhh... it this because something like this isn't a value?!!!

Name: Anonymous 2012-01-11 14:18

>>78
Probably because it is put in a read only section with other strings, and OS X doesn't allow readonly sections to be executed.

Name: Anonymous 2012-01-11 14:21

>>79
wat

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