{"id":96055,"date":"2017-04-27T07:00:00","date_gmt":"2017-04-27T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96055"},"modified":"2019-03-13T01:10:42","modified_gmt":"2019-03-13T08:10:42","slug":"20170427-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170427-00\/?p=96055","title":{"rendered":"Static hooking through predefinition"},"content":{"rendered":"<p>A customer had a program that incorporated source code from two different third parties, let&#8217;s call them Contoso and LitWare. These libraries were originally written for Linux, and they are trying to port them to Windows. <\/p>\n<p>Contoso&#8217;s library implements some useful feature that they want to use. LitWare&#8217;s library implements some fancy memory management and wants to intercept all calls to <code>malloc<\/code>, <code>free<\/code>, and related functions. In particular, it wants to intercept the calls from Contoso. <\/p>\n<p>The customer knew that they could use Detours to do the intercepting, but that would require them to obtain a professional license, and the cost was a concern. <\/p>\n<p>Fortunately, since the customer is building all the libraries themselves, they can make changes to the code and recompile. <\/p>\n<p>I suggested using this header file: <\/p>\n<pre>\n\/\/ interceptable.h\nextern void* (*intercepted_malloc)(size_t);\n#define malloc interceptable_malloc\n\nextern void (*intercepted_free)(void*);\n#define free interceptable_free\n\n... repeat as necessary ...\n<\/pre>\n<p>Include this header file after <code>stdlib.h<\/code> so that all calls to the functions you care about are redirected to the <code>intercepted_...<\/code> wrappers. <\/p>\n<p>The implementation file is simple: <\/p>\n<pre>\n\/\/ interceptable.c\n#undef malloc\nvoid* (*intercepted_malloc)(size_t) = malloc;\n\n#undef free\nvoid (*intercepted_free)(void*) = free;\n\n... repeat as necessary ...\n<\/pre>\n<p>When the LitWare library wants to intercept the functions of interest, it does this: <\/p>\n<pre>\nvoid* (*original_malloc)(size_t);\n\nvoid* replacement_malloc(size_t size)\n{\n ... replacement can call original_malloc() ...\n}\n\nvoid install_malloc_wrapper()\n{\n  original_malloc = intercepted_malloc;\n  intercepted_malloc = replacement_malloc;\n}\n<\/pre>\n<p>Now, when the Contoso library calls <code>intercepted_malloc<\/code>, it ends up calling <code>replacement_malloc<\/code>, which can do whatever it wants (including calling the original <code>malloc<\/code>). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating your own hook points.<\/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":[25],"class_list":["post-96055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Creating your own hook points.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96055","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=96055"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96055\/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=96055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}