Skip to content

Instantly share code, notes, and snippets.

@Totktonada
Created April 16, 2020 16:01
Show Gist options
  • Save Totktonada/9896c4fb35fa976e18e10c3314a6fd44 to your computer and use it in GitHub Desktop.
Save Totktonada/9896c4fb35fa976e18e10c3314a6fd44 to your computer and use it in GitHub Desktop.
Verify out-of-bound accesses with region.c buffers
static const uint64_t watermark_before = 0x74f31d37285c4c37;
static const uint64_t watermark_after = 0xb10269a05bf10c29;
static void *
region_alloc_with_watermark(struct region *region, size_t size)
{
size_t real_size = size + 2 * sizeof(uint64_t);
void *real_buf = region_alloc(region, real_size);
if (real_buf == NULL)
return real_buf;
*(uint64_t *)real_buf = watermark_before;
*((uint64_t *)((char *)real_buf + real_size) - 1) = watermark_after;
return (char *)real_buf + sizeof(uint64_t);
}
static void
region_check_watermark(void *buf, size_t size)
{
void *real_buf = (char *)buf - sizeof(uint64_t);
size_t real_size = size + 2 * sizeof(uint64_t);
assert(*(uint64_t *)real_buf == watermark_before);
assert(*((uint64_t *)((char *)real_buf + real_size) - 1) ==
watermark_after);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment