Wireshark-dev: Re: [Wireshark-dev] Checking address in WMEM

From: Evan Huus <eapache@xxxxxxxxx>
Date: Thu, 26 Jan 2017 08:11:47 -0500
On Thu, Jan 26, 2017 at 4:06 AM, Dario Lombardo
<dario.lombardo.ml@xxxxxxxxx> wrote:
>
>
> On Wed, Jan 25, 2017 at 6:50 PM, Evan Huus <eapache@xxxxxxxxx> wrote:
>>
>> On my phone, but the short version is that there's no way to check this,
>> and no efficient way to build it.
>>
>> Evan
>>
>
> Looking into the code it seems to me that the routine of free_all should
> traverse all the allocated memory. I've written a code that mimics it, but
> I'm still failing to have all the addresses for some type conversions.
> That's the code I'm working on
>
> // Check if 'address' belongs to the given scope.
> gboolean
> wmem_check_allocator_block(wmem_allocator_t* allocator, void* address)
> {
>     wmem_block_allocator_t *private_allocator =
> (wmem_block_allocator_t*)allocator->private_data;
>     wmem_block_hdr_t       *cur;
>     wmem_block_chunk_t     *chunk;
>     void* a = WMEM_DATA_TO_CHUNK(address);
>
>     cur = private_allocator->block_list;
>
>     while (cur) {
>         chunk = WMEM_BLOCK_TO_CHUNK(cur);
>         if (a == chunk)
>             return TRUE;
>         cur = cur->next;
>     }
>     return FALSE;
> }
>
> If I allocate a block and the allocator gives me 7c40 as address, the
> function checks only 7c30. It looks like I'm casting/converting the
> addresses the wrong way.
> Any idea? Does this approach sound good, or am I completely following the
> wrong path?

Each block can consist of multiple chunks, so you need a second, inner
loop. You can do this with WMEM_CHUNK_NEXT.

Do note, however, that:
- you'll be iterating over every piece of memory allocated in this
scope, which will probably be quite expensive
- your code will fail any time wmem chooses a different allocator
(this happens in CI, and occasionally elsewhere as well)

What problem specifically are you trying to solve? There may be an easier way.

Evan