Skip to content

testing: apparent memory leak in fuzzer #44517

Closed

Description

@dsnet

If I leave a fuzzer running for sufficiently long, then it crashes with an OOM. Snippet from dmesg:

[1974087.246830] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-319973.slice/session-c1.scope,task=json.test,pid=3659815,uid=319973
[1974087.264733] Out of memory: Killed process 3659815 (json.test) total-vm:18973836kB, anon-rss:13185376kB, file-rss:0kB, shmem-rss:0kB, UID:319973 pgtables:33988kB oom_score_adj:0
[1974087.971181] oom_reaper: reaped process 3659815 (json.test), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB

I don't believe this is because the code being tested is OOMing, but rather the fuzzer itself is retaining too much memory.

Here's a graph of the RSS memory usage over time:
graph
The machine has 32GiB of RAM.

There are large jumps in memory usage at various intervals. I don't have much understanding of how the fuzzer works, but maybe this could be the mutator discovering that some input expands coverage and adding that to some internal data structure?

I should further note that when the fuzzer crashes, it produces a testdata/FuzzXXX/YYY file as the "reproducer". Running the test with that "reproducer" does not fail the fuzz test. If possible, the fuzzer should be able to distinguish between OOMs due to itself and versus OOMs due to the code being tested. The former should not result in any "repro" corpus files being added, while the latter should.

