DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Burakov, Anatoly" <anatoly.burakov@intel.com>
To: David Marchand <david.marchand@redhat.com>
Cc: dev <dev@dpdk.org>, "Mcnamara, John" <john.mcnamara@intel.com>,
	"Dmitry Kozlyuk" <dmitry.kozliuk@gmail.com>,
	dpdk stable <stable@dpdk.org>,
	"Xueqin Lin" <xueqin.lin@intel.com>
Subject: Re: [PATCH 2/3] mem: fix ASan shadow for remapped memory segments
Date: Wed, 27 Apr 2022 16:32:03 +0100	[thread overview]
Message-ID: <3d6e93d8-e9f6-6d69-c542-4e274e410a58@intel.com> (raw)
In-Reply-To: <36f82439-a251-d095-ca9b-c14fe4a32430@intel.com>

On 26-Apr-22 5:07 PM, Burakov, Anatoly wrote:
> On 26-Apr-22 3:15 PM, David Marchand wrote:
>> On Tue, Apr 26, 2022 at 2:54 PM Burakov, Anatoly
>> <anatoly.burakov@intel.com> wrote:
>>>>> @@ -1040,9 +1040,25 @@ malloc_heap_free(struct malloc_elem *elem)
>>>>>
>>>>>           rte_mcfg_mem_write_unlock();
>>>>>    free_unlock:
>>>>> -       /* Poison memory range if belonging to some still mapped
>>>>> pages. */
>>>>> -       if (!unmapped_pages)
>>>>> +       if (!unmapped_pages) {
>>>>>                   asan_set_freezone(asan_ptr, asan_data_len);
>>>>> +       } else {
>>>>> +               /*
>>>>> +                * We may be in a situation where we unmapped pages
>>>>> like this:
>>>>> +                * malloc header | free space | unmapped space | free
>>>>> space | malloc header
>>>>> +                */
>>>>> +               void *free1_start = asan_ptr;
>>>>> +               void *free1_end = aligned_start;
>>>>> +               void *free2_start = RTE_PTR_ADD(aligned_start,
>>>>> aligned_len);
>>>>> +               void *free2_end = RTE_PTR_ADD(asan_ptr, 
>>>>> asan_data_len);
>>>>> +
>>>>> +               if (free1_start < free1_end)
>>>>> +                       asan_set_freezone(free1_start,
>>>>> +                               RTE_PTR_DIFF(free1_end, free1_start));
>>>>> +               if (free2_start < free2_end)
>>>>> +                       asan_set_freezone(free2_start,
>>>>> +                               RTE_PTR_DIFF(free2_end, free2_start));
>>>>> +       }
>>>>>
>>>>>           rte_spinlock_unlock(&(heap->lock));
>>>>>           return ret;
>>>>>
>>>>
>>>> Something like that, yes. I will have to think through this a bit more,
>>>> especially in light of your func_reentrancy splat :)
>>>>
>>>
>>> So, the reason splat in func_reentrancy test happens is as follows: the
>>> above patch is sorta correct (i have a different one but does the same
>>> thing), but incomplete. What happens then is when we add new memory, we
>>> are integrating it into our existing malloc heap, which triggers
>>> `malloc_elem_join_adjacent_free()` which will trigger a write into old
>>> header space being merged, which may be marked as "freed". So, again we
>>> are hit with our internal allocator messing with ASan.
>>
>> I ended up with the same conclusion.
>> Thanks for confirming.
>>
>>
>>>
>>> To properly fix this is to answer the following question: what is the
>>> goal of having ASan support in DPDK? Is it there to catch bugs *in the
>>> allocator*, or can we just trust that our allocator code is correct, and
>>> only concern ourselves with user-allocated areas of the code? Because it
>>
>> The best would be to handle both.
>> I don't think clang disables ASan for the instrumentations on malloc.
> 
> I've actually prototyped these changes a bit. We use memset in a few 
> places, and that one can't be disabled as far as i can tell (not without 
> blacklisting memset for entire DPDK).
> 
>>
>>
>>> seems like the best way to address this issue would be to just avoid
>>> triggering ASan checks for certain allocator-internal actions: this way,
>>> we don't need to care what allocator itself does, just what user code
>>> does. As in, IIRC there was a compiler attribute that disables ASan
>>> checks for a specific function: perhaps we could just wrap certain
>>> access in that and be done with it?
>>>
>>> What do you think?
>>
>> It is tempting because it is the easiest way to avoid the issue.
>> Though, by waiving those checks in the allocator, does it leave the
>> ASan shadow in a consistent state?
>>
> 
> The "consistent state" is kinda difficult to achieve because there is no 
> "default" state for memory - sometimes it comes as available (0x00), 
> sometimes it is marked as already freed (0xFF). So, coming into a malloc 
> function, we don't know whether the memory we're about to mess with is 
> 0x00 or 0xFF.
> 
> What we could do is mark every malloc header with 0xFF regardless of its 
> status, and leave the rest to "regular" zoning. This would be strange 
> from ASan's point of view (because we're marking memory as "freed" when 
> it wasn't ever allocated), but at least this would be consistent :D
> 

I've been prototyping a solution for this, but I keep bumping into our 
dual usage of ASan: ASan doesn't differentiate between 
allocator-internal accesses, and user code accesses. Therefore, we can't 
either, so either we start marking areas as "accessible" even when they 
shouldn't be (such as unallocated areas that correspond to malloc 
headers), or we only use ASan to mark user-available areas and forego 
its usage inside the allocator entirely.

Right now, the best I can think of is the combination of approaches 
discussed earlier: that is, we mark all malloc element header areas as 
"available" unconditionally (thereby sacrificing part of the protection 
ASan provides us - because we can't prevent ASan from complaining about 
accesses from inside the allocator without losing our ability to detect 
cases where user accidentally accesses a malloc element), and we also 
mark unmapped memory as "available" (because writing to it will trigger 
a fault anyway).

I haven't yet figured out the cleanest solution (we miss asan zoning for 
headers somewhere), but at least i got func reentrancy test to pass :D

-- 
Thanks,
Anatoly

  reply	other threads:[~2022-04-27 15:50 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-15 17:31 [PATCH 0/3] Enable ASan in GHA David Marchand
2022-04-15 17:31 ` [PATCH 1/3] test/mem: disable ASan when accessing unallocated mem David Marchand
2022-04-20 14:48   ` Burakov, Anatoly
2022-04-15 17:31 ` [PATCH 2/3] mem: fix ASan shadow for remapped memory segments David Marchand
2022-04-20 14:47   ` Burakov, Anatoly
2022-04-21  9:37     ` David Marchand
2022-04-21  9:50       ` David Marchand
2022-04-21 13:18       ` Burakov, Anatoly
2022-04-26 12:54         ` Burakov, Anatoly
2022-04-26 14:15           ` David Marchand
2022-04-26 16:07             ` Burakov, Anatoly
2022-04-27 15:32               ` Burakov, Anatoly [this message]
2022-04-15 17:31 ` [PATCH 3/3] ci: build some job with ASan David Marchand
2022-05-05  9:29 ` [PATCH v2 0/2] Enable ASan in GHA David Marchand
2022-05-05  9:29   ` [PATCH v2 1/2] test/mem: disable ASan when accessing unallocated mem David Marchand
2022-05-05  9:29   ` [PATCH v2 2/2] ci: build some job with ASan David Marchand
2022-05-10 19:22     ` Aaron Conole
2022-05-11 12:01   ` [PATCH v2 0/2] Enable ASan in GHA David Marchand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3d6e93d8-e9f6-6d69-c542-4e274e410a58@intel.com \
    --to=anatoly.burakov@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=dmitry.kozliuk@gmail.com \
    --cc=john.mcnamara@intel.com \
    --cc=stable@dpdk.org \
    --cc=xueqin.lin@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).