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

Pages: 1-

Is this valid C code?

Name: Anonymous 2017-01-02 2:13

char x [5] = { 0xf0, 0x0f, 0xc7, 0xc8 };

main ()
{
void (*f)() = x;
f();
}


Found this on the Everything2 page for the Pentium F00F bug. My question is essentially whether it is indeed valid to write machine code subroutines as byte arrays and call them via a function pointer. Does the Standard have anything to say about this? And is executing code in the data segment even possible on modern architectures?

Name: Anonymous 2017-01-02 7:02

the Standard
U MENA DA STANDARD

Name: L. A. Calculus 2017-01-02 8:36

>>2
WADDA YA WANT, SUSSMAN?

Name: Anonymous 2017-01-02 9:09

obviously this is undefined behavior and not part of the C standard.

It probably only works with gcc and I doubt even gcc specifies that this should work.. but it is a kind of common trick.

Name: Anonymous 2017-01-02 14:59

>>1
I'm not sure if C standard even considers the option of writing pure machine code as a char* and then treating the address as a function pointer, but having such an array act like a function written in machine code is a sane option - after all, you just allocate bytes and move your instruction pointer to their address.

as for whether it's possible to run code from the data section - it is because the processor does not know anything about data sections, it's a feature of executable file formats. what the processor knows is whether the area of memory is executable or not, and while stuff placed in data section will get marked as non-exectuable when running a binary, you can change that with syscalls (mprotect() on posix, VirtualProtect() on windows).

so the specific code in your example will probably segfault (disregard segfaults or sigills that will happen later due to lack of return instruction as it will attempt to execute junk data after f00f fails to cause a hardware error on a modern CPU) unless the compiler recognizes the construct and acts accordingly, but you can make it work with a single line of code.

>>4
what about this is gcc-specific? it's probably undefined but the most straightforward behavior for any compiler would be to invoke a call or equivalent instruction on the array address (because array = pointer)

Name: Anonymous 2017-01-02 15:01

>>5
array = pointer
You don't know C.

Name: Anonymous 2017-01-02 21:40

Name: Anonymous 2017-01-02 21:55

>>6
Array = pointer. prove me wrong.

Name: Anonymous 2017-01-02 21:58

Name: Anonymous 2017-01-03 7:54

>>6,9
so you're being pedantic about corner cases that don't apply to the example discussed in this thread simply for the sake of being pedantic? you're the programming equivalent of that guy who complains how your URL is not actually a valid URL but a URI. which is the networking equivalent of that guy who complains about sound in space in science fiction movies.

Name: Anonymous 2017-01-03 9:55

πŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡Έ
I claim these dubz in the name of the United States of America
πŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡Έ

Name: Anonymous 2017-01-03 10:04

>>10
Arrays are not pointers, period.
And Star Wars is shit.

Name: Anonymous 2017-01-03 10:25

>>12
if you want to be pedantic, arrays are areas of memory and their names are pointers to the first element. also, Star Wars is not shit

Name: Anonymous 2017-01-03 10:39

>>13
No, their names are not pointers, which you would realize if you visited >>9's second link.

Star Wars is childish shit with a primitive plot and sounds in fucking space. Read up on Dune instead of this overrated crap.

Name: Anonymous 2017-01-03 10:50

>>14
childish shit with a primitive plot and sounds in fucking space
it's not a defect, it's a feature.
Read up on Dune instead of this overrated crap.
I actually read most of the books in the series. none were as good as the first one though. but if you're bothered by sound in space then why aren't you bothered by space travel drugs made from shit of a huge worm that can live only on a single planet? it's not hard sci-fi either.

Name: Anonymous 2017-01-03 11:32

>>15
it's not a defect, it's a feature.
Said every loser ever.

it's not hard sci-fi either.
It's creative and unique, not plain illiterate like sounds in space.

Name: Anonymous 2017-01-03 12:30

It's creative and unique, not plain illiterate like sounds in space.
sounds in space is a convention, complaining it is as pointless as complaining about soundtracks because hey, in real life there's never a mood-appropriate music playing from nowhere in particular (inb4 dogme95 did exactly that). Dune is creative and unique but if you examine it pedantically by pointing out plot holes or inconsistencies with actual science, it will fall apart. the takeaway from that is that unless your story aims specifically for realism, nitpicking minor details is pointless because those minor details are not why the story is good or bad and if you complain about them, you're the kind of insufferable asshole who insists on arrays not being pointers.

Name: Anonymous 2017-01-03 13:04

>>15
but if you're bothered by sound in space then why aren't you bothered by space travel drugs made from shit of a huge worm that can live only on a single planet?

Because those things actually have a coherent explanation in-universe, rather than just being thrown in with little concern for consistency or logic. (Technically, there actually is a logical in-universe explanation for sound in space in Star Wars, but it's only revealed in the expanded universe.)

Name: Anonymous 2017-01-03 13:24

>>17
sounds in space is a convention
Convention? By whom? Shitty movie makers? What else do you think is a convention, laser cannons and plasma blasters in space? Real sci-fi novels have real space weapons like railguns, artillery and cluster bombs.

Name: Anonymous 2017-01-03 14:10

>>19
Unfortunately that what public expect. A realistic space narrative would be boring and slow. People want action, explosions and shiny starships rolling plasma beams and lazers.
Its the equivalent of dragons and fairies in sci-fi.

Name: Anonymous 2017-01-03 14:23

>>20
The public also expect that arrays in C are the same as pointers, but that doesn't make them right.

Name: Anonymous 2017-01-03 14:33

πŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡Έ
I claim these dubz in the name of the United States of America
πŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡ΈπŸ‡ΊπŸ‡Έ

Name: Anonymous 2017-01-03 15:07

>>22
You racist heterogenous bigot!!!

Name: Anonymous 2017-01-07 5:22

You can still do it if you make the page executable.
#include <windows.h>
#include <stdio.h>
#define BYTE unsigned char

int main(int argc, char ** argv){
const BYTE testcode[] = {0xB8, 0xEF, 0xBE, 0xAD, 0xDE, 0xC3,
0xB8, 0xBE, 0xBA, 0xFE, 0xCA, 0xC3,
0xB8, 0xDE, 0xC0, 0xD0, 0x9E, 0xC3,
0xB8, 0xED, 0xDA, 0x5C, 0xCA, 0xC3};
const int sections = 4;

int pagesize = 0, allocation = 0;
int (*testcodefunc)() = NULL;
BYTE* codemem = NULL;
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);

pagesize = sysinfo.dwPageSize;
allocation = sysinfo.dwPageSize;
codemem = VirtualAlloc(NULL, allocation, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
VirtualAlloc(codemem, sysinfo.dwPageSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

for(int i = 0; i < sizeof(testcode); i++) codemem[i] = testcode[i];

for(int i = 0; i < sections; i++)
printf("%X\n", ((int (*)())codemem + (i * sizeof(testcode) / 4))());

return 0;
}

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