test_extended_memory.cpp 4.6 KB
Newer Older
B
Bucky Kittinger 已提交
1 2
#include <eosiolib/eosio.hpp>
#include <eosiolib/memory.hpp>
3 4
#include "../test_api/test_api.hpp"

5
//using namespace eosio;
6 7 8 9

void verify( const void* const ptr, const uint32_t val, const uint32_t size) {
	const char* char_ptr = (const char*)ptr;
	for (uint32_t i = 0; i < size; ++i)
10
		eosio_assert(static_cast<uint32_t>(static_cast<unsigned char>(char_ptr[i])) == val, "buffer slot doesn't match");
11 12 13 14 15 16 17 18 19 20 21 22
}

#define PRINT_PTR(x) prints("PTR : "); print((uint32_t)x, 4); prints("\n");

void test_extended_memory::test_page_memory() {
	constexpr uint32_t _64K = 64*1024;
   /*
    * Test test_extended_memory::test_page_memory `ensure initial page size`
    * Given I have not tried to increase the "program break" yet,
    * when I call sbrk(0), then I should get the end of the first page, which should be 64K.
    */
	auto prev = sbrk(0);
23
	eosio_assert(reinterpret_cast<uint32_t>(prev) == _64K, "Should initially have 1 64K page allocated");
24 25 26 27 28 29 30

   /*
    * Test test_extended_memory::test_page_memory `ensure sbrk returns previous end of program break`
    * Given I have not tried to increase memory,
    * when I call sbrk(1), then I should get the end of the first page, which should be 64K.
    */
	prev = sbrk(1);
31
	eosio_assert(reinterpret_cast<uint32_t>(prev) == _64K, "Should still be pointing to the end of the 1st 64K page");
32 33 34 35 36 37 38

   /*
    * Test test_extended_memory::test_page_memory `ensure sbrk aligns allocations`
    * Given that I allocated 1 byte via sbrk, 
    * when I call sbrk(2), then I should get 8 bytes past the previous end because of maintaining 8 byte alignment.
    */
	prev = sbrk(2);
39
	eosio_assert(reinterpret_cast<uint32_t>(prev) == _64K+8, "Should point to 8 past the end of 1st 64K page");
40 41 42 43 44 45 46

   /*
    * Test test_extended_memory::test_page_memory `ensure sbrk aligns allocations 2`
    * Given that I allocated 2 bytes via sbrk, 
    * when I call sbrk(_64K-17), then I should get 8 bytes past the previous end because of maintaining 8 byte alignment.
    */
	prev = sbrk(_64K - 17);
47
	eosio_assert(reinterpret_cast<uint32_t>(prev) == _64K+16, "Should point to 16 past the end of the 1st 64K page");
48 49

	prev = sbrk(1);
50
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 2*_64K, "Should point to the end of the 2nd 64K page");
51 52

	prev = sbrk(_64K);
53
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 2*_64K+8, "Should point to 8 past the end of the 2nd 64K page");
54 55

	prev = sbrk(_64K - 15);
56
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 3*_64K+8, "Should point to 8 past the end of the 3rd 64K page");
57 58

	prev = sbrk(2*_64K-1);
59
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 4*_64K, "Should point to the end of the 4th 64K page");
60 61

	prev = sbrk(2*_64K);
62
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 6*_64K, "Should point to the end of the 6th 64K page");
63 64

	prev = sbrk(2*_64K+1);
65
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 8*_64K, "Should point to the end of the 8th 64K page");
66 67

	prev = sbrk(6*_64K-15);
68
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 10*_64K+8, "Should point to 8 past the end of the 10th 64K page");
69 70

	prev = sbrk(0);
71
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 16*_64K, "Should point to 8 past the end of the 16th 64K page");
72 73 74 75 76 77 78 79 80
}

void test_extended_memory::test_page_memory_exceeded() {
   /*
    * Test test_extended_memory::test_page_memory_exceeded `ensure sbrk won't allocation more than 1M of memory`
    * Given that I have not tried to increase allocated memory,
    * when I increase allocated memory with sbrk(15*64K), then I should get the end of the first page.
    */
	auto prev = sbrk(15*64*1024);
81
	eosio_assert(reinterpret_cast<uint32_t>(prev) == 64*1024, "Should have allocated 1M of memory");
82 83 84 85 86

   /*
    * Test test_extended_memory::test_page_memory_exceeded `ensure sbrk won't allocation more than 1M of memory 2`
    */
   prev = sbrk(0);
87
	eosio_assert(reinterpret_cast<uint32_t>(prev) == (1024*1024), "Should have allocated 1M of memory");
88 89

	eosio_assert(reinterpret_cast<int32_t>(sbrk(1)) == -1, "sbrk should have failed for trying to allocate too much memory");
90 91 92
}

void test_extended_memory::test_page_memory_negative_bytes() {
93 94
   sbrk((uint32_t)-1);
   eosio_assert(0, "Should have thrown exception for trying to remove memory");
95 96 97 98 99
}

void test_extended_memory::test_initial_buffer() {
	// initial buffer should be exhausted at 8192 bytes
	// 8176 left ( 12 + ptr header )
100
	char* ptr1 = (char*)malloc(12);
101
	eosio_assert(ptr1 != nullptr, "should have allocated 12 char buffer");
102

103
	char* ptr2 = (char*)malloc(8159);
104
	eosio_assert(ptr2 != nullptr, "should have allocate 8159 char buffer");
105 106

	// should overrun initial heap, allocated in 2nd heap
107
	char* ptr3 = (char*)malloc(20);
108
	eosio_assert(ptr3 != nullptr, "should have allocated a 20 char buffer");
109 110 111
   verify(ptr3, 0, 20);

}