I'm using 5aacd47.
(I can provide the code I'm fuzzing, but contact me privately)

\cc @katiehockman @jayconrod

Activity

  1. added
    fuzzIssues related to native fuzzing support
    on Feb 22, 2021
  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Feb 23, 2021
  3. added this to the Backlog milestone on Feb 23, 2021
  4. katiehockman commented on Mar 4, 2021

    @katiehockman
    Contributor

    Thanks @dsnet. We'll definitely look into this.

  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Mar 4, 2021
  6. self-assigned this
    on Mar 4, 2021
  7. rolandshoemaker commented on May 20, 2021

    @rolandshoemaker
    Member

    I suspect this will have been fixed by https://golang.org/cl/316030. @dsnet could you re-run your target with the current head of dev.fuzz and see if it still OOMs?

  8. dsnet commented on May 23, 2021

    @dsnet
    MemberAuthor

    It seems that there's maybe still a possible memory leak?

    chart-oom

    The horizontal axis is in seconds, while the vertical axis is memory utilization in percentages (on a system with 8GiB). This is the memory usage for the worker, while the parent was closer to 0.1% memory usage at all times. It seems that https://golang.org/cl/316030 had a notable impact.

    Of note, the worker process seemed to allocate more and more memory until it hit 75% memory utilization around 2 hours in. Afterwards, it held at that level. It never triggered an OOM from the kernel, but did cause the system to be sufficiently unusable that I couldn't SSH into the machine to kill the fuzzer. It's unclear to me whether the process hovering around 75% utilization was intended behavior.

    I'm using 60f16d7.

  9. rolandshoemaker commented on Jun 3, 2021

    @rolandshoemaker
    Member

    @dsnet I'll take another look, I think we expect that the fuzzer will typically use a bunch of memory, especially if it is finding a lot of interesting inputs, but the chart does look strange.

    Could you share the target you are using? It's hard to debug without really knowing what is actually happening. You can email me at bracewell@ if you'd like it to remain private.

  10. klauspost commented on Jun 7, 2021

    @klauspost
    Contributor

    For a test set with a rather large base set I see huge memory allocs as well. I've had to reduce the base set to only include input <100K to not have memory usage explode.

    go version devel go1.17-5542c10fbf Fri Jun 4 03:07:33 2021 +0000 windows/amd64 FWIW.

  11. rolandshoemaker commented on Jun 8, 2021

    @rolandshoemaker
    Member

    I am currently unable to reproduce the behavior described in your second comment on the head of dev.fuzz (b2ff3e8) or at 60f16d7 @dsnet. Running your target on my system results in workers each using a stable ~200MB of memory (tested over ~4 hours).

    One thing that may be happening is that the coordinator process currently stores a copy of the corpus in memory, so if the input corpus is extremely large, or if the workers find a lot of new inputs, the memory footprint will be quite large. We can address this by just always reading the inputs from disk, rather than keeping a copy in memory.

    Another factor may be that we currently don't influence the mutator towards smaller inputs, so over time inputs will trend larger, although this is capped at ~100MB so as long as there isn't a leak in the worker we shouldn't see unbounded memory usage growth over time.

  12. dsnet commented on Aug 11, 2021

    @dsnet
    MemberAuthor

    Anecdotal evidence: it seems that there might be a memory leak related to the use of sub-tests.

    https://github.com/go-json-experiment/json/blob/120675eb6f8ce84b755a48c2ee8dded9c29818a6/fuzz_test.go#L122-L141

    In the snippet above, I eventually encounter an OOM (within 30 minutes) if t.Run is used. However, if I remove the use of sub-tests, the memory stays fairly low.

  13. dsnet commented on Aug 11, 2021

    @dsnet
    MemberAuthor

    Okay, it seems that this is the problem. Trivial reproduction:

    func FuzzOOM(f *testing.F) {
    	f.Fuzz(func(t *testing.T, b []byte) {
    		for i := 0; i < 1000; i++ {
    			t.Run("", func(t *testing.T) {})
    		}
    	})
    }

    This will allocate and hold onto gigabytes of memory within minutes.

  14. gopherbot commented on Aug 11, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/341336 mentions this issue: [dev.fuzz] testing: fix memory leak with sub-tests

  15. 3 remaining items

  16. removed their assignment
    on Sep 14, 2021
  17. changed the title [-][dev.fuzz] apparent memory leak in fuzzer [/-] [+]testing: apparent memory leak in fuzzer[/+] on Sep 21, 2021
  18. gopherbot commented on Sep 22, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/351452 mentions this issue: testing: reduce memory used by subtest names

  19. bcmills commented on Oct 4, 2021

    @bcmills
    Contributor

    Another case to consider: the test can spawn arbitrarily many subtests with names based on the fuzz inputs, so the set of user-provided subtest names will grow over time even if it only stores O(1) bits per user-provided test name.

    Consider this related test:

    func FuzzInputAsSubtest(f *testing.F) {
    	f.Fuzz(func(t *testing.T, b []byte) {
    		t.Run(string(b), func(t *testing.T) {
    		})
    	})
    }

    As far as I can tell, in order to make this scale we must ensure that the fuzz worker periodically drops the subNames map in between test runs, since we only really care that the subtest names be unique within a given Fuzz input invocation — not that they be globally unique across the full fuzzing run.

  20. bcmills commented on Oct 4, 2021

    @bcmills
    Contributor

    FWIW I'm unable to reproduce unbounded memory growth with the example in #44517 (comment) from the current head, and I don't observe a leak with #44517 (comment) either.

    I'm not sure how to resolve that discrepancy. @dsnet, can you still reproduce the problem?

  21. dsnet commented on Oct 4, 2021

    @dsnet
    MemberAuthor

    @dsnet, can you still reproduce the problem?

    As of go version devel go1.18-3bbc82371e, this is still an issue. I run it with:

    go.tip test -fuzz=OOM -v -parallel=1
    

    Since each individual fuzz worker has its own namespace, running with -parallel=1 seems to manifest the problem fastest. On my Ryzen 5900x, it was allocating about ~1GiB/minute.

  22. bcmills commented on Oct 4, 2021

    @bcmills
    Contributor

    Hmm, I wonder if the -v and -parallel flags in that invocation are load-bearing. 🤔

  23. gopherbot commented on Oct 6, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/354430 mentions this issue: testing: don't create unique subtest names while fuzzing

  24. gopherbot commented on Oct 8, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/354749 mentions this issue: testing: reduce memory used by subtest names

  25. locked and limited conversation to collaborators on Jun 23, 2023
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

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.fuzzIssues related to native fuzzing supportrelease-blocker

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions