Skip to content

Commit 6cd0c7f

Browse files
committed
[legacy, test] Add e test to verify the heap/malloc
Signed-off-by: Douglas Reis <[email protected]>
1 parent fcbe59e commit 6cd0c7f

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

sw/legacy/test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ add_custom_command(
1919
COMMAND ${CMAKE_OBJCOPY} -O binary "$<TARGET_FILE:spi_test>" "$<TARGET_FILE:spi_test>.bin"
2020
COMMAND srec_cat "$<TARGET_FILE:spi_test>.bin" -binary -offset 0x0000 -byte-swap 4 -o "$<TARGET_FILE:spi_test>.vmem" -vmem
2121
VERBATIM)
22+
23+
add_executable(heap_test heap_test.c)
24+
target_link_libraries(heap_test common syscall)
25+
target_link_options(heap_test PRIVATE "-specs=nosys.specs")
26+
27+
add_custom_command(
28+
TARGET heap_test POST_BUILD
29+
COMMAND ${CMAKE_OBJCOPY} -O binary "$<TARGET_FILE:heap_test>" "$<TARGET_FILE:heap_test>.bin"
30+
COMMAND srec_cat "$<TARGET_FILE:heap_test>.bin" -binary -offset 0x0000 -byte-swap 4 -o "$<TARGET_FILE:heap_test>.vmem" -vmem
31+
VERBATIM)

sw/legacy/test/heap_test.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright lowRISC contributors.
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include <stdbool.h>
6+
#include <stdlib.h>
7+
8+
#include "dev_access.h"
9+
#include "sonata_system.h"
10+
#include "timer.h"
11+
12+
extern char _heap_size[];
13+
#define TEST_DATA (0xDEADBEEF)
14+
// Total ram size minus space for .text and .rodata
15+
#define TEST_SIZE_BYTES ((uint32_t)_heap_size / 4)
16+
#define TEST_SIZE_WORDS TEST_SIZE_BYTES / 4
17+
18+
int pass() {
19+
puts("Test Passed");
20+
21+
while (true);
22+
return 0;
23+
}
24+
25+
int fail() {
26+
puts("Test Failed");
27+
28+
while (true);
29+
return -1;
30+
}
31+
32+
int memory_test(uint32_t *pointer, size_t size) {
33+
putstr("Memory_test testing 0x");
34+
puthex(size * sizeof(uint32_t));
35+
putstr(" bytes at addr: 0x");
36+
puthex(pointer);
37+
putstr("...");
38+
// Simple word write/read test of most of the RAM; some RAM is used by the
39+
// code itself, global static and stack...
40+
for (int i = 0; i < size; i++) {
41+
pointer[i] = TEST_DATA + i;
42+
}
43+
// Read back data and check it is correct
44+
for (int i = 0; i < size; i++) {
45+
if (pointer[i] != TEST_DATA + i) {
46+
puts("Failed");
47+
return -1;
48+
}
49+
}
50+
51+
// Byte read/write test
52+
uint8_t *btest = (uint8_t *)pointer;
53+
for (int i = 0; i < size; i++) {
54+
btest[i * 4 + (i & 3)] ^= i;
55+
}
56+
// Read back data and check it is correct
57+
for (int i = 0; i < size; i++) {
58+
uint32_t orig = TEST_DATA + i;
59+
uint32_t mod = orig ^ (uint8_t)i << (8 * (i & 3));
60+
if (pointer[i] != mod) {
61+
puts("Failed");
62+
return -1;
63+
}
64+
}
65+
puts("Passed");
66+
return 0;
67+
}
68+
69+
int main(void) {
70+
uart_init(DEFAULT_UART);
71+
72+
putstr("\n>>> Starting ");
73+
puts(__FILE__);
74+
75+
// Allocate a chunk of memory, run the test and free the pointer.
76+
uint32_t *test_array1 = (uint32_t *)malloc(TEST_SIZE_BYTES);
77+
if (test_array1 == NULL) {
78+
puts("Failed to allocate memory");
79+
return fail();
80+
}
81+
int ret = memory_test(test_array1, TEST_SIZE_WORDS);
82+
free(test_array1);
83+
if (ret == -1) {
84+
return fail();
85+
}
86+
87+
// Allocate another chunk of memory, Check that the pointers point to the same address, run the test, and let the
88+
// pointer allocated.
89+
uint32_t *test_array2 = (uint32_t *)malloc(TEST_SIZE_BYTES);
90+
if (test_array2 == NULL) {
91+
puts("Failed to allocate memory");
92+
return fail();
93+
}
94+
if (test_array1 != test_array2) {
95+
puts("Failed: Second malloc should reuse freed memory.");
96+
return fail();
97+
}
98+
ret = memory_test(test_array2, TEST_SIZE_WORDS);
99+
if (ret == -1) {
100+
return fail();
101+
}
102+
103+
// Allocate another chunk of memory, Check that the pointers point to different addresses, run the test, and free the
104+
// pointer.
105+
uint32_t *test_array3 = (uint32_t *)malloc(TEST_SIZE_BYTES);
106+
if (test_array3 == NULL) {
107+
puts("Failed to allocate memory");
108+
return fail();
109+
}
110+
if (test_array3 == test_array2) {
111+
puts("Failed: Allocated unfree memory.");
112+
return fail();
113+
}
114+
ret = memory_test(test_array3, TEST_SIZE_WORDS);
115+
free(test_array2);
116+
free(test_array3);
117+
118+
return pass();
119+
}

0 commit comments

Comments
 (0)