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-12 11:49

>>279
But arithmetic with function pointers are.

Name: Anonymous 2012-01-12 11:54

>>280
OP never claimed it to be safe, nor to work on any architecture.

Name: Anonymous 2012-01-12 11:56

>>282
Well if it doesn't work on any architecture why are we having this discussion? If it doesn't work on any architecture then the program obviously doesn't output anything and the reason why is because it doesn't work on any architecture.

Name: Anonymous 2012-01-12 12:02

>>283
But it does work on some architectures... How hard is that to grasp, exactly?

Name: Anonymous 2012-01-12 12:02

>>284
But it does work on some architectures
You can't guarantee that.

Name: Anonymous 2012-01-12 12:07

>>281
Are you sure about that?

Name: Anonymous 2012-01-12 12:09

>>285
It as simple as this, if it doesn't compile or it segfaults then don't bother answering in this thread. If it does compile and works, try to provide a program flow for OP. How is this hard to understand?

Also, see >>142

Name: Anonymous 2012-01-12 12:10

>>284
How hard is it to grasp the concept of undefined behavior?

Name: Anonymous 2012-01-12 12:10

>>286
If you want to prove me wrong feel free to show me where in the standard it says that it's allowed to do arithmetic on function pointer types.

Name: Anonymous 2012-01-12 12:12

>>287
Compile the program in >>125 and tell me what it outputs, perhaps that will help you realize that running a programming 4 times doesn't necessarily tell you much about it.

Name: Anonymous 2012-01-12 12:16

>>270,276,281
I guess it depends on which standard you use. Arithmetics with void pointers are generally considered non-standard, but function pointer arithmetics are considered standard.

Name: Anonymous 2012-01-12 12:18

>>288,290
And how hard is it to grasp that that piece of code isn't relying on undefined behavior, but the underlaying architecture.

Name: Anonymous 2012-01-12 12:35

>>289
| function pointer arithmetics aren't allowed but array of function pointers is
I think you need to think before you write.

Name: Anonymous 2012-01-12 12:41

I will only run ENTERPRISE QUALITY code in my machine

Name: Anonymous 2012-01-12 12:52

>>293
fp is a function pointer, fp[1]() is undefined.
fpa is an array of function pointers, fpa[1]() is defined, fpa[1][1]() is undefined.

Name: Anonymous 2012-01-12 12:55

>>295
I'm not sure if we're talking about the same thing now, but I don't see how (*(fpa+1))() wouldn't be allowed, while fpa[1]() would.

Name: Anonymous 2012-01-12 12:57

Next, we discuss #pragmas.

Name: Anonymous 2012-01-12 12:57

>>296
but I don't see how (*(fpa+1))() wouldn't be allowed,
You're still doing pointer arithmetic on an array, it is allowed.

Name: Anonymous 2012-01-12 13:00

>>1-298

Dennis Ritchie is rolling in his grave right now.

Name: Anonymous 2012-01-12 13:00

>>298
Oh, I see where the misunderstanding is now.

Name: Anonymous 2012-01-12 13:55

>>291
Are you fucking retarded you dumb piece of shit? The C standard is obviously the standard in question, where you can't do pointer arithmetic with void pointers and you can't do pointer arithmetic with function pointer types.

Name: Anonymous 2012-01-12 13:56

>>292
It relies on both you fucking moron.

Name: Anonymous 2012-01-12 14:06

you guys are mean... im going back to reddit with my cool programs fuck you

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-12 14:06

>>298
Technically you're not doing pointer arithmetic on an array. Psst....hey toilet scrubber, an array and an element in that array are two different things. One is an unmodifiable lvalue and the other isn't.

Name: VIPPER 2012-01-12 14:07

>>303
Make sure you never come back.

Name: Anonymous 2012-01-12 14:13

>>302
Prove it. No, __asm is considered bad and potentially dangerous, not undefined.

>>301
| The C standard
You know there are multiple standards, moron. You can have an array of functions, you are this retarded.

Name: Anonymous 2012-01-12 14:15

>>306
*function pointers

Name: Anonymous 2012-01-12 14:25

>>306
You know there are multiple standards, moron. You can have an array of functions, you are this retarded.
I said that you can't do arithmetic on function pointers you dumb piece of shit, learn do read you fucking retard.

And please you retarded piece of crap, show me which of the C standards allows arithmetic on function pointers.

Name: Anonymous 2012-01-12 14:27

>>308
Gnu99, amongst others

Name: Anonymous 2012-01-12 14:28

>>309
Hahaha.

Name: Anonymous 2012-01-12 14:29

>>309
You dumb piece of shit, Gnu99 isn't a C standard you fucking retard. Are you seriously this fucking dumb?

Name: Anonymous 2012-01-12 14:30

>>309
GNU is the new standards committee guys, you heard it here first.
Free as in freedom C standards.

Name: Anonymous 2012-01-12 14:32

>>312
I wouldn't mind that, ISO is ran by a bunch of Jews.

Name: Anonymous 2012-01-12 14:40

>>312
Enjoying your bloated freedom at all costs?

Name: Anonymous 2012-01-12 14:44

>>314
He who sacrifices freedom for non-bloatedness deserves Windows.

Name: Anonymous 2012-01-12 14:45

>>315
Windows isn't bloated? Now I've heard everything.

Name: Anonymous 2012-01-12 14:46

>>314
Enjoying your shit performance?

Name: Anonymous 2012-01-12 15:02

>>316
It's less bloated than GUN/Rinux.

Name: Anonymous 2012-01-12 15:10

>>318
Who's talking about Loonix?

Name: Anonymous 2012-01-12 15:29

IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT
IM BETTER THAN U _BECAUSE_ I STAY AT HOME ARGUING ABOUT AUTISTIC ASM SHIT

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