• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <cstring>
7 
8 #include <xnnpack/allocator.h>
9 #include <xnnpack/common.h>
10 
11 #include <gtest/gtest.h>
12 
TEST(JIT_MEMORY,allocate_and_release_empty_code)13 TEST(JIT_MEMORY, allocate_and_release_empty_code) {
14   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
15   xnn_code_buffer b;
16   ASSERT_EQ(xnn_status_success, xnn_allocate_code_memory(&b, XNN_DEFAULT_CODE_BUFFER_SIZE));
17 #if XNN_PLATFORM_JIT
18   ASSERT_EQ(xnn_status_success, xnn_finalize_code_memory(&b));
19 #endif  // XNN_PLATFORM_JIT
20   ASSERT_EQ(xnn_status_success, xnn_release_code_memory(&b));
21 }
22 
TEST(JIT_MEMORY,allocate_and_release_junk_code)23 TEST(JIT_MEMORY, allocate_and_release_junk_code) {
24   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
25   xnn_code_buffer b;
26   ASSERT_EQ(xnn_status_success, xnn_allocate_code_memory(&b, XNN_DEFAULT_CODE_BUFFER_SIZE));
27   std::string junk = "1234";
28   std::memcpy(b.start, junk.data(), junk.length());
29   b.size = junk.length();
30 #if XNN_PLATFORM_JIT
31   ASSERT_EQ(xnn_status_success, xnn_finalize_code_memory(&b));
32   ASSERT_GT(XNN_DEFAULT_CODE_BUFFER_SIZE, b.capacity);
33 #endif  // XNN_PLATFORM_JIT
34   ASSERT_EQ(xnn_status_success, xnn_release_code_memory(&b));
35   ASSERT_EQ(nullptr, b.start);
36   ASSERT_EQ(0, b.size);
37   ASSERT_EQ(0, b.capacity);
38 }
39 
TEST(JIT_MEMORY,allocate_and_release_code_buffer_with_no_capacity)40 TEST(JIT_MEMORY, allocate_and_release_code_buffer_with_no_capacity) {
41   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
42   xnn_code_buffer b;
43   b.capacity = 0;
44   ASSERT_EQ(xnn_status_success, xnn_release_code_memory(&b));
45 }
46 
TEST(JIT_MEMORY,grow_memory)47 TEST(JIT_MEMORY, grow_memory) {
48   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
49   xnn_code_buffer b;
50   ASSERT_EQ(xnn_status_success, xnn_allocate_code_memory(&b, 8));
51   size_t original_capacity = b.capacity;
52   constexpr size_t junk_len = 4;
53   b.size += junk_len;
54   ASSERT_EQ(b.size, 4);
55   const uintptr_t old_code = reinterpret_cast<uintptr_t>(b.start);
56 
57   // This should be a no-op, since we have enough space.
58   ASSERT_EQ(xnn_status_success, xnn_reserve_code_memory(&b, 4));
59   ASSERT_EQ(old_code, reinterpret_cast<uintptr_t>(b.start));
60   ASSERT_EQ(original_capacity, b.capacity);
61 
62   // Simulate copying bytes until the memory is full.
63   b.size += original_capacity - junk_len;
64   ASSERT_EQ(b.size, b.capacity);
65 
66   const size_t old_size = b.size;
67   ASSERT_EQ(xnn_status_success, xnn_reserve_code_memory(&b, 4));
68 
69   // After growing, the new capacity should be bigger than the old one.
70   ASSERT_LT(original_capacity, b.capacity);
71   // At least 4 bytes free.
72   ASSERT_GE(b.capacity, b.size + 4);
73   // But size stays the same.
74   ASSERT_EQ(old_size, b.size);
75 
76   ASSERT_EQ(xnn_status_success, xnn_release_code_memory(&b));
77 }
78 
TEST(JIT_MEMORY,finalize_twice)79 TEST(JIT_MEMORY, finalize_twice) {
80   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
81   xnn_code_buffer b;
82   ASSERT_EQ(xnn_status_success, xnn_allocate_code_memory(&b, XNN_DEFAULT_CODE_BUFFER_SIZE));
83   const std::string junk = "1234";
84   std::memcpy(b.start, junk.data(), junk.length());
85   b.size += junk.length();
86   ASSERT_EQ(b.size, 4);
87 
88 #if XNN_PLATFORM_JIT
89   ASSERT_EQ(xnn_status_success, xnn_finalize_code_memory(&b));
90 #endif
91   const size_t capacity = b.capacity;
92   // Finalizing twice does not error.
93 #if XNN_PLATFORM_JIT
94   ASSERT_EQ(xnn_status_success, xnn_finalize_code_memory(&b));
95 #endif
96   // Capacity does not change.
97   ASSERT_EQ(capacity, b.capacity);
98   ASSERT_EQ(4, b.size);
99 
100   ASSERT_EQ(xnn_status_success, xnn_release_code_memory(&b));
101 }
102