Skip to content

Address Sanitizer support for native libraries #13458

Description

@rayslava

We work on sanitization of whole Tizen OS and therefore try to cover as many code as we can with Address Sanitizer.

Currently we came to sanitization of native libraries which are used only by managed applications running on CoreCLR.

Currently we have setup of two parts:

  1. Additional build of patched Address Sanitizer run-time with "switchable" interceptors.

  2. Patched CoreCLR version with changed native calls generation behavior.

Address Sanitizer patch is required to add additional API to activate or deactivate ASan interception of library calls (e.g. tracing malloc()/free()) in order to get appropriate performance of CoreCLR engine and to avoid reports about CoreCLR internals' issues: our primary customer wants to get reports about their code only, CoreCLR testing is a separate task. Additional build is required since this API introduces a performance overhead to each library call, which we don't want to see for the rest of applications.

CoreCLR version has changes in JIT part in generator of calls from managed code to native libraries, a wrapper is added which calls ASan API to switch tracing on when code from library is executed and turn it off during return back to managed code.

Now we only detect issues inside native libraries, but our next target is annotate CoreCLR allocator, integrate it with ASan and catch bugs for cases when buffer allocated by managed code is passed to native library and corrupted there. Accordingly to our customers, who develop apps for Tizen, this case is rather hard to debug and consequences of such errors are rather bad.

Currently we use GCC to build Tizen OS, so our changes are introduced only to GCC ASan run-time inside libasan.so. Technically it's possible to add it to Clang run-time as well, but it'll require recompilation of target libraries since Clang ASan uses static linkage and we're not sure that someone really needs this support. If you are interested in this kind of testing as well, we could introduce this mode in a conditional build.

cc @alpencolt

Activity

  1. alpencolt commented on Sep 23, 2019

    @alpencolt
  2. AaronRobinsonMSFT commented on Sep 23, 2019

    @AaronRobinsonMSFT
    Member

    @rayslava It would be helpful to see an example of the kind of change needed. The cost of implementing and maintaining the hooks would determine the acceptance of the work. This is something we should definitely consider and a small example would help quicken that decision on work.

  3. jkotas commented on Sep 23, 2019

    @jkotas
    Member

    @rayslava Thank you for writing down this proposal.

    If I understand this right, you would like to use the special ASAN allocator when managed code interop with unmanaged code, but keep using the regular allocator for the runtime itself.

    Would it make sense to re-reroute all malloc/free done for interop (e.g. the calls to Marshal.AllocHGlobal / Marshal.FreeHGlobal or malloc/free calls done by the interop marshaling layer) to the special ASAN allocator? It would avoid the need for the switching that will be hard to get right 100% of the time, and maybe even avoid a need for a special build of the runtime.

  4. xiangzhai commented on Dec 3, 2019

    @xiangzhai
    Contributor

    :mips-interest

  5. transferred this issue fromdotnet/coreclron Jan 31, 2020
  6. added this to the Future milestone on Jan 31, 2020
  7. dkson95 commented on Nov 9, 2021

    @dkson95

    Hello @ALL
    Recently, I've noticed the related issue on : https://developercommunity.visualstudio.com/t/address-sanitizer-not-working-on-dll-pro/1561504?entry=problem&space=62 .

    And, I hope this feature adoption can help to fix and support native libraries(.dll) - memory corruption detection called by dotnet apps.

    @rayslava how do you think of above feedback case ?

  8. fforjan commented on Aug 16, 2024

    @fforjan

    @jkoritzinsky or @agocke as you have work on the Enable Windows runs of AddressSanitizer #95758, do you have any update on this, we would like to run some .net tests which consume our native library and we would like the native library to be instrumented with ASAN but it seems there is no way we could do that ?

  9. jkoritzinsky commented on Aug 16, 2024

    @jkoritzinsky
    Member

    We do not have support for only instrumenting native libraries and not instrumenting the runtime today.

    If you want to instrument everything, you can build the runtime with the -fsanitize address flag to enable ASAN for the whole runtime. Then you can load an instrumented native library and things will work.

  10. fforjan commented on Aug 16, 2024

    @fforjan

    @jkoritzinsky thanks for the idea !

  11. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/runtime-infrastructure
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-InfrastructurequestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions