-
Notifications
You must be signed in to change notification settings - Fork 123
Add malloc tests #20
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
Add malloc tests #20
Changes from all commits
f6497e6
a9615bc
e42551b
0ff2301
24ba068
942313f
057595a
c555bf7
d2909ee
a3fb2b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #include <cerrno> | ||
| #include <malloc.h> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file has been deprecated for about 20 years and causes a hard error on FreeBSD, it probably should be stdlib.h. |
||
| #include <snmalloc.h> | ||
|
|
||
| using namespace snmalloc; | ||
|
|
||
| void check_result(size_t size, size_t align, void* p, int err, bool null) | ||
| { | ||
| if (errno != err) | ||
| abort(); | ||
|
|
||
| if (null) | ||
| { | ||
| if (p != nullptr) | ||
| abort(); | ||
| } | ||
| else | ||
| { | ||
| if (malloc_usable_size(p) < size) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a non-standard function, so we can't expect it to be in any headers. It isn't on FreeBSD or macOS. |
||
| abort(); | ||
|
|
||
| if (((uintptr_t)p % align) != 0) | ||
| abort(); | ||
|
|
||
| free(p); | ||
| } | ||
| } | ||
|
|
||
| void test_calloc(size_t nmemb, size_t size, int err, bool null) | ||
| { | ||
| fprintf(stderr, "calloc(%d, %d)\n", (int)nmemb, (int)size); | ||
| errno = 0; | ||
| void* p = calloc(nmemb, size); | ||
|
|
||
| if ((p != nullptr) && (errno == 0)) | ||
| { | ||
| for (size_t i = 0; i < (size * nmemb); i++) | ||
| { | ||
| if (((uint8_t*)p)[i] != 0) | ||
| abort(); | ||
| } | ||
| } | ||
| check_result(nmemb * size, 1, p, err, null); | ||
| } | ||
|
|
||
| void test_realloc(void* p, size_t size, int err, bool null) | ||
| { | ||
| fprintf(stderr, "realloc(%p(%d), %d)\n", p, int(size), (int)size); | ||
| errno = 0; | ||
| p = realloc(p, size); | ||
| check_result(size, 1, p, err, null); | ||
| } | ||
|
|
||
| void test_posix_memalign(size_t size, size_t align, int err, bool null) | ||
| { | ||
| fprintf(stderr, "posix_memalign(&p, %d, %d)\n", (int)align, (int)size); | ||
| void* p = nullptr; | ||
| errno = posix_memalign(&p, align, size); | ||
| check_result(size, align, p, err, null); | ||
| } | ||
|
|
||
| void test_memalign(size_t size, size_t align, int err, bool null) | ||
| { | ||
| fprintf(stderr, "memalign(%d, %d)\n", (int)align, (int)size); | ||
| errno = 0; | ||
| void* p = memalign(align, size); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memalign is deprecated in glibc and doesn't exist anywhere else, so we probably need to explicitly declare it if we want to test our version. |
||
| check_result(size, align, p, err, null); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| UNUSED(argc); | ||
| UNUSED(argv); | ||
|
|
||
| constexpr int SUCCESS = 0; | ||
|
|
||
| test_calloc(0, 0, SUCCESS, false); | ||
|
|
||
| for (uint8_t sc = 0; sc < NUM_SIZECLASSES; sc++) | ||
| { | ||
| const size_t size = sizeclass_to_size(sc); | ||
|
|
||
| bool overflow = false; | ||
| for (size_t n = 1; bits::umul(size, n, overflow) <= SUPERSLAB_SIZE; n *= 5) | ||
| { | ||
| if (overflow) | ||
| break; | ||
|
|
||
| test_calloc(n, size, SUCCESS, false); | ||
| test_calloc(n, 0, SUCCESS, false); | ||
| } | ||
| test_calloc(0, size, SUCCESS, false); | ||
|
|
||
| test_realloc(malloc(size), size, SUCCESS, false); | ||
| test_realloc(malloc(size), 0, SUCCESS, true); | ||
| test_realloc(nullptr, size, SUCCESS, false); | ||
| test_realloc(malloc(size), (size_t)-1, ENOMEM, true); | ||
| } | ||
|
|
||
| test_posix_memalign(0, 0, EINVAL, true); | ||
| test_posix_memalign((size_t)-1, 0, EINVAL, true); | ||
|
|
||
| for (size_t align = sizeof(size_t); align <= SUPERSLAB_SIZE; align <<= 1) | ||
| { | ||
| for (uint8_t sc = 0; sc < NUM_SIZECLASSES; sc++) | ||
| { | ||
| const size_t size = sizeclass_to_size(sc); | ||
| test_posix_memalign(size, align, SUCCESS, false); | ||
| test_posix_memalign(size, 0, EINVAL, true); | ||
| test_memalign(size, align, SUCCESS, false); | ||
| } | ||
| test_posix_memalign(0, align, SUCCESS, false); | ||
| test_posix_memalign((size_t)-1, align, ENOMEM, true); | ||
| test_posix_memalign(0, align + 1, EINVAL, true); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.