SDL_test_fuzzer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * Fuzzer functions of SDL test framework.
  20. *
  21. * This code is a part of the SDL test library, not the main SDL library.
  22. */
  23. /*
  24. Data generators for fuzzing test data in a reproducible way.
  25. */
  26. #ifndef SDL_test_fuzzer_h_
  27. #define SDL_test_fuzzer_h_
  28. #include <SDL3/SDL_stdinc.h>
  29. #include <SDL3/SDL_begin_code.h>
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /*
  35. Based on GSOC code by Markus Kauppila <markus.kauppila@gmail.com>
  36. */
  37. /**
  38. * Note: The fuzzer implementation uses a static instance of random context
  39. * internally which makes it thread-UNsafe.
  40. */
  41. /**
  42. * Initializes the fuzzer for a test
  43. *
  44. * \param execKey Execution "Key" that initializes the random number generator uniquely for the test.
  45. *
  46. */
  47. void SDLCALL SDLTest_FuzzerInit(Uint64 execKey);
  48. /**
  49. * Returns a random Uint8
  50. *
  51. * \returns a generated integer
  52. */
  53. Uint8 SDLCALL SDLTest_RandomUint8(void);
  54. /**
  55. * Returns a random Sint8
  56. *
  57. * \returns a generated signed integer
  58. */
  59. Sint8 SDLCALL SDLTest_RandomSint8(void);
  60. /**
  61. * Returns a random Uint16
  62. *
  63. * \returns a generated integer
  64. */
  65. Uint16 SDLCALL SDLTest_RandomUint16(void);
  66. /**
  67. * Returns a random Sint16
  68. *
  69. * \returns a generated signed integer
  70. */
  71. Sint16 SDLCALL SDLTest_RandomSint16(void);
  72. /**
  73. * Returns a random integer
  74. *
  75. * \returns a generated integer
  76. */
  77. Sint32 SDLCALL SDLTest_RandomSint32(void);
  78. /**
  79. * Returns a random positive integer
  80. *
  81. * \returns a generated integer
  82. */
  83. Uint32 SDLCALL SDLTest_RandomUint32(void);
  84. /**
  85. * Returns random Uint64.
  86. *
  87. * \returns a generated integer
  88. */
  89. Uint64 SDLTest_RandomUint64(void);
  90. /**
  91. * Returns random Sint64.
  92. *
  93. * \returns a generated signed integer
  94. */
  95. Sint64 SDLCALL SDLTest_RandomSint64(void);
  96. /**
  97. * \returns a random float in range [0.0 - 1.0]
  98. */
  99. float SDLCALL SDLTest_RandomUnitFloat(void);
  100. /**
  101. * \returns a random double in range [0.0 - 1.0]
  102. */
  103. double SDLCALL SDLTest_RandomUnitDouble(void);
  104. /**
  105. * \returns a random float.
  106. *
  107. */
  108. float SDLCALL SDLTest_RandomFloat(void);
  109. /**
  110. * \returns a random double.
  111. *
  112. */
  113. double SDLCALL SDLTest_RandomDouble(void);
  114. /**
  115. * Returns a random boundary value for Uint8 within the given boundaries.
  116. * Boundaries are inclusive, see the usage examples below. If validDomain
  117. * is true, the function will only return valid boundaries, otherwise non-valid
  118. * boundaries are also possible.
  119. * If boundary1 > boundary2, the values are swapped
  120. *
  121. * Usage examples:
  122. * RandomUint8BoundaryValue(10, 20, true) returns 10, 11, 19 or 20
  123. * RandomUint8BoundaryValue(1, 20, false) returns 0 or 21
  124. * RandomUint8BoundaryValue(0, 99, false) returns 100
  125. * RandomUint8BoundaryValue(0, 255, false) returns 0 (error set)
  126. *
  127. * \param boundary1 Lower boundary limit
  128. * \param boundary2 Upper boundary limit
  129. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  130. *
  131. * \returns a random boundary value for the given range and domain or 0 with error set
  132. */
  133. Uint8 SDLCALL SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, bool validDomain);
  134. /**
  135. * Returns a random boundary value for Uint16 within the given boundaries.
  136. * Boundaries are inclusive, see the usage examples below. If validDomain
  137. * is true, the function will only return valid boundaries, otherwise non-valid
  138. * boundaries are also possible.
  139. * If boundary1 > boundary2, the values are swapped
  140. *
  141. * Usage examples:
  142. * RandomUint16BoundaryValue(10, 20, true) returns 10, 11, 19 or 20
  143. * RandomUint16BoundaryValue(1, 20, false) returns 0 or 21
  144. * RandomUint16BoundaryValue(0, 99, false) returns 100
  145. * RandomUint16BoundaryValue(0, 0xFFFF, false) returns 0 (error set)
  146. *
  147. * \param boundary1 Lower boundary limit
  148. * \param boundary2 Upper boundary limit
  149. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  150. *
  151. * \returns a random boundary value for the given range and domain or 0 with error set
  152. */
  153. Uint16 SDLCALL SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, bool validDomain);
  154. /**
  155. * Returns a random boundary value for Uint32 within the given boundaries.
  156. * Boundaries are inclusive, see the usage examples below. If validDomain
  157. * is true, the function will only return valid boundaries, otherwise non-valid
  158. * boundaries are also possible.
  159. * If boundary1 > boundary2, the values are swapped
  160. *
  161. * Usage examples:
  162. * RandomUint32BoundaryValue(10, 20, true) returns 10, 11, 19 or 20
  163. * RandomUint32BoundaryValue(1, 20, false) returns 0 or 21
  164. * RandomUint32BoundaryValue(0, 99, false) returns 100
  165. * RandomUint32BoundaryValue(0, 0xFFFFFFFF, false) returns 0 (with error set)
  166. *
  167. * \param boundary1 Lower boundary limit
  168. * \param boundary2 Upper boundary limit
  169. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  170. *
  171. * \returns a random boundary value for the given range and domain or 0 with error set
  172. */
  173. Uint32 SDLCALL SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, bool validDomain);
  174. /**
  175. * Returns a random boundary value for Uint64 within the given boundaries.
  176. * Boundaries are inclusive, see the usage examples below. If validDomain
  177. * is true, the function will only return valid boundaries, otherwise non-valid
  178. * boundaries are also possible.
  179. * If boundary1 > boundary2, the values are swapped
  180. *
  181. * Usage examples:
  182. * RandomUint64BoundaryValue(10, 20, true) returns 10, 11, 19 or 20
  183. * RandomUint64BoundaryValue(1, 20, false) returns 0 or 21
  184. * RandomUint64BoundaryValue(0, 99, false) returns 100
  185. * RandomUint64BoundaryValue(0, 0xFFFFFFFFFFFFFFFF, false) returns 0 (with error set)
  186. *
  187. * \param boundary1 Lower boundary limit
  188. * \param boundary2 Upper boundary limit
  189. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  190. *
  191. * \returns a random boundary value for the given range and domain or 0 with error set
  192. */
  193. Uint64 SDLCALL SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, bool validDomain);
  194. /**
  195. * Returns a random boundary value for Sint8 within the given boundaries.
  196. * Boundaries are inclusive, see the usage examples below. If validDomain
  197. * is true, the function will only return valid boundaries, otherwise non-valid
  198. * boundaries are also possible.
  199. * If boundary1 > boundary2, the values are swapped
  200. *
  201. * Usage examples:
  202. * RandomSint8BoundaryValue(-10, 20, true) returns -11, -10, 19 or 20
  203. * RandomSint8BoundaryValue(-100, -10, false) returns -101 or -9
  204. * RandomSint8BoundaryValue(SINT8_MIN, 99, false) returns 100
  205. * RandomSint8BoundaryValue(SINT8_MIN, SINT8_MAX, false) returns SINT8_MIN (== error value) with error set
  206. *
  207. * \param boundary1 Lower boundary limit
  208. * \param boundary2 Upper boundary limit
  209. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  210. *
  211. * \returns a random boundary value for the given range and domain or SINT8_MIN with error set
  212. */
  213. Sint8 SDLCALL SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, bool validDomain);
  214. /**
  215. * Returns a random boundary value for Sint16 within the given boundaries.
  216. * Boundaries are inclusive, see the usage examples below. If validDomain
  217. * is true, the function will only return valid boundaries, otherwise non-valid
  218. * boundaries are also possible.
  219. * If boundary1 > boundary2, the values are swapped
  220. *
  221. * Usage examples:
  222. * RandomSint16BoundaryValue(-10, 20, true) returns -11, -10, 19 or 20
  223. * RandomSint16BoundaryValue(-100, -10, false) returns -101 or -9
  224. * RandomSint16BoundaryValue(SINT16_MIN, 99, false) returns 100
  225. * RandomSint16BoundaryValue(SINT16_MIN, SINT16_MAX, false) returns SINT16_MIN (== error value) with error set
  226. *
  227. * \param boundary1 Lower boundary limit
  228. * \param boundary2 Upper boundary limit
  229. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  230. *
  231. * \returns a random boundary value for the given range and domain or SINT16_MIN with error set
  232. */
  233. Sint16 SDLCALL SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, bool validDomain);
  234. /**
  235. * Returns a random boundary value for Sint32 within the given boundaries.
  236. * Boundaries are inclusive, see the usage examples below. If validDomain
  237. * is true, the function will only return valid boundaries, otherwise non-valid
  238. * boundaries are also possible.
  239. * If boundary1 > boundary2, the values are swapped
  240. *
  241. * Usage examples:
  242. * RandomSint32BoundaryValue(-10, 20, true) returns -11, -10, 19 or 20
  243. * RandomSint32BoundaryValue(-100, -10, false) returns -101 or -9
  244. * RandomSint32BoundaryValue(SINT32_MIN, 99, false) returns 100
  245. * RandomSint32BoundaryValue(SINT32_MIN, SINT32_MAX, false) returns SINT32_MIN (== error value)
  246. *
  247. * \param boundary1 Lower boundary limit
  248. * \param boundary2 Upper boundary limit
  249. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  250. *
  251. * \returns a random boundary value for the given range and domain or SINT32_MIN with error set
  252. */
  253. Sint32 SDLCALL SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, bool validDomain);
  254. /**
  255. * Returns a random boundary value for Sint64 within the given boundaries.
  256. * Boundaries are inclusive, see the usage examples below. If validDomain
  257. * is true, the function will only return valid boundaries, otherwise non-valid
  258. * boundaries are also possible.
  259. * If boundary1 > boundary2, the values are swapped
  260. *
  261. * Usage examples:
  262. * RandomSint64BoundaryValue(-10, 20, true) returns -11, -10, 19 or 20
  263. * RandomSint64BoundaryValue(-100, -10, false) returns -101 or -9
  264. * RandomSint64BoundaryValue(SINT64_MIN, 99, false) returns 100
  265. * RandomSint64BoundaryValue(SINT64_MIN, SINT64_MAX, false) returns SINT64_MIN (== error value) and error set
  266. *
  267. * \param boundary1 Lower boundary limit
  268. * \param boundary2 Upper boundary limit
  269. * \param validDomain Should the generated boundary be valid (=within the bounds) or not?
  270. *
  271. * \returns a random boundary value for the given range and domain or SINT64_MIN with error set
  272. */
  273. Sint64 SDLCALL SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, bool validDomain);
  274. /**
  275. * Returns integer in range [min, max] (inclusive).
  276. * Min and max values can be negative values.
  277. * If Max in smaller than min, then the values are swapped.
  278. * Min and max are the same value, that value will be returned.
  279. *
  280. * \param min Minimum inclusive value of returned random number
  281. * \param max Maximum inclusive value of returned random number
  282. *
  283. * \returns a generated random integer in range
  284. */
  285. Sint32 SDLCALL SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max);
  286. /**
  287. * Generates random null-terminated string. The minimum length for
  288. * the string is 1 character, maximum length for the string is 255
  289. * characters and it can contain ASCII characters from 32 to 126.
  290. *
  291. * Note: Returned string needs to be deallocated.
  292. *
  293. * \returns a newly allocated random string; or NULL if length was invalid or string could not be allocated.
  294. */
  295. char * SDLCALL SDLTest_RandomAsciiString(void);
  296. /**
  297. * Generates random null-terminated string. The maximum length for
  298. * the string is defined by the maxLength parameter.
  299. * String can contain ASCII characters from 32 to 126.
  300. *
  301. * Note: Returned string needs to be deallocated.
  302. *
  303. * \param maxLength The maximum length of the generated string.
  304. *
  305. * \returns a newly allocated random string; or NULL if maxLength was invalid or string could not be allocated.
  306. */
  307. char * SDLCALL SDLTest_RandomAsciiStringWithMaximumLength(int maxLength);
  308. /**
  309. * Generates random null-terminated string. The length for
  310. * the string is defined by the size parameter.
  311. * String can contain ASCII characters from 32 to 126.
  312. *
  313. * Note: Returned string needs to be deallocated.
  314. *
  315. * \param size The length of the generated string
  316. *
  317. * \returns a newly allocated random string; or NULL if size was invalid or string could not be allocated.
  318. */
  319. char * SDLCALL SDLTest_RandomAsciiStringOfSize(int size);
  320. /**
  321. * Get the invocation count for the fuzzer since last ...FuzzerInit.
  322. *
  323. * \returns the invocation count.
  324. */
  325. int SDLCALL SDLTest_GetFuzzerInvocationCount(void);
  326. /* Ends C function definitions when using C++ */
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #include <SDL3/SDL_close_code.h>
  331. #endif /* SDL_test_fuzzer_h_ */