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.

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

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?

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