Created
December 2, 2016 13:15
-
-
Save delcypher/58f8bad4559eceaa36ec32a8d148e539 to your computer and use it in GitHub Desktop.
Fuzzing "subnormal result implies exact result with LibFuzzer"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <inttypes.h> | |
#include <math.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <string.h> | |
int is_subnormal(float f) { | |
return fpclassify(f) == FP_SUBNORMAL; | |
} | |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { | |
float a = 0.0; | |
float b = 0.0; | |
if (Size < (sizeof(a) + sizeof(b))) | |
return 0; | |
// Big enough | |
memcpy(&a, Data, sizeof(a)); | |
memcpy(&b, Data + sizeof(a), sizeof(b)); | |
float result = a + b; | |
if (!is_subnormal(result)) | |
return 0; | |
// Approximately check the addition | |
// was exact. Not sure how good this | |
// approximation is. | |
int condition = (result - b) == a; | |
if (!condition) { | |
printf("Bad value: %f\n", result); | |
printf("as hex float: %a\n", result); | |
// Show as bits | |
uint32_t result_bits = 0; | |
memcpy(&result_bits, &result, sizeof(float)); | |
printf("as bits: 0x%.8" PRIx32 "\n", result_bits); | |
abort(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment