{"id":779,"date":"2020-06-21T10:57:00","date_gmt":"2020-06-21T16:57:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=779"},"modified":"2020-06-12T11:17:07","modified_gmt":"2020-06-12T17:17:07","slug":"ptrs-and-memoryblocks-oh-my","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/06\/21\/ptrs-and-memoryblocks-oh-my\/","title":{"rendered":"Ptrs. And Memoryblocks. Oh My !"},"content":{"rendered":"\n<p>Sure I badly paraphrased the line from Wizard of Oz but this isn&#8217;t a word junkies blog. Or a Word\u00a9 junkies blog either \ud83d\ude42<\/p>\n\n\n\n<p>Stupid tech jokes aside this one is about Ptr and Memoryblock and their interesting relationship.<\/p>\n\n\n\n<p>In Xojo a memory block is just that &#8211; a chunk of memory acquired in one of many ways, that usually can be manipulated in lots of ways. They can be really useful for all kinds of tasks like dealing with byte oriented data (files, binary streams) as well as more high level things like manipulating UTF-8 data since at the very lowest level UTF-8 is just special runs of bytes that we interpret as &#8220;characters&#8221; or &#8220;text&#8221;.<\/p>\n\n\n\n<p>But then, everything in a program is JUST runs of bytes that are interpreted in special ways depending on what the bytes are, whether they happen to be &#8220;data&#8221; or &#8220;code&#8221;.<\/p>\n\n\n\n<p>And this is where things get interesting.<\/p>\n\n\n\n<p>A recent post on the forums did <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim p as ptr = d \/\/d is a delegate like \"AddressOf methodName\"\ndim mb as MemoryBlock = p\ndim id as string = Crypto.SHA256(mb)<\/code><\/pre>\n\n\n\n<p>So exactly whats going on here and why might this be a problem and why might it cause a crash ?<\/p>\n\n\n\n<p>AddressOf returns, as it states, the in memory address of whatever method you ask for. And this address is a ptr &#8211; a specific place in the running instance as loaded at this time which _could_ be different next time you run the application. This tends to be an OS level security measure.<\/p>\n\n\n\n<p>Memory blocks CAN be created from a Ptr. Usually this is so you can use it with a declare that creates and allocates some memory and returns a pointer to that allocated chunk of memory. But, a pointer is a pointer is a pointer. So it really doesn&#8217;t matter where that pointer came from. And with this particular pointer <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim mb as MemoryBlock = p<\/code><\/pre>\n\n\n\n<p>being the address of some method, or executable code, it just happens to be in our program and its memory space. But it is relevant that is its executable code.<\/p>\n\n\n\n<p>And then when this code tries to access that ptr it tries to READ the code, not just the ptr, since the base address of the memory block is just where &#8220;the bytes that make up thi memoryblock&#8221; are.<\/p>\n\n\n\n<p>Crypto may die for one of two reasons. First &#8211; the OS may bock you from reading the executable code this way. Secondly Crypto would need to know how much data to encrypt. And a memoryblock created from a Ptr doesn&#8217;t have a known size. So SHA256 may just try and read -1 byes (which is a HUGE unsigned number) and crash because of that.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>If you&#8217;re going to mess with Ptr&#8217;s &amp; memoryblocks make sure you know what toes you&#8217;re prepared to lose \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sure I badly paraphrased the line from Wizard of Oz but this isn&#8217;t a word junkies blog. Or a Word\u00a9 junkies blog either \ud83d\ude42 Stupid tech jokes aside this one is about Ptr and Memoryblock and their interesting relationship. In Xojo a memory block is just that &#8211; a chunk of memory acquired in one &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/06\/21\/ptrs-and-memoryblocks-oh-my\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ptrs. And Memoryblocks. Oh My !&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":780,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions\/780"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}