118 points · by rramadass · 3y ago · 189 comments
I would probably use something like this when hacking on an Apple II with a zero page.
I have also written programs that ran on VMS which dumped memory starting at '0' without getting any segfaults.
Turbo-C used to check the zero location to see if it had changed and would issue a warning if so (in the real mode days).
[0] My other computer is a PDP-11,
Consider how a surgeon would respond if told not to use a scalpel because of the risk of accidental injury when using a sharp tool.
We learn from our mistakes - to which I'd add, sometimes we can afford to make mistakes (home programs) and other times we can't (safety-critical code).
This is significant because the following two snippets of code are not required to be equal in C.
char* c = (char*)(0);
char* d;
memset(&d, 0, sizeof(d));
c == d; // this does not have to be true
That's what the spec says, but are there any computers around anymore where nullptr is not zero? The spec should really go with the times IMHO, old hardware can be supported via platform-specific language extensions.
> Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.
And it isn't just zero anyway because there's segmentation in play.
Yeah that's absolutely fair. I actually can't think of a single platform, embedded or otherwise, old or new, where the null pointer is not address 0.
[]
*()++
Technically, can only do address "1" with first bit set as a valid, usable address on hardware allowing addressing smaller than 8 bits (standard hardware epsilon factor).So really, address one is literally address 0 + epsilon, where epsilon is minimal addressable bit group, typically 8 bit clean without seg fault.
Although, for standard x86, epsilon size would depend on which ring/boot method level & asm addressing method available (8,16,32,64).
That's also totally valid on WebAssembly btw, address 0 is a regular read/write location.
Yes, I said if you had registers--if you were cheap you could buy your PDP-10 without registers and all the instructions that referenced registers would use core instead.
Since the registers were in effect just 16 words of fast semiconductor memory overlaying the first 16 words of slow core memory you could do anything in them that you could do in regular memory, including running code out of them.
If you had a small loop that did a lot of iterations you could sometimes get a significant speed boost by copying the loop into the registers and running it there.
Originaly using anonymous unions & placing largest bit count variable as first item in union was the context needed to align the "char" correctly -- vs. casting.
Technically, can point at anything in an unaligned manner, just need to align access to machines address boundary to use a "fetch the value of a complete valid memory alignment address". One can hand roll the appropriate shifts/masking to get at the set of bits loaded (which usually let compiler do). Awk and settable end of line marker good way to safely visualize this.
Today you can still work with (say) TI DSP chips that spit complex FFT pipelines results once per cycle and have absolutely no 8-bit hardware addressing or masking abilities as they're lean mean RISC optimised machines for pure 32 or 64 bit floating point operations.
They have C compilers and CHAR_BIT==32 (or 64).
#include <stdio.h>
int main() {
for (int x = 0;;x++) {
putc(*(char*)x);
}
return 0;
}
I was mezmerized by the strings from the uncompressed BIOS ROM being dumped back at me, "American Megatrends".. etc. Eventually this process crashes of course, when it runs past the end of mapped memory locations.Then came the realization you could alter any memory location, and further, you could write a tiny TSR to do things...
What were you trying to do and how did this work? I am interested in understanding the interplay between -funwind-tables, -fasyncronous-unwind-tables and -fexceptions.
I had an inline asm statement that could throw exceptions (by indirectly calling exception throwing functions). GCC assumes that asm cannot throw, and the function the statement was in did not have any throw statement nor call any other potentially throwing functions nor any non-optimized out memory access, so the compiler omitted generating unwind tables for the function even with -fasync-unwind-tables.
The result was that throwing from the asm would at best not call destructors, at worse, crash with a corrupted stack.
By adding a non-optimizable dummy memory access right after the asm (and jumping over it from the asm, so it actually doesn't get executed), I forced GCC to generate the unwind info . I also had to make sure that no compensation code was needed for the asm statements themselves, but after that things worked out fine.
I wouldn't really use this in a finished product, it is very fragile and just happened to work on the version of GCC I was using. The right solution would be for GCC to add an attribute to mark asm statements as potentially throwing.
I had long wanted to understand the interplay between -funwind-tables, -fasynchronous-unwind-tables and -fexceptions since the last is language specific but the first two are not. GCC docs are not of much help in understanding what exactly is going on in each case so i guess i need to do some research and experimentation.
The point is the journey and the discussion around what the line of code does, and how that discussion is valuable. The point isn't to tell us what the code itself does.
do you mean the browsing plugin, or using base GPT-4?
Anyhow it's just barely possible that this assignment was a hack to make this sort of code work on a machine where address 0 is (uninitialized) nonzero and can be harmlessly made zero.
https://docstore.mik.ua/manuals/hp-ux/en/B2355-60130/chatr_p...
Wonderful fun when they shipped a Kerberos library that unconditionally dereferenced a potentially null optional pointer-to-struct, relying on the executable to be set in the “null tolerant” mode!
You can relocate the vector table and to block it off with the MPU if you have one and memory to spare.
Certain higher ups determined that behavior was a business requirement when I refused to reimplement it on a new platform.
This is writing sizeof(char) (== 1 almost everywhere) zero to address zero. It is not using a NULL macro or other predefined symbol.
In the real world, this would generally write a byte to address 0000:0000, leading to UB because it would fuck up the divide-by-zero IV.
PS: I used Borland C++ 3.1, Microsoft C++ 3.x and 4.5x, Watcom, and early GNU.
https://c-faq.com/null/null2.html
https://c-faq.com/null/machexamp.html
Actual ways to do what you want to do are described in
https://c-faq.com/null/accessloc0.html
but technically speaking the pointer with a constant zero assigned to it _is_ a null pointer (which can be implemented as whatever bit pattern), independent of the preprocessor macro.
Here in godbolt, clang compiling C simply deletes the code in the function past and including the null pointer dereference.
https://godbolt.org/z/9aqWPazsP
> This is writing sizeof(char) (== 1 almost everywhere)
1 everywhere. sizeof's unit is "how many chars". For instance there was a cray machine that could only access 64bit words. sizeof(char) is still 1, with 64bit chars.
> zero to address zero. It is not using a NULL macro or other predefined symbol.
NULL is defined as literal 0.
sizeof char is 1 by definition everywhere.
/pedantic
The literal 0 is treated specially, so this could indeed be one of those 'turns into a weird bit pattern NULL pointers', if such a thing existed in the wild anymore.
But you're correct in that there probably haven't been any since the turn of the century or whenever the last Univac mainframes got turned off.
But yes, the interrupt table was my first thought when reading the headline.
A byte is CHAR_BIT bits, where CHAR_BIT >= 8. (It's exactly 8 on most implementations; DSPs are the most common exception).
short and int are both required to be at least 16 bits wide. It's possible for int to be 1 byte (sizeof (int) == 1), but only if CHAR_BIT >= 16.
As for why it's address 0, well, it has to go somewhere, every machine has a CPU so everyone needs an interrupt table even if they don't have much memory. And when memory was precious there was no sense wasting even one byte of it; 0 was a real address on your physical memory chip, so why not use it just like any other?
(The fact that it's "address 0" for "division by 0" is just coincidence as far as I can see; division by 0 just happens to be the first kind of possible CPU interrupt. Perhaps it was the most common one?)
From the numerous responses here, it's clear that people interpret my question as about how the hardware itself works, which isn't at all what I was asking about; I'm aware of how stuff like this works at the assembly level, but my understanding was that in C and C++, trying to write arbitrarily to "special" addresses like that would be considered undefined behavior (often resulting in segfaults). When I read the comment I responded to above, it surprised me, so I wanted to check whether I understood what was said correctly. It's honestly kind of confusing to me that so many people seem very upset by the idea that a stranger on the internet might have a misconception about how hardware abstractions are exposed via compiled code to the point that they feel the need to explain in detail how hardware works but not actually answer the question I asked.
The address can be changed with the LIDT instruction and operating systems nowadays will just put it wherever, but for backward compatibility it is expected to still be at 0000:0000 (not sure how this is handled nowadays in UEFI, but it should still be possible t o set it up that way).
And yes, some addresses are special. (AFAIK, on all current mainstream architectures.) This is the expected way to set those signal handlers, output (and input) data, configure devices, etc.
That said, there are some gotchas on using specific addresses in C. AFAIK none apply to x86, but it's something you usually do in assembly.
There are hundreds of instances of (char)0=0; in github, and there are none of the bit_cast variant, so if your goal is to inform people how to read code, starting with the one humans might actually encounter in the wild makes sense.
Not true. It's illegal to execute that statement because of undefined behavior. But it's legal to have: if (false) { *(char*)0 = 0; }
(And of course it's legal to #ifdef it out or comment it out, but that's too much cheating.)
Reference if you don’t get the joke: https://www.reddit.com/r/shittyprogramming/comments/3bmszo/t...
Until accessed, a memory location & variable location can be expressed as an indefinate integral.
Memory location access request implies definate integral.
1) evaluation value indefinate of integrand variables: "A" less than/equal to "B" less than/equal to "C" less than/equal to "D". Variable a is start of sequence. Variable D is end of sequence.
2) integrand variables D & A define the continuous range of PC group of bits
3) integrand variables C & B define the cohntinous range of 8 bit char
4) Accessing bits outside the integrand limits implies math equivalent of computer segfault.
In order to not generate a segfault, only positive delta, and epsilon is 0 or 1. aka The "alignment request" implies that A MUST be equal to B; C less than or equal to D.
K&R C short hand for above is anonymous union.[0]
Physics / Mechanical Engineering center of gravity approach rotated 90 & implimented via punch card reader that doesn't skip cards much more interesting physical list processing visual than calculus number line (IMHO). Physical form bit to bulky to carry to/from class room. Fortunately, electrial engineering split the difference via 1/2 way point (45 degree angle). Computer scientests get the autonoma / semantic analysis difference. Software Engineers, by completing the circle with & with out a sigh of "e e e e e e ..."
Wait for C++ coder to get disney sponsorship for a stack frame version of/translation to George Lucas expressions / Steven Speilburg expressions.
aka * wars vs. * trek crossover comparisons, perhaps with less () than lisp/scheme take.
Ideally, in UTF format so no "7 samuri/8 bit kleen", AND/OR, lower API, protocol droids distractions.
In interm, learn python, show a python version of beetles 'let it be' and 'eigen a feeling'[1] parody.
Although, perhaps 'irq feeling' more on topic with an appropriate reference to irq hooks & NULL dumps.
Perhaps get DMCA'd by Wierd Al for using wrong character color scheme.
".plan 0[]", long[sigh] different discussion?
[0] :
sigh[long] : http://news.ycombinator.com/item?id=3367392"Eigen Maze by this pointer" may be theme song for this post; but no letting things be in order to let this stack down to get this pointer back to complete 'init & kill -9' autonoma sequence.
What's passed in there? Some googling let me know it's used on macOS, but every result was too generic to be helpful.
(obligatory in C and OS theory, other languages might differ)
Note: think there's a way to convert this to some shorter, massively recursive C pointer, to pointer .... to pointer declaration explaination.
Ideally after the appropriate cast of characters has had a no-fault OS page(s) performance.
That, of course, would be part of the talk: that this is perhaps an OS-specific feature that the original coder was maybe trying to trigger, and that it is still OS-dependent.
[1]: https://en.wikipedia.org/wiki/Zero_page states this to be the case, but mmap(2) seems to disagree / suggests 0 is mappable.
Pointers and type safety still causing issues decades later.
I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again.
Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the value at that address, you'll get a non-zero and then run code you didn't intend to run. By defining address 0x0 as value 0, you'll avoid this issue. That has two problems.
Alternatively, you define address 0x0 to erase the initial stack pointer so that a nefarious actor can't somehow memory dump and then find out where the program starts its execution. I don't see the benefit for several reasons.
-----
Scenario 1.
The first problem is that you shouldn't write code poorly. If you meant to check a variable's address and instead check its value, that's on you, your test suite, your compiler, and your debugger (and by extension, your colleagues and educators).
Second, writing to address 0x0 on a Cortex usually involves writing to flash... which is not a temporary/nonpersistent change, more involved than simply writing to an address (you usually need to enable the flash controller and set some peripheral registers), and would usually not be allowed on a production device because that's where the bootloader is or that section of memory has been write protected following good practices.
Scenario 2.
The more I think about this one, the less it makes sense. Anyone with debug port access or a memory dump is already inside the castle, well past any defenses e.g. zeroing the main stack pointer. Plus, the same caveats from Scenario 1 apply: if you COULD write to 0x0, it is almost always a terrible idea.
-----
Grand takeaway? This is probably a talk relevant to PC and not embedded. A Cortex-M would definitely complain about this code. Even on an AVR (atmega) changing 0x0 would be changing R0. I don't even know if that register is directly writeable during execution---probably because it's how you set some bootloader bits---but it's definitely dangerous.
With that in mind, I searched for "virtual address space windows" to figure out what goes in an executable at address 0x0. After reading Microsoft's first two articles, it's still unclear what goes in 0x0. Wikipedia? No answer. Next result? Nothing.
Finally, a page about Windows ME/98/95 states address 0x0 is "available to the process" but is "not writable".
After ten more minutes of fruitless searching, I'm willing to just test what happens, because I expect that's not even writable without side effects during execution.
#include <iostream>
int main() {
*(char*)0 = 0;
std::cout << "test" << std::endl;
}
and then, "g++ test.cpp -o test", and ...[1] 869 segmentation fault ./test
Linux? Fail.
-----
"cl test.cpp /link /out:test.exe"...
This compiles and runs but does not display the test string. (If you remove the 0x0 assignment, the string displays; no surprise)
Interestingly, I can't delete test.exe right away. It has stayed open:
ERROR: The process with PID 12776 (child process of PID 19728) could not be terminated.
Not only does PID 19728 not exist, I have to use an administrator console to "taskkill test.exe", so this would probably never belong inside user space code on Windows.
-----
Thus, this short code snippet seems like trouble, and the answer I would hope for as an interviewer is, "yeah, I don't write code like that, sorry."
(Coincidentally, I would say the same in response to the professors giving microcontroller exam questions that try to stump students with multiple casts and dereferences in a single line *ahem Moreno*. Stop teaching students methods they will never, ever use in a shop that cares about its code!!)
Now I watch and learn.
For embedded, this might do something nonawful on a device with memory management e.g. Cortex-A. This exercise is left to the reader.
Moral of the talk? Most of computing is extremely complicated, and you can hand-wave and abstract much of it away unless it's directly in your field. In this case, that would be a compiler engineer or low-level kernel developer.
Humorous 'memory' explaination of various levels of 'memory'[1]
Without reviewing the video, looks like a coding artifact related to segment 64k memory of 8/16/32/64 bit dos/MS windows (vs. arm / sparc with flat address space).
far/near pointer delclaration would clarify things a bit.
This is a c language artifact. Using in C++, one would need to impliment/override the default C++ supplied memory management to avoid memory management gotchas.
The code example was a way to make sure that pointer variable on a non-unified memory architecture was initialized to point within a given memory segment.
aka Reference zero's out the non-unique bits and leaves the "memory reference bits for given memory segment" alone / segmented NULL.
Zeroing out the non-unique memory bits still allows one to make use of the upper address bits to find out where in memory the given
memory segment is (useful for implimentation of setjump(), figuring out machine byte order, 1st time in segment, etc.).
[0] : Expert C Programming, Deep C Secrets by Peter Weinberger
For all the gory details see Unwinding the Stack: Exploring How C++ Exceptions Work on Windows - https://www.youtube.com/watch?v=COEv2kq_Ht8
The code can do something shifty at run time, without a power surge, to the address reference before the address is de-referenced