Skip to content

Freeing of rgILMapEntries in ProfToEEInterfaceImpl::SetILInstrumentedCodeMap() #12129

Closed
dotnet/docs
#14228

Description

@k15tfu

Hi guys!

I'm not sure who is right because there are almost no any information about releasing of rgILMapEntries argument in ICorProfilerInfo::SetILInstrumentedCodeMap(), but probably we forgot to free that memory.

This page [ https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms232500(v%3Dvs.100) ] says nothing about transferring ownership for rgILMapEntries, but in this book [ http://blog.monstuff.com/archives/images/Profiling.pdf ] it says The code profiler should not attempt to free this memory.

Moreover, OpenCover does release this memory on .NET 4: https://github.com/OpenCover/opencover/blob/d94d935bfe77a48395096e17ab430a142b3f628b/main/OpenCover.Profiler/CodeCoverage.cpp#L389

It's not clear what it is the memory should be: how to allocate it, and when to release. Could someone shed more light on this?

Activity

  1. jkotas commented on Feb 27, 2019

    @jkotas
    Member

    Runtime makes a copy of rgILMapEntries: https://github.com/dotnet/coreclr/blob/ee755e322dabc2fc280e2561b0fbaf6e90aedf54/src/vm/proftoeeinterfaceimpl.cpp#L4624

    There is no transfer of ownership. The profiler code can allocate the array any way it likes and it is responsible for freeing it. The array can be freed right after SetILInstrumentedCodeMap returns.

  2. k15tfu commented on Feb 28, 2019

    @k15tfu
    ContributorAuthor

    @jkotas,

    Yes, I saw it, but taking into account the various information I found on the Internet, it still could be an implementation-specific behaviour. So, can profiler developers rely on it?

    If yes, does the same apply to .NET Framework in general without reservation?

    Anyway, I think it's really important to clarify it in the docs: https://github.com/dotnet/docs/blob/master/docs/framework/unmanaged-api/profiling/icorprofilerinfo-setilinstrumentedcodemap-method.md . What do you think?

  3. reopened this on Feb 28, 2019
  4. jkotas commented on Feb 28, 2019

    @jkotas
    Member

    There was a change in behavior between .NET Framework 2.0 to .NET Framework 4.0.

    Before .NET Framework 4.0, the runtime took over the ownership of the memory. The profiler had to allocate the memory using CoTaskMemAlloc and it should not free it.

    In .NET Framework 4.0 and all versions of .NET Core, the runtime does not take over the ownership of the memory anymore. The profiler can use any allocator it wishes to allocate the memory and it is responsible for freeing it.

    For reference, this is the change description from the .NET Framework source control tracking system:

    change 3357811 edit on 2009/06/27 22:37:48

    COR_IL_MAP array
    Title: Dev10 693636: SetILInstrumentedCodeMap needs to make a copy of the
      COR_IL_MAP array
    
    Issue description:
      In both V2 and V4, SetILInstrumentedCodeMap() does not make a copy of
      the COR_IL_MAP array passed to it as an argument.  However, the
      documentation on the profiling APIs specifies that the profiler is free
      to release the memory after the function call.  Thus, the runtime would
      be holding onto a dangling pointer after the profiler releases the
      memory.  We should make a copy of the array instead.  This should be
      fixed in V4, but it's probably not important enough to fix in V2 since
      not many profilers care about updating the IL to native offset mapping
      information.
    
    Change description:
      Make a copy of the COR_IL_MAP array instead of using COR_IL_MAP array
      allocate by the profiler
    

    @k15tfu Yes, I agree that the docs should be updated. Would you like to submit a PR to propose the update?

  5. reopened this on Mar 2, 2019
  6. sawilde commented on Mar 3, 2019

    @sawilde
    Contributor

    There was a change in behavior between .NET Framework 2.0 to .NET Framework 4.0.

    beat me to it.

  7. k15tfu commented on Sep 5, 2019

    @k15tfu
    ContributorAuthor

    @jkotas Could you check it pls: dotnet/docs#14228

  8. k15tfu commented on Sep 9, 2019

    @k15tfu
    ContributorAuthor

    thanks!

  9. transferred this issue fromdotnet/coreclron Jan 31, 2020
  10. added this to the Future milestone on Jan 31, 2020
  11. ghost locked as resolved and limited conversation to collaborators on Dec 14, 2020
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-Diagnostics-coreclrdocumentation

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions