-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add fixed point support to printf #82707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelrj-google
merged 6 commits into
llvm:main
from
michaelrj-google:libcFixedPointPrintf
Feb 27, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7c1991
[libc] Add fixed point support to printf
michaelrj-google 553c7d9
add fuzzer
michaelrj-google 2bd5302
add documentation on the fixed point conversions.
michaelrj-google 8d0db6b
address comments, add fixed point values in comments to sprintf_test
michaelrj-google 3aacbac
move type identification to nested macros for simpicity
michaelrj-google 150b0be
add proper cmake configuration variables
michaelrj-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//===-- printf_fixed_conv_fuzz.cpp ----------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// Fuzzing test for llvm-libc printf %f/e/g/a implementations. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
#include "src/stdio/snprintf.h" | ||
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/fixed_point/fx_bits.h" | ||
#include "src/__support/fixed_point/fx_rep.h" | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#include "utils/MPFRWrapper/mpfr_inc.h" | ||
|
||
constexpr int MAX_SIZE = 10000; | ||
|
||
inline bool simple_streq(char *first, char *second, int length) { | ||
for (int i = 0; i < length; ++i) | ||
if (first[i] != second[i]) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
inline int clamp(int num, int max) { | ||
if (num > max) | ||
return max; | ||
if (num < -max) | ||
return -max; | ||
return num; | ||
} | ||
|
||
enum class TestResult { | ||
Success, | ||
BufferSizeFailed, | ||
LengthsDiffer, | ||
StringsNotEqual, | ||
}; | ||
|
||
template <typename F> | ||
inline TestResult test_vals(const char *fmt, uint64_t num, int prec, | ||
int width) { | ||
typename LIBC_NAMESPACE::fixed_point::FXRep<F>::StorageType raw_num = num; | ||
|
||
auto raw_num_bits = LIBC_NAMESPACE::fixed_point::FXBits<F>(raw_num); | ||
|
||
// This needs to be a float with enough bits of precision to hold the fixed | ||
// point number. | ||
static_assert(sizeof(long double) > sizeof(long accum)); | ||
|
||
// build a long double that is equivalent to the fixed point number. | ||
long double ld_num = | ||
static_cast<long double>(raw_num_bits.get_integral()) + | ||
(static_cast<long double>(raw_num_bits.get_fraction()) / | ||
static_cast<long double>(1ll << raw_num_bits.get_exponent())); | ||
|
||
if (raw_num_bits.get_sign()) | ||
ld_num = -ld_num; | ||
|
||
// Call snprintf on a nullptr to get the buffer size. | ||
int buffer_size = LIBC_NAMESPACE::snprintf(nullptr, 0, fmt, width, prec, num); | ||
|
||
if (buffer_size < 0) | ||
return TestResult::BufferSizeFailed; | ||
|
||
char *test_buff = new char[buffer_size + 1]; | ||
char *reference_buff = new char[buffer_size + 1]; | ||
|
||
int test_result = 0; | ||
int reference_result = 0; | ||
|
||
test_result = LIBC_NAMESPACE::snprintf(test_buff, buffer_size + 1, fmt, width, | ||
prec, num); | ||
|
||
// The fixed point format is defined to be %f equivalent. | ||
reference_result = mpfr_snprintf(reference_buff, buffer_size + 1, "%*.*Lf", | ||
width, prec, ld_num); | ||
|
||
// All of these calls should return that they wrote the same amount. | ||
if (test_result != reference_result || test_result != buffer_size) | ||
return TestResult::LengthsDiffer; | ||
|
||
if (!simple_streq(test_buff, reference_buff, buffer_size)) | ||
return TestResult::StringsNotEqual; | ||
|
||
delete[] test_buff; | ||
delete[] reference_buff; | ||
return TestResult::Success; | ||
} | ||
|
||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||
// const uint8_t raw_data[] = {0x8d,0x43,0x40,0x0,0x0,0x0,}; | ||
// data = raw_data; | ||
// size = sizeof(raw_data); | ||
int prec = 0; | ||
int width = 0; | ||
|
||
LIBC_NAMESPACE::fixed_point::FXRep<long accum>::StorageType raw_num = 0; | ||
|
||
// Copy as many bytes of data as will fit into num, prec, and with. Any extras | ||
// are ignored. | ||
for (size_t cur = 0; cur < size; ++cur) { | ||
if (cur < sizeof(raw_num)) { | ||
raw_num = (raw_num << 8) + data[cur]; | ||
} else if (cur < sizeof(raw_num) + sizeof(prec)) { | ||
prec = (prec << 8) + data[cur]; | ||
} else if (cur < sizeof(raw_num) + sizeof(prec) + sizeof(width)) { | ||
width = (width << 8) + data[cur]; | ||
} | ||
} | ||
|
||
width = clamp(width, MAX_SIZE); | ||
prec = clamp(prec, MAX_SIZE); | ||
|
||
TestResult result; | ||
result = test_vals<long accum>("%*.*lk", raw_num, prec, width); | ||
if (result != TestResult::Success) | ||
__builtin_trap(); | ||
|
||
result = test_vals<unsigned long accum>("%*.*lK", raw_num, prec, width); | ||
if (result != TestResult::Success) | ||
__builtin_trap(); | ||
|
||
return 0; | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point, it might be worthwhile to add something akin to std::clamp to libc/src/__support/CPP/algorithm.h.