Skip to content

GetGCMemoryInfo.TotalAvailableMemoryBytes returns negative value on ARM32 #65466

Closed

Description

@brianpopow

Description

GC.GetGCMemoryInfo().TotalAvailableMemoryBytes reports negative values on linux/ARM32.

The is issue was brought to our attention original with ImageSharp Raspberry Pi OS 11 Issue

There was a similar Issue already reported here issues/13115, but this was closed.

The following simple code snippet triggers the issue:

Console.WriteLine("TotalAvailableMemoryBytes = " + GC.GetGCMemoryInfo().TotalAvailableMemoryBytes);

I tried the following official dotnet sdk images:

  • mcr.microsoft.com/dotnet/sdk:6.0.102-bullseye-slim-arm32v7
  • mcr.microsoft.com/dotnet/sdk:6.0.102-focal-arm32v7
  • mcr.microsoft.com/dotnet/sdk:5.0.405-buster-slim-arm32v7

All report TotalAvailableMemoryBytes = -402354176

Reproduction Steps

Console.WriteLine("TotalAvailableMemoryBytes = " + GC.GetGCMemoryInfo().TotalAvailableMemoryBytes);

Expected behavior

Report a positive number of available bytes.

Actual behavior

TotalAvailableMemoryBytes = -402354176

Regression?

ARM64 does not seem to be affected.

.NET Core 3.1.416 does not seem to be affected (tried with docker image: mcr.microsoft.com/dotnet/sdk:3.1.416-bullseye-arm32v7)

Known Workarounds

No response

Configuration

  • .NET version 6.0.102 and 5.0.405
  • architecture: ARM32

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 16, 2022
  2. removed
    untriaged
    on Jul 6, 2022
  3. added this to the 7.0.0 milestone on Jul 6, 2022
  4. cshung commented on Aug 2, 2022

    @cshung
    Contributor

    @antonfirsov, @brianpopow can you still reproduce the issue?

  5. antonfirsov commented on Aug 2, 2022

    @antonfirsov
    Contributor

    This has been reported by an ImageSharp user in SixLabors/ImageSharp#2001 for Raspberry Pi OS 11 - Bullseye. I don't have the HW to repro it unfortunately. ImageSharp implemented a sub-optimal workaround to deal with this issue.

    Was there a change in the runtime that potentially addressed this?

  6. cshung commented on Aug 2, 2022

    @cshung
    Contributor

    This has been reported by an ImageSharp user in SixLabors/ImageSharp#2001 for Raspberry Pi OS 11 - Bullseye. I don't have the HW to repro it, unfortunately. ImageSharp implemented a sub-optimal workaround to deal with this issue.

    Was there a change in the runtime that potentially addressed this?

    I confirmed that the managed API, GetGCMemoryInfo.TotalAvailableMemoryBytes, faithfully reports the value stored in gc_heap::total_physical_mem, the field is a uint64_t, which cannot be negative (but of course, could be a wrong value). The property GetGCMemoryInfo.TotalAvailableMemoryBytes is a long, which is a 64-bit signed integer, and there is a possibility that the conversion generated a negative number)

    Indeed, if I hard code gc_heap::total_physical_mem to 0xF000 0000 0000 0000`, it is possible to force the API to return a negative value.

    If I hard code gc_heap::total_physical_mem to 0xF000 0000, the GetGCMemoryInfo.TotalAvailableMemoryBytes returned a positive number just fine.

    Analyzing the code further, it must be the case that GCToOSInterface::GetPhysicalMemoryLimit. That API returns a uint64_t, but it could return a bogus value that is larger than 2^63-1.

    For Linux, the detection of memory info is based on sysctl. For that, unfortunately, without the repro hardware, there is no way to look at that further.

    There is indeed something suspicious in the underlying code, something along this line.

    if (physical_memory_limit > std::numeric_limits<size_t>::max())
    {
    // It is observed in practice when the memory is unrestricted, Linux control
    // group returns a physical limit that is bigger than the address space
    return std::numeric_limits<size_t>::max();
    }
    else
    {
    return (size_t)physical_memory_limit;
    }

    That case is specific to containers though - note that size_t is an unsigned 32-bit integer on a Linux ARM machine.

    In various places, I also see size_t is used in the available physical memory code path, which could be a reason.

  7. cshung commented on Aug 2, 2022

    @cshung
    Contributor

    Turn out I am able to repro on an ARM64 device by installing an ARM dotnet SDK. I will take it from there.

  8. antonfirsov commented on Aug 2, 2022

    @antonfirsov
    Contributor

    If this helps or is interesting for any reason: The value reported by the user was -456847360 which is 18446744073252704256 unsigned.

  9. antonfirsov commented on Aug 2, 2022

    @antonfirsov
    Contributor

    I'm stupid, and missed that @brianpopow had a repro: SixLabors/ImageSharp#2001 (comment)

  10. ghost added
    in-pr
    on Aug 2, 2022
  11. ghost removed
    in-pr
    on Aug 3, 2022
  12. ghost locked as resolved and limited conversation to collaborators on Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions