What is the range of illegle memory addresses?

Xi Ruoyao xry111@mengyan1223.wang
Tue May 18 16:20:26 GMT 2021


On Tue, 2021-05-18 at 10:55 -0500, Peng Yu via Gcc-help wrote:
> Hi,
> 
> NULL (0) is a commonly used value for an invalid memory address. I
> also see -1 is used for an illegal memory address.
> 
> Besides those values, I'd suspect that other values that are close to
> 0 (both negatives and positives) won't appear in real programs.
> 
> Is there a limit on the safe range that can be assumed to not be used
> by user level code as valid addresses?

Using a value from dereferencing NULL is always undefined behavior. 
Otherwise, it depends on platform, OS, and system configuration.

For example, on x86_64 Linux with 4-level page table [1] the userspace
address space is 47-bit.   So in non-kernel code any pointers >= 2^{47}
can be considered invalid.

For another example, on 32-bit x86 Linux with KPTI enabled, a valid
userspace pointer can be very close to 0xFFFFFFFF.  I'm not sure how
close, but I've observed some problem [2] caused by this.

Again, this question is off-topic.  I can't see how this is related to
GCC anyway.

[1]: https://www.kernel.org/doc/html/latest/x86/x86_64/mm.html
[2]: https://github.com/python/cpython/pull/13205
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list