/src/mozilla-central/tools/fuzzing/common/FuzzingTraits.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_fuzzing_FuzzingTraits_h |
8 | | #define mozilla_fuzzing_FuzzingTraits_h |
9 | | |
10 | | #include "mozilla/Assertions.h" |
11 | | #include "mozilla/TypeTraits.h" |
12 | | #include <random> |
13 | | |
14 | | namespace mozilla { |
15 | | namespace fuzzing { |
16 | | |
17 | | /** |
18 | | * RandomNumericLimit returns either the min or max limit of an arithmetic |
19 | | * data type. |
20 | | */ |
21 | | template <typename T> |
22 | | T RandomNumericLimit() |
23 | | { |
24 | | static_assert(mozilla::IsArithmetic<T>::value == true, |
25 | | "T must be an arithmetic type"); |
26 | | return random() % 2 == 0 ? std::numeric_limits<T>::min() |
27 | | : std::numeric_limits<T>::max(); |
28 | | } |
29 | | |
30 | | /** |
31 | | * RandomInteger generates negative and positive integers in 2**n increments. |
32 | | */ |
33 | | template <typename T> |
34 | | T RandomInteger() |
35 | 0 | { |
36 | 0 | static_assert(mozilla::IsIntegral<T>::value == true, |
37 | 0 | "T must be an integral type"); |
38 | 0 | double r = static_cast<double>(random() % ((sizeof(T) * CHAR_BIT) + 1)); |
39 | 0 | T x = static_cast<T>(pow(2.0, r)) - 1; |
40 | 0 | if (std::numeric_limits<T>::is_signed && random() % 2 == 0) { |
41 | 0 | return (x * -1) - 1; |
42 | 0 | } |
43 | 0 | return x; |
44 | 0 | } |
45 | | |
46 | | /** |
47 | | * RandomIntegerRange returns a random integral within a [min, max] range. |
48 | | */ |
49 | | template <typename T> |
50 | | T RandomIntegerRange(T min, T max) |
51 | 0 | { |
52 | 0 | static_assert(mozilla::IsIntegral<T>::value == true, |
53 | 0 | "T must be an integral type"); |
54 | 0 | MOZ_ASSERT(min < max); |
55 | 0 | return static_cast<T>(random() % (max - min) + min); |
56 | 0 | } Unexecuted instantiation: unsigned long mozilla::fuzzing::RandomIntegerRange<unsigned long>(unsigned long, unsigned long) Unexecuted instantiation: unsigned char mozilla::fuzzing::RandomIntegerRange<unsigned char>(unsigned char, unsigned char) |
57 | | |
58 | | /** |
59 | | * RandomFloatingPointRange returns a random floating-point number within a |
60 | | * [min, max] range. |
61 | | */ |
62 | | template <typename T> |
63 | | T RandomFloatingPointRange(T min, T max) |
64 | | { |
65 | | static_assert(mozilla::IsFloatingPoint<T>::value == true, |
66 | | "T must be a floating point type"); |
67 | | MOZ_ASSERT(min < max); |
68 | | T x = static_cast<T>(random()) / static_cast<T>(RAND_MAX); |
69 | | return min + x * (max - min); |
70 | | } |
71 | | |
72 | | /** |
73 | | * RandomFloatingPoint returns a random floating-point number in 2**n |
74 | | * increments. |
75 | | */ |
76 | | template <typename T> |
77 | | T RandomFloatingPoint() |
78 | | { |
79 | | static_assert(mozilla::IsFloatingPoint<T>::value == true, |
80 | | "T must be a floating point type"); |
81 | | int radix = RandomIntegerRange<int>(std::numeric_limits<T>::min_exponent, |
82 | | std::numeric_limits<T>::max_exponent); |
83 | | T x = static_cast<T>(pow(2.0, static_cast<double>(radix))); |
84 | | return x * RandomFloatingPointRange<T>(-1.0, 1.0); |
85 | | } |
86 | | |
87 | | class FuzzingTraits |
88 | | { |
89 | | public: |
90 | | static unsigned int Random(unsigned int aMax); |
91 | | static bool Sometimes(unsigned int aProbability); |
92 | | /** |
93 | | * Frequency() defines how many mutations of a kind shall be applied to a |
94 | | * target buffer by using a user definable factor. The higher the factor, |
95 | | * the less mutations are being made. |
96 | | */ |
97 | | static size_t Frequency(const size_t aSize, const uint64_t aFactor); |
98 | | }; |
99 | | |
100 | | } // namespace fuzzing |
101 | | } // namespace mozilla |
102 | | |
103 | | #endif |