<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/117925>117925</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Asan] ASan flags set through a user-defined `__asan_default_options` function may not be honored in Windows
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          davidmrdavid
      </td>
    </tr>
</table>

<pre>
    **Context:**

ASan configuration flags can be specified, among other methods, through a user-defined function of the following form:

```C++
extern "C" __declspec(dllexport) extern const char *__asan_default_options()
{
    return "allocator_may_return_null=1"; // or some other flag
}
```

When using this method, the configuration options are not available during ASan initialization (responsible for parsing configuration flags), which occurs in `AsanInitInternal`:

https://github.com/llvm/llvm-project/blob/175051b05edcd10d997d8f98f142af1ff191b93a/compiler-rt/lib/asan/asan_rtl.cpp#L393-L402

Instead, these flags are only parsed through a callback registered through `AddRegisterWeakFunctionCallback`:

https://github.com/llvm/llvm-project/blob/175051b05edcd10d997d8f98f142af1ff191b93a/compiler-rt/lib/asan/asan_flags.cpp#L215-L244

This is further corroborated by that method's top-level comment, which reads:

> On Windows, weak symbols are emulated by having the user program register which weak functions are defined. The ASAN DLL will initialize flags prior to user module initialization, so __asan_default_options will not point to the user definition yet. We still want to ensure we capture when options are passed via __asan_default_options, so we add a callback to be run when it is registered with the runtime.

Therefore, that callback should probably be checking every single ASan flag in case it has changed, and initialize them accordingly, but it's not doing so. 

**For example**, the flag `allocator_may_return_null` is not being properly initialized today as it's not calling `SetAllocatorMayReturnNull`.

In  `AsanInitInternal`, there are many calls to methods starting with the `Set` keyword, which configure ASan flags. These include:
- `SetCanPoisonMemory`
- `SetMallocContextSize`
- `SetCheckUnwindCallback`

and others. I expect these are additional methods that should also be invoked through the callback and that they represent other flags that are currently being missed.

**Preliminary thoughts on how to address this:**
I think what we want is for the flag initialization code of `ASanInitInternal` to be refactored into a convenient helper function that can be called multiple times, onces in `ASanInitInternal`, and then again in the call back within `AddRegisterWeakFunctionCallback`.

**Of less relevance:**
Hi, I'm from the msvc team, I work on ASan there. I just fixed this bug in our internal fork, looking to contribute it back upstream _shortly_. Just creating this issue for reference. In general, we're interested in contributing more fixes directly upstream, so you might see more of these issues pop up, assuming that's 'ok'. Thanks!


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV11v6zYS_TX0yyCGRMex_eAH32SDzeLetmi6uI8GRY4s1hRHICk76q9fDCUl7m2Dxb4tYMCJJc3HmXPOUCpGe_KIe7H-ItZPC9WnhsLeqIs1bchfi4rMsBfyIOThkXzCtyRWh_F_UfDn8Ko8aPK1PfVBJUseaqdOEbTyUCHEDrWtLRohH0G15E9AqcEALaaGTOSfUxOoPzWgoI8Y7gzW1qOBuvc6B6QaUoNQk3N0tf4ENYWW68gViIdi_DwK-YU_xQHfEgYPQspHISUcjwa140qE3Brn8K2jkITcwXSjJh8T6EYFEPJwPKqo_NFgrXqXjtRxEVHIrZA7zrfhFAAAAVM_plHOkVaJwrFVw3H8_eh758TqqRRSitUXEPJZyGegAJFanFBgrHLMp9tOxsa-N-ihj9xwamycEBsBwx8wn4oEFRA8JVAXZZ2qHILpA0fIc7LeJquc_WN8SMhtwNiRj5bvrClAp0JO-DcT5e7lI1wbqxsgrfsQwXoQD8UhKv_ibXrxjKZy3ME8nSalLmbOcPcnm5q-WmpqhXx27jJ_3XWBfkedhHyuHFVCPpebdbEuq2KNRpuyMLvdxmzr3bYu76Wqy7oud2W1WykhnzW1nXUY7nimz87y4zzB6esYklvqrhNy9XW1W919vS_kWNuLjwnVjGjEibmMIXk3ZDDQ3NBTK-cqpc8Q8GRjwnBzlXEw5tfpwndU5-eJv4_TU_9nsORmZ2Bkub77Ku_vx-p-Y7rZCHUfMks1hUAVBZXQQDVAalR6p-MmQqLuzuEFHWhqW_TpgygBlYkfWl39A3728N16Q9es_SuqM8Shrci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