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

>>39
>>40
The code posted is neither: C,C++,Ada,Fortan nor Java

Please go back to the imageboards you uneducated pieces of shit.

Please learn what the word `undefined' means.
Please learn what reserved keywords mean.
`

Name: Anonymous 2012-01-11 14:30

>>81
>reserved keyword
That is exactly what it is for. Inline assembly. You are so god damned retarded it hurts.

Name: Anonymous 2012-01-11 14:31

>>81
Are you back again?

Name: Anonymous 2012-01-11 14:32

>>81
>hurr wuts assembly durrrrr

Name: Anonymous 2012-01-11 14:33

>>82-84
Why are you samefagging so much?

Name: Anonymous 2012-01-11 14:35

>>82
>>83
>>84
Enjoying your non-standard, unportable, undefined code that's shit?

I hope you do realize this code is as it says: `undefined'. The output is not guaranteed to be the same for all people. Can you comprehend that simple statement?

Name: Anonymous 2012-01-11 14:37

>>86
>same compiler on Intel architecture
Its not going to be different, retard

Name: Anonymous 2012-01-11 14:37

>>86
No they can't, I have tried.
I'm the person he thinks you are now.

Just ask them show you where __asm is defined in any of the C standards.

Name: Anonymous 2012-01-11 14:37

>>86-87
samefag samefagging samefag

Name: Anonymous 2012-01-11 14:38

>>87
There is no way you can guarantee that, that is due to the undefined behavior.

Name: Anonymous 2012-01-11 14:39

>>87-88
For fuck sake, why do you think it is reserved? That's right, to inline assembly. God you are retarded.

Name: Anonymous 2012-01-11 14:39

OP, just go back to /g/.

Name: Anonymous 2012-01-11 14:40

>>90
There is. Same compiler for same architecture will produce same machine code -> same result. It isn't undefined behaviour. __asm doesn't result in undefined behaviour you fucking moron, __asm inlines assembly.

Name: Anonymous 2012-01-11 14:41

>>87
HURR DURR WE ALL HAVE INTELS AND THE SAME PROCESSOR. HOW DO I PORTABLE CODE.
>>88
see >>74
Your code is undefined, stop acting as if it's not.
Some people can't even compile your code even with the commands you give because it is not standard.

Name: Anonymous 2012-01-11 14:41

>>91
Read the standard you fucking retard. Any identifier that starts with two leading underscores is reserved, there is nothing in the standard that defines what __asm does or even mentions assembly.
__asm isn't part of C you fucking moron now fuck off back to /g/ with your retarded shit.

Name: Anonymous 2012-01-11 14:42

FIGHT FIGHT

Name: Anonymous 2012-01-11 14:43

K&R The C programming language, 2ed page 192. (apendix A)

The following identifiers are reserved for use as keywords (AS keywords[sic!]), and may not be used otherwise: [...]
Some implementations reserve the words fortran and asm.

It means that they ARE keywords. Which is VALID code, for some implementations of C. God you are retarded. Go back to scripting.

Name: Anonymous 2012-01-11 14:43

>>93
You don't understand, if it's undefined then you can't guarantee that it will produce the same code every time.

Name: Anonymous 2012-01-11 14:44

>>97
Which is VALID code, for some implementations of C.

YOU ENJOYING THAT UNDEFINED BEHAVIOR?

Name: Anonymous 2012-01-11 14:44

Name: Anonymous 2012-01-11 14:45

>>99
USE THE COMPILER IN OP. God how autistic are you?

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

>>95
I just searched for the phrase "__asm" in ANSI/ISO C9899. Do you know many matches I found? None. Zip. Zero.

Name: Anonymous 2012-01-11 14:45

>>97
[quote]Some implementations reserve the words fortran and asm.
[/quote]

__asm != asm

Name: Anonymous 2012-01-11 14:45

>>100
Still isn't valid C code you dumb shit.

Name: Anonymous 2012-01-11 14:46

>>98
Of course you can.

Name: Anonymous 2012-01-11 14:46

>>100
asm, __asm__
__asm is not defined in that article, try again.

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

Hey genius, did you notice the part of the doc that read "C extensions"? In other words, it's an extension of standard C.

Name: Anonymous 2012-01-11 14:48

>>101
USE THE COMPILER IN OP. God how autistic are you?

I don't use non-standard compilers.

Please take your shitty toy language subset of C back to the imageboards you dumb fuck.

Next time you come to /prog/ expecting us to do something for you don't fucking make obscure code that requires the download of a non-standard tool, We are not fucking hipsters that act like hackers all day by using undefined shitty code.

Name: Anonymous 2012-01-11 14:49

>>105
You can guarantee that GCC won't rely on undefined behavior to compile undefined behavior?

Name: Anonymous 2012-01-11 14:50

>>108
| I don't use non-standard compilers.

So why are you even replying?

| Please take your shitty toy language subset of C

herp derp

| that requires the download of a non-standard tool

GCC is non-standard, wow. Just wow.

Name: Anonymous 2012-01-11 14:51

>>107
I never claimed it to be non-standard. But the faggots here are all herp derp its non-standard derp instead of just not responding. Fucking obsessive-compulsive disorder

Name: Anonymous 2012-01-11 14:52

>can't add/remove __
>go apeshit insane
Fucking /prog/

Name: Anonymous 2012-01-11 14:53

>>100
This does not define __asm, it only defines asm and __asm__.

Name: Anonymous 2012-01-11 14:53

>>110
You think gnu89 which is the default for Gcc is standard now?
You think GCC extensions are standard?

Name: Anonymous 2012-01-11 14:56

>>113
Beyond this, even if __asm was asm in your example your usage of it is undefined as it has side effects.

Name: Anonymous 2012-01-11 14:56

>>114
I'm telling you to use gcc for fuck sake. And you wont, so okay. GTFO then.

Name: Anonymous 2012-01-11 14:59


[ Wed Jan 11 02:52:07 ]
[ @ ~/fhost/prog/C ] $ cat nstd.c
// 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;
}
[ Wed Jan 11 02:52:09 ]
[ @ ~/fhost/prog/C ] $ gcc -O0 -m32 nstd.c
nstd.c: In function ‘g’:
nstd.c:49: warning: cast from pointer to integer of different size
nstd.c:56: warning: cast from pointer to integer of different size
nstd.c: In function ‘h’:
nstd.c:62: warning: cast to pointer from integer of different size
nstd.c: In function ‘sh’:
nstd.c:70: warning: cast to pointer from integer of different size
nstd.c: In function ‘main’:
nstd.c:98: warning: cast to pointer from integer of different size
nstd.c:107: warning: cast to pointer from integer of different size
[ Wed Jan 11 02:55:03 ]
[ @ ~/fhost/prog/C ] $ ./a.out
Segmentation fault



SEGMENTATION FAULT

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-11 15:02

>>117
And the truth shall set you free...

Name: Anonymous 2012-01-11 15:02

Dirty dumb non-standard scum.

Name: Anonymous 2012-01-11 15:02

>>117
| -m32
| cast from pointer to integer of different size

Yeah, no. 0 / 10 trolling attempt

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