Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New define in mem.c DEBUG_USE_AFTER_FREE_ON_WINDOWS which helps catch use-after-free issues in a debugger on Windows #737

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

#include <stdlib.h>

// Enable this to catch use-after-free issues when debugging on Windows.
// #define DEBUG_USE_AFTER_FREE_ON_WINDOWS

#if !defined(DEBUG_USE_AFTER_FREE_ON_WINDOWS)

// The standard implementation, using malloc() and free().

void * avifAlloc(size_t size)
{
void * out = malloc(size);
Expand All @@ -18,3 +25,72 @@ void avifFree(void * p)
{
free(p);
}

#else

// This implementation rounds up all memory allocations to the nearest 4k (page size), allocates
// using VirtualAlloc(), and then records the allocation in a linked list. When avifFree is called,
// instead of freeing the memory, it simply revokes all read and write access to that region
// forever. If any use-after-free issues are discovered (via fuzzing or the like), enabling this
// should immediately catch it in a debugger right when it happens.

#include <stdio.h>
#include <windows.h>

typedef struct Allocation
{
void * ptr;
size_t originalSize;
size_t size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: If I understand the MSDN pages for VirtualAlloc() and VirtualProtect() correctly, we can just pass the original size to these two functions and they will round the size up to the next page boundary for us.

struct Allocation * next;
} Allocation;

static Allocation * allocations = NULL;

void * avifAlloc(size_t size)
{
size_t originalSize = size;
size = (size + 4095) & ~(4095);

void * out = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
if (out == NULL) {
abort();
}

Allocation * a = (Allocation *)malloc(sizeof(Allocation));
a->ptr = out;
a->originalSize = originalSize;
a->size = size;
a->next = allocations;
allocations = a;

// printf("Alloc %p %zu (%zu)\n", a->ptr, a->originalSize, a->size);
return out;
}

void avifFree(void * p)
{
if (!p) {
return;
}

Allocation * a = allocations;
for (; a != NULL; a = a->next) {
if (a->ptr == p) {
break;
}
}

if (!a) {
abort();
}

DWORD old;
if (!VirtualProtect(a->ptr, a->size, PAGE_NOACCESS, &old)) {
abort();
}

// printf("Free %p %zu (%zu)\n", a->ptr, a->originalSize, a->size);
}

#endif