Skip to content

Commit 41289fa

Browse files
committed
Update libFuzzer to llvm/llvm-project's 70cbc6d
1 parent 56ca914 commit 41289fa

25 files changed

+411
-570
lines changed

libfuzzer/CREDITS.TXT

Lines changed: 0 additions & 36 deletions
This file was deleted.

libfuzzer/FuzzerBuiltins.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
2626
inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
2727

2828
inline uint32_t Clzll(unsigned long long X) { return __builtin_clzll(X); }
29-
inline uint32_t Clz(unsigned long long X) { return __builtin_clz(X); }
3029
inline int Popcountll(unsigned long long X) { return __builtin_popcountll(X); }
3130

3231
} // namespace fuzzer

libfuzzer/FuzzerBuiltinsMsvc.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ inline uint32_t Clzll(uint64_t X) {
5252
return 64;
5353
}
5454

55-
inline uint32_t Clz(uint32_t X) {
56-
unsigned long LeadZeroIdx = 0;
57-
if (_BitScanReverse(&LeadZeroIdx, X)) return 31 - LeadZeroIdx;
58-
return 32;
59-
}
60-
6155
inline int Popcountll(unsigned long long X) {
6256
#if !defined(_M_ARM) && !defined(_M_X64)
6357
return __popcnt(X) + __popcnt(X >> 32);

libfuzzer/FuzzerCorpus.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct InputInfo {
4444
// Power schedule.
4545
bool NeedsEnergyUpdate = false;
4646
double Energy = 0.0;
47-
size_t SumIncidence = 0;
47+
double SumIncidence = 0.0;
4848
Vector<std::pair<uint32_t, uint16_t>> FeatureFreqs;
4949

5050
// Delete feature Idx and its frequency from FeatureFreqs.
@@ -74,27 +74,28 @@ struct InputInfo {
7474
void UpdateEnergy(size_t GlobalNumberOfFeatures, bool ScalePerExecTime,
7575
std::chrono::microseconds AverageUnitExecutionTime) {
7676
Energy = 0.0;
77-
SumIncidence = 0;
77+
SumIncidence = 0.0;
7878

7979
// Apply add-one smoothing to locally discovered features.
8080
for (auto F : FeatureFreqs) {
81-
size_t LocalIncidence = F.second + 1;
82-
Energy -= LocalIncidence * logl(LocalIncidence);
81+
double LocalIncidence = F.second + 1;
82+
Energy -= LocalIncidence * log(LocalIncidence);
8383
SumIncidence += LocalIncidence;
8484
}
8585

8686
// Apply add-one smoothing to locally undiscovered features.
87-
// PreciseEnergy -= 0; // since logl(1.0) == 0)
88-
SumIncidence += (GlobalNumberOfFeatures - FeatureFreqs.size());
87+
// PreciseEnergy -= 0; // since log(1.0) == 0)
88+
SumIncidence +=
89+
static_cast<double>(GlobalNumberOfFeatures - FeatureFreqs.size());
8990

9091
// Add a single locally abundant feature apply add-one smoothing.
91-
size_t AbdIncidence = NumExecutedMutations + 1;
92-
Energy -= AbdIncidence * logl(AbdIncidence);
92+
double AbdIncidence = static_cast<double>(NumExecutedMutations + 1);
93+
Energy -= AbdIncidence * log(AbdIncidence);
9394
SumIncidence += AbdIncidence;
9495

9596
// Normalize.
9697
if (SumIncidence != 0)
97-
Energy = (Energy / SumIncidence) + logl(SumIncidence);
98+
Energy = Energy / SumIncidence + log(SumIncidence);
9899

99100
if (ScalePerExecTime) {
100101
// Scaling to favor inputs with lower execution time.
@@ -213,6 +214,8 @@ class InputCorpus {
213214
assert(!U.empty());
214215
if (FeatureDebug)
215216
Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
217+
// Inputs.size() is cast to uint32_t below.
218+
assert(Inputs.size() < std::numeric_limits<uint32_t>::max());
216219
Inputs.push_back(new InputInfo());
217220
InputInfo &II = *Inputs.back();
218221
II.U = U;
@@ -224,7 +227,7 @@ class InputCorpus {
224227
II.HasFocusFunction = HasFocusFunction;
225228
// Assign maximal energy to the new seed.
226229
II.Energy = RareFeatures.empty() ? 1.0 : log(RareFeatures.size());
227-
II.SumIncidence = RareFeatures.size();
230+
II.SumIncidence = static_cast<double>(RareFeatures.size());
228231
II.NeedsEnergyUpdate = false;
229232
std::sort(II.UniqFeatureSet.begin(), II.UniqFeatureSet.end());
230233
ComputeSHA1(U.data(), U.size(), II.Sha1);
@@ -399,7 +402,7 @@ class InputCorpus {
399402
// Zero energy seeds will never be fuzzed and remain zero energy.
400403
if (II->Energy > 0.0) {
401404
II->SumIncidence += 1;
402-
II->Energy += logl(II->SumIncidence) / II->SumIncidence;
405+
II->Energy += log(II->SumIncidence) / II->SumIncidence;
403406
}
404407
}
405408

@@ -426,7 +429,8 @@ class InputCorpus {
426429
NumUpdatedFeatures++;
427430
if (FeatureDebug)
428431
Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
429-
SmallestElementPerFeature[Idx] = Inputs.size();
432+
// Inputs.size() is guaranteed to be less than UINT32_MAX by AddToCorpus.
433+
SmallestElementPerFeature[Idx] = static_cast<uint32_t>(Inputs.size());
430434
InputSizesPerFeature[Idx] = NewSize;
431435
return true;
432436
}
@@ -464,7 +468,7 @@ class InputCorpus {
464468

465469
static const bool FeatureDebug = false;
466470

467-
size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
471+
uint32_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
468472

469473
void ValidateFeatureSet() {
470474
if (FeatureDebug)
@@ -539,9 +543,11 @@ class InputCorpus {
539543

540544
if (VanillaSchedule) {
541545
for (size_t i = 0; i < N; i++)
542-
Weights[i] = Inputs[i]->NumFeatures
543-
? (i + 1) * (Inputs[i]->HasFocusFunction ? 1000 : 1)
544-
: 0.;
546+
Weights[i] =
547+
Inputs[i]->NumFeatures
548+
? static_cast<double>((i + 1) *
549+
(Inputs[i]->HasFocusFunction ? 1000 : 1))
550+
: 0.;
545551
}
546552

547553
if (FeatureDebug) {

libfuzzer/FuzzerDataFlowTrace.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ bool BlockCoverage::AppendCoverage(std::istream &IN) {
6060
CoveredBlocks.push_back(BB);
6161
}
6262
if (CoveredBlocks.empty()) return false;
63+
// Ensures no CoverageVector is longer than UINT32_MAX.
6364
uint32_t NumBlocks = CoveredBlocks.back();
6465
CoveredBlocks.pop_back();
6566
for (auto BB : CoveredBlocks)
@@ -200,7 +201,8 @@ bool DataFlowTrace::Init(const std::string &DirPath, std::string *FocusFunction,
200201
Printf("INFO: AUTOFOCUS: %zd %s\n", FocusFuncIdx,
201202
FunctionNames[FocusFuncIdx].c_str());
202203
for (size_t i = 0; i < NumFunctions; i++) {
203-
if (!Weights[i]) continue;
204+
if (Weights[i] == 0.0)
205+
continue;
204206
Printf(" [%zd] W %g\tBB-tot %u\tBB-cov %u\tEntryFreq %u:\t%s\n", i,
205207
Weights[i], Coverage.GetNumberOfBlocks(i),
206208
Coverage.GetNumberOfCoveredBlocks(i), Coverage.GetCounter(i, 0),

libfuzzer/FuzzerDataFlowTrace.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ int CollectDataFlow(const std::string &DFTBinary, const std::string &DirPath,
4242
const Vector<SizedFile> &CorporaFiles);
4343

4444
class BlockCoverage {
45-
public:
45+
public:
46+
// These functions guarantee no CoverageVector is longer than UINT32_MAX.
4647
bool AppendCoverage(std::istream &IN);
4748
bool AppendCoverage(const std::string &S);
4849

4950
size_t NumCoveredFunctions() const { return Functions.size(); }
5051

5152
uint32_t GetCounter(size_t FunctionId, size_t BasicBlockId) {
5253
auto It = Functions.find(FunctionId);
53-
if (It == Functions.end()) return 0;
54+
if (It == Functions.end())
55+
return 0;
5456
const auto &Counters = It->second;
5557
if (BasicBlockId < Counters.size())
5658
return Counters[BasicBlockId];
@@ -61,7 +63,7 @@ class BlockCoverage {
6163
auto It = Functions.find(FunctionId);
6264
if (It == Functions.end()) return 0;
6365
const auto &Counters = It->second;
64-
return Counters.size();
66+
return static_cast<uint32_t>(Counters.size());
6567
}
6668

6769
uint32_t GetNumberOfCoveredBlocks(size_t FunctionId) {
@@ -78,8 +80,7 @@ class BlockCoverage {
7880
Vector<double> FunctionWeights(size_t NumFunctions) const;
7981
void clear() { Functions.clear(); }
8082

81-
private:
82-
83+
private:
8384
typedef Vector<uint32_t> CoverageVector;
8485

8586
uint32_t NumberOfCoveredBlocks(const CoverageVector &Counters) const {
@@ -91,7 +92,8 @@ class BlockCoverage {
9192
}
9293

9394
uint32_t NumberOfUncoveredBlocks(const CoverageVector &Counters) const {
94-
return Counters.size() - NumberOfCoveredBlocks(Counters);
95+
return static_cast<uint32_t>(Counters.size()) -
96+
NumberOfCoveredBlocks(Counters);
9597
}
9698

9799
uint32_t SmallestNonZeroCounter(const CoverageVector &Counters) const {

libfuzzer/FuzzerDictionary.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ template <size_t kMaxSizeT> class FixedWord {
2323
public:
2424
static const size_t kMaxSize = kMaxSizeT;
2525
FixedWord() {}
26-
FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
26+
FixedWord(const uint8_t *B, size_t S) { Set(B, S); }
2727

28-
void Set(const uint8_t *B, uint8_t S) {
28+
void Set(const uint8_t *B, size_t S) {
29+
static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
30+
"FixedWord::kMaxSizeT cannot fit in a uint8_t.");
2931
assert(S <= kMaxSize);
3032
memcpy(Data, B, S);
31-
Size = S;
33+
Size = static_cast<uint8_t>(S);
3234
}
3335

3436
bool operator==(const FixedWord<kMaxSize> &w) const {

libfuzzer/FuzzerDriver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ static bool ParseOneFlag(const char *Param) {
159159
const char *Str = FlagValue(Param, Name);
160160
if (Str) {
161161
if (FlagDescriptions[F].IntFlag) {
162-
int Val = MyStol(Str);
163-
*FlagDescriptions[F].IntFlag = Val;
162+
auto Val = MyStol(Str);
163+
*FlagDescriptions[F].IntFlag = static_cast<int>(Val);
164164
if (Flags.verbosity >= 2)
165165
Printf("Flag: %s %d\n", Name, Val);
166166
return true;
167167
} else if (FlagDescriptions[F].UIntFlag) {
168-
unsigned int Val = std::stoul(Str);
169-
*FlagDescriptions[F].UIntFlag = Val;
168+
auto Val = std::stoul(Str);
169+
*FlagDescriptions[F].UIntFlag = static_cast<unsigned int>(Val);
170170
if (Flags.verbosity >= 2)
171171
Printf("Flag: %s %u\n", Name, Val);
172172
return true;
@@ -789,8 +789,8 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
789789
unsigned Seed = Flags.seed;
790790
// Initialize Seed.
791791
if (Seed == 0)
792-
Seed =
793-
std::chrono::system_clock::now().time_since_epoch().count() + GetPid();
792+
Seed = static_cast<unsigned>(
793+
std::chrono::system_clock::now().time_since_epoch().count() + GetPid());
794794
if (Flags.verbosity)
795795
Printf("INFO: Seed: %u\n", Seed);
796796

libfuzzer/FuzzerFork.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ struct GlobalEnv {
142142
CollectDFT(SF);
143143
}
144144
auto Time2 = std::chrono::system_clock::now();
145-
Job->DftTimeInSeconds = duration_cast<seconds>(Time2 - Time1).count();
145+
auto DftTimeInSeconds = duration_cast<seconds>(Time2 - Time1).count();
146+
assert(DftTimeInSeconds < std::numeric_limits<int>::max());
147+
Job->DftTimeInSeconds = static_cast<int>(DftTimeInSeconds);
146148
}
147149
if (!Seeds.empty()) {
148150
Job->SeedListPath =
@@ -314,8 +316,11 @@ void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
314316
Env.Files.push_back(File.File);
315317
} else {
316318
auto CFPath = DirPlusFile(Env.TempDir, "merge.txt");
317-
CrashResistantMerge(Env.Args, {}, SeedFiles, &Env.Files, {}, &Env.Features,
318-
{}, &Env.Cov, CFPath, false);
319+
Set<uint32_t> NewFeatures, NewCov;
320+
CrashResistantMerge(Env.Args, {}, SeedFiles, &Env.Files, Env.Features,
321+
&NewFeatures, Env.Cov, &NewCov, CFPath, false);
322+
Env.Features.insert(NewFeatures.begin(), NewFeatures.end());
323+
Env.Cov.insert(NewFeatures.begin(), NewFeatures.end());
319324
RemoveFile(CFPath);
320325
}
321326
Printf("INFO: -fork=%d: %zd seed inputs, starting to fuzz in %s\n", NumJobs,

libfuzzer/FuzzerIO.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path) {
9090
fclose(Out);
9191
}
9292

93-
void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
94-
long *Epoch, size_t MaxSize, bool ExitOnError) {
93+
void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V, long *Epoch,
94+
size_t MaxSize, bool ExitOnError,
95+
Vector<std::string> *VPaths) {
9596
long E = Epoch ? *Epoch : 0;
9697
Vector<std::string> Files;
9798
ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
@@ -103,12 +104,14 @@ void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
103104
if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
104105
Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
105106
auto S = FileToVector(X, MaxSize, ExitOnError);
106-
if (!S.empty())
107+
if (!S.empty()) {
107108
V->push_back(S);
109+
if (VPaths)
110+
VPaths->push_back(X);
111+
}
108112
}
109113
}
110114

111-
112115
void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V) {
113116
Vector<std::string> Files;
114117
ListFilesInDirRecursive(Dir, 0, &Files, /*TopDir*/true);

0 commit comments

Comments
 (0)