{"id":38183,"date":"2004-08-12T07:00:00","date_gmt":"2004-08-12T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/08\/12\/myth-the-3gb-switch-expands-the-user-mode-address-space-of-all-programs\/"},"modified":"2004-08-12T07:00:00","modified_gmt":"2004-08-12T07:00:00","slug":"myth-the-3gb-switch-expands-the-user-mode-address-space-of-all-programs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040812-00\/?p=38183","title":{"rendered":"Myth: The \/3GB switch expands the user-mode address space of all programs"},"content":{"rendered":"<p>\nOnly programs marked as \/LARGEADDRESSAWARE are affected.\n<\/p>\n<p>\nFor compatibility reasons, only programs that explicitly\nindicate that they are prepared to handle a virtual address space\nlarger than 2GB will get the larger virtual address space.\nUnmarked programs get the normal 2GB virtual address space,\nand the address space between 2GB and 3GB goes unused.\n<\/p>\n<p>\nWhy?\n<\/p>\n<p>\nBecause far too many programs assume that the high bit of user-mode\nvirtual addresses is always clear, often unwittingly.\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/memory\/base\/4gt_ram_tuning.asp\">\nMSDN has a page listing some of the ways programs make this\nassumption<\/a>.\nOne such assumption you may be making is taking\nthe midpoint between two pointers by using the formula\n(a+b)\/2.\n<a HREF=\"\/oldnewthing\/archive\/2003\/09\/17\/54945.aspx\">\nAs I noted in a previous exercise<\/a>,\nthis is subject to integer overflow and consequently can result\nin an erroneous pointer computation.\nConsequently, you can&#8217;t just take an existing program that you didn&#8217;t\nwrite, mark it \/LARGEADDRESSAWARE, and declare your job done.\nYou have to check with the authors of that program that they verified\nthat their code does not make any 2GB assumptions.\n(And the fact that the authors\ndidn&#8217;t mark their program as 3GB-compatible\nstrongly suggests that no such verification has occurred.\nIf it had, they would have marked the program \/LARGEADDRESSAWARE!)\n<\/p>\n<p>\nMarking your program \/LARGEADDRESSAWARE indicates to the operating\nsystem, &#8220;Go ahead and give this program access to that extra\ngigabyte of user-mode address space,&#8221;\nand as a result, addresses in the third gigabyte\nbecome possible return values from memory allocation functions.\nIf you set\n<a HREF=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=28166\">\nthe &#8220;Top down&#8221; flag in the\nmemory manager allocation preferences mask<\/a>\n(search for &#8220;top down&#8221;),\nyou can instruct the memory manager to allocate high-address memory\nfirst, thereby forcing your program to deal with those addresses\nsooner than it normally would.\nThis is very handy when testing your program in a \/3GB configuration\nsince it forces the troublesome memory addresses to be used sooner\nthan normal.\n<\/p>\n<p>\n<b>Exercise<\/b>: Find the bug in the following function.\nHint: What&#8217;s today&#8217;s topic?\n<\/p>\n<pre>\n#define BUFFER_SIZE 32768\nBOOL  IsPointerInsideBuffer(const BYTE *p, const BYTE *buffer)\n{\n  return p &gt;= buffer &amp;&amp; p - buffer &lt; BUFFER_SIZE;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Only programs marked as \/LARGEADDRESSAWARE are affected. For compatibility reasons, only programs that explicitly indicate that they are prepared to handle a virtual address space larger than 2GB will get the larger virtual address space. Unmarked programs get the normal 2GB virtual address space, and the address space between 2GB and 3GB goes unused. Why? [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-38183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Only programs marked as \/LARGEADDRESSAWARE are affected. For compatibility reasons, only programs that explicitly indicate that they are prepared to handle a virtual address space larger than 2GB will get the larger virtual address space. Unmarked programs get the normal 2GB virtual address space, and the address space between 2GB and 3GB goes unused. Why? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=38183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=38183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}