1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/private/base/SkAssert.h"
9 #include "include/private/base/SkMalloc.h"
10 #include "tests/Test.h"
11
12 #include <cstddef>
13 #include <cstdint>
14
DEF_TEST(SkFree_SafeToPassNull,reporter)15 DEF_TEST(SkFree_SafeToPassNull, reporter) {
16 // This test passes by not crashing
17 sk_free(nullptr);
18 }
19
DEF_TEST(SkCalloc_DataIsZeroInitializedAndWriteable,reporter)20 DEF_TEST(SkCalloc_DataIsZeroInitializedAndWriteable, reporter) {
21 constexpr size_t alloc_count = 100;
22
23 uint8_t* bytes = (uint8_t*) sk_calloc_throw(alloc_count); // provide num bytes directly
24 SkASSERT_RELEASE(bytes != nullptr);
25 for (size_t i = 0; i < alloc_count; i++) {
26 REPORTER_ASSERT(reporter, bytes[i] == 0);
27 bytes[i] = (uint8_t)i;
28 }
29 sk_free(bytes);
30
31 int32_t* ints = (int32_t*) sk_calloc_throw(alloc_count, sizeof(int32_t)); // count + elem size
32 SkASSERT_RELEASE(ints != nullptr);
33 for (size_t i = 0; i < alloc_count; i++) {
34 REPORTER_ASSERT(reporter, ints[i] == 0);
35 ints[i] = (int32_t)i;
36 }
37 sk_free(ints);
38 }
39
DEF_TEST(SkMalloc_DataIsWriteable,reporter)40 DEF_TEST(SkMalloc_DataIsWriteable, reporter) {
41 constexpr size_t alloc_count = 100;
42
43 uint8_t* bytes = (uint8_t*) sk_malloc_throw(alloc_count); // provide num bytes directly
44 SkASSERT_RELEASE(bytes != nullptr);
45 for (size_t i = 0; i < alloc_count; i++) {
46 bytes[i] = (uint8_t)i;
47 }
48 sk_free(bytes);
49
50 int32_t* ints = (int32_t*) sk_malloc_throw(alloc_count, sizeof(int32_t)); // count + elem size
51 SkASSERT_RELEASE(ints != nullptr);
52 for (size_t i = 0; i < alloc_count; i++) {
53 ints[i] = (int32_t)i;
54 }
55 sk_free(ints);
56 }
57
DEF_TEST(SkRealloc_DataIsWriteableAndEventuallyFreed,reporter)58 DEF_TEST(SkRealloc_DataIsWriteableAndEventuallyFreed, reporter) {
59 // Calling sk_realloc_throw with null should be treated as if it was sk_malloc_throw
60 uint8_t* bytes = (uint8_t*) sk_realloc_throw(nullptr, 10);
61 SkASSERT_RELEASE(bytes != nullptr);
62 // Make sure those 10 bytes are writeable
63 for (size_t i = 0; i < 10; i++) {
64 bytes[i] = (uint8_t)i;
65 }
66
67 // Make it smaller
68 bytes = (uint8_t*) sk_realloc_throw(bytes, 5);
69 SkASSERT_RELEASE(bytes != nullptr);
70 // Make sure those 5 bytes are still writeable and contain the previous values
71 for (int i = 0; i < 5; i++) {
72 REPORTER_ASSERT(reporter, bytes[i] == i, "bytes[%d] != %d", i, i);
73 bytes[i] = (uint8_t)i + 17;
74 }
75
76 // Make it bigger
77 bytes = (uint8_t*) sk_realloc_throw(bytes, 20, sizeof(uint8_t)); // count + elem size
78 SkASSERT_RELEASE(bytes != nullptr);
79 // Make sure the first 5 bytes are still writeable and contain the previous values
80 for (int i = 0; i < 5; i++) {
81 REPORTER_ASSERT(reporter, bytes[i] == (i + 17), "bytes[%d] != %d", i, i+17);
82 bytes[i] = (uint8_t)i + 43;
83 }
84 // The next 15 bytes are uninitialized, so just make sure we can write to them.
85 for (int i = 5; i < 20; i++) {
86 bytes[i] = (uint8_t)i + 43;
87 }
88
89 // This should free the memory and return nullptr.
90 bytes = (uint8_t*) sk_realloc_throw(bytes, 0);
91 REPORTER_ASSERT(reporter, bytes == nullptr);
92 // We run our tests with LeakSanitizer, so if bytes is *not* freed, we should see a failure.
93 }
94