xref: /llvm-project/compiler-rt/test/fuzzer/SymbolizeDeadlock.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
3*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
414cf71a3SMatt Morehouse 
514cf71a3SMatt Morehouse // Tests that deadlocks do not occur when an OOM occurs during symbolization.
614cf71a3SMatt Morehouse 
714cf71a3SMatt Morehouse #include <cassert>
814cf71a3SMatt Morehouse #include <cstdint>
914cf71a3SMatt Morehouse #include <cstdio>
1014cf71a3SMatt Morehouse #include <cstdlib>
1114cf71a3SMatt Morehouse #include <cstring>
1214cf71a3SMatt Morehouse 
1314cf71a3SMatt Morehouse #include "Bingo.h"
1414cf71a3SMatt Morehouse 
1514cf71a3SMatt Morehouse volatile unsigned Sink = 0;
1614cf71a3SMatt Morehouse 
1714cf71a3SMatt Morehouse // Do not inline this function.  We want to trigger NEW_FUNC symbolization when
1814cf71a3SMatt Morehouse // libFuzzer finds this function.  We use a macro to make the name as long
1914cf71a3SMatt Morehouse // possible, hoping to increase the time spent in symbolization and increase the
2014cf71a3SMatt Morehouse // chances of triggering a deadlock.
BINGO()2114cf71a3SMatt Morehouse __attribute__((noinline)) void BINGO() {
2214cf71a3SMatt Morehouse   // Busy work.  Inserts a delay here so the deadlock is more likely to trigger.
2314cf71a3SMatt Morehouse   for (unsigned i = 0; i < 330000000; i++) Sink += i;
2414cf71a3SMatt Morehouse }
2514cf71a3SMatt Morehouse 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)2614cf71a3SMatt Morehouse extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
2714cf71a3SMatt Morehouse   assert(Data);
2814cf71a3SMatt Morehouse   if (Size < 3) return 0;
2914cf71a3SMatt Morehouse   if (Data[0] == 'F' &&
3014cf71a3SMatt Morehouse       Data[1] == 'U' &&
3114cf71a3SMatt Morehouse       Data[2] == 'Z')
3214cf71a3SMatt Morehouse     BINGO();
3314cf71a3SMatt Morehouse   return 0;
3414cf71a3SMatt Morehouse }
3514cf71a3SMatt Morehouse 
36