Re: Address range returned by malloc
site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com On Oct 4, 2008, at 3:29 PM, Terry Lambert wrote: unsigned int a = 4294967295; // 2^32-1 int b = (int)a; unsigned int c = (unsigned int)b; printf("%s\n", a == c ? "equal" : "not equal"); -- Terry _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... On Oct 4, 2008, at 12:56 PM, Greg <greg@kinostudios.com> wrote: Kernel virtual address space for a 32 bit kernel is limited to 32 bits. Pointers are NOT signed values, so do not store the returned pointer into a signed integer, or you may truncate it to 31 bits and overwrite/access something in lower memory than your allocation actually occurred when you go to use it. This would be Bad(tm). Thanks Terry, and I searched online and found that it appears that Apple will continue to use a 32-bit kernel (I hope). I'm aware that void* is unsigned, but I wasn't sure if converting that to a signed type and then back again would lose information. For example this program (with gcc -O0) displays "equal": So what's going on? It doesn't appear to lose any information here. The short answer is that if you do pointer math on an address above 2G in a signed it, it will subtract rather than add, and you will get the wrong address. Don't do that. The longer answer is that, as an architectural detail, Intel processors expect high bits not current supported to be set instead of cleared, so the compiler sign extends pointers on conversion from integsr before assignment. In both cases, you are getting luck because of the simplicity of your example. In any event, it doesn't matter if I say this because there are over 15,000 google hits on the information: google the terms: SnowLeopard kernel 64bit. You will be very unhappy if you use a 32 bit integer type to contain a kernel pointer in SnowLeopard. This email sent to site_archiver@lists.apple.com
participants (1)
-
Terry Lambert