Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🐛 [newlib] fix broken sbrk function #957

Merged
merged 5 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mimpid = 0x01040312 -> Version 01.04.03.12 -> v1.4.3.12

| Date | Version | Comment | Ticket |
|:----:|:-------:|:--------|:------:|
| 20.07.2024 | 1.10.1.7 | :bug: fix bug in `sbrk` newlib system call (causing `malloc` to provide infinite memory until heap and stack collide) | [#957](https://github.com/stnolting/neorv32/pull/957) |
| 20.07.2024 | 1.10.1.6 | SDI: remove explicit "RX clear flag"; add new flag to check the current state of the chip-select input | [#955](https://github.com/stnolting/neorv32/pull/955) |
| 19.07.2024 | 1.10.1.5 | :sparkles: add "programmable" chip-select enable/disable functionality to SPI module | [#954](https://github.com/stnolting/neorv32/pull/954) |
| 19.07.2024 | 1.10.1.4 | :bug: fix SDI "TX FIFO full" flag | [#953](https://github.com/stnolting/neorv32/pull/953) |
Expand Down
2 changes: 1 addition & 1 deletion rtl/core/neorv32_package.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package neorv32_package is

-- Architecture Constants -----------------------------------------------------------------
-- -------------------------------------------------------------------------------------------
constant hw_version_c : std_ulogic_vector(31 downto 0) := x"01100106"; -- hardware version
constant hw_version_c : std_ulogic_vector(31 downto 0) := x"01100107"; -- hardware version
constant archid_c : natural := 19; -- official RISC-V architecture ID
constant XLEN : natural := 32; -- native data path width

Expand Down
2 changes: 2 additions & 0 deletions sw/common/neorv32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ SECTIONS
.heap : ALIGN(4)
{
PROVIDE(__heap_start = .);
/* start section on WORD boundary */
. = ALIGN(4);
. = __neorv32_heap_size;
/* finish section on WORD boundary */
. = ALIGN(4);
Expand Down
45 changes: 34 additions & 11 deletions sw/example/demo_newlib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**************************************************************************/
#include <neorv32.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>


Expand All @@ -33,6 +34,9 @@ void __attribute__((destructor)) main_destructor_test(void) {

int32_t main_ret = (int32_t)neorv32_cpu_csr_read(CSR_MSCRATCH);
neorv32_uart0_printf("\nDestructor: main terminated with return/exit code %i.\n", main_ret);
if (main_ret == 7) {
neorv32_uart0_printf("exit() succeeded.\n");
}
}


Expand Down Expand Up @@ -68,34 +72,51 @@ int main() {
// say hello
neorv32_uart0_printf("<<< Newlib demo/test program >>>\n\n");


// check if newlib is really available
#ifndef __NEWLIB__
neorv32_uart0_printf("ERROR! Seems like the compiler toolchain does not support newlib...\n");
return -1;
#endif
neorv32_uart0_printf("NEWLIB version %u.%u\n\n", (uint32_t)__NEWLIB__, (uint32_t)__NEWLIB_MINOR__);


// heap size definition
volatile uint32_t max_heap = (uint32_t)__crt0_max_heap;
uint32_t max_heap = (uint32_t)&__crt0_max_heap[0];
if (max_heap > 0){
neorv32_uart0_printf("MAX heap size: %u bytes\n", max_heap);
}
else {
neorv32_uart0_printf("ERROR! No heap size defined (USER_FLAGS+='-Wl,--defsym,__neorv32_heap_size=1024')!\n");
neorv32_uart0_printf("ERROR! No heap size defined!\n");
neorv32_uart0_printf("Use <USER_FLAGS+='-Wl,--defsym,__neorv32_heap_size=1024'> to set the heap size.\n");
return -1;
}

// check if newlib is really available
#ifndef __NEWLIB__
neorv32_uart0_printf("ERROR! Seems like the compiler toolchain does not support newlib...\n");
return -1;
#endif

neorv32_uart0_printf("newlib version %i.%i\n\n", (int32_t)__NEWLIB__, (int32_t)__NEWLIB_MINOR__);

// rand test
neorv32_uart0_printf("<rand> test... ");
srand(neorv32_cpu_csr_read(CSR_CYCLE)); // set random seed
neorv32_uart0_printf("%i, %i, %i, %i\n", rand() % 100, rand() % 100, rand() % 100, rand() % 100);


char *char_buffer; // pointer for dynamic memory allocation
// time test
neorv32_uart0_printf("<time> test... ");
time_t seconds = time(NULL);
neorv32_uart0_printf("Seconds since January 1, 1970 (32-bit!) = %u\n", (uint32_t)seconds);
neorv32_uart0_printf("%i, %i, %i, %i\n", rand() % 100, rand() % 100, rand() % 100, rand() % 100);


// malloc test
neorv32_uart0_printf("<malloc> test...\n");
char_buffer = (char *) malloc(4 * sizeof(char)); // 4 bytes
char *char_buffer = (char *) malloc(4 * sizeof(char)); // 4 bytes

if (char_buffer == NULL) {
neorv32_uart0_printf("malloc FAILED!\n");
return -1;
}


// STDx tests using read and write
// do not test read & write in simulation as there would be no UART RX input
if (neorv32_cpu_csr_read(CSR_MXISA) & (1 << CSR_MXISA_IS_SIM)) {
neorv32_uart0_printf("Skipping <read> & <write> tests as this seems to be a simulation.\n");
Expand All @@ -118,13 +139,15 @@ int main() {
free(char_buffer);


// exit test
// NOTE: exit is highly over-sized as it also includes clean-up functions (destructors), which
// are not required for bare-metal or RTOS applications... better use the simple 'return' or even better
// make sure main never returns. Anyway, let's check if 'exit' works.
int exit_code = 7;
neorv32_uart0_printf("<exit> terminating by exit(%i)...\n", exit_code);
exit(exit_code);


// should never be reached
neorv32_uart0_printf("exit failed!\n");
return 0;
Expand Down
2 changes: 1 addition & 1 deletion sw/example/demo_newlib/makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Configure max HEAP size
override USER_FLAGS += "-Wl,--defsym,__neorv32_heap_size=1024"
override USER_FLAGS += "-Wl,--defsym,__neorv32_heap_size=3072"

NEORV32_HOME ?= ../../..
include $(NEORV32_HOME)/sw/common/common.mk
Loading