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?
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:
Anonymous2017-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)
>>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:
Anonymous2017-01-03 9:55
πΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈ I claim these dubz in the name of the United States of America πΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈ
Name:
Anonymous2017-01-03 10:04
>>10 Arrays are not pointers, period. And Star Wars is shit.
Name:
Anonymous2017-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:
Anonymous2017-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.
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.
It's creative and unique, not plain illiterate like sounds in space.
Name:
Anonymous2017-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.
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.)
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:
Anonymous2017-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:
Anonymous2017-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:
Anonymous2017-01-03 14:33
πΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈ I claim these dubz in the name of the United States of America πΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈπΊπΈ