simavr icon indicating copy to clipboard operation
simavr copied to clipboard

Fix segfault with ATMega16m1 UART simulation

Open jack-greenberg opened this issue 3 years ago • 3 comments

Previously, when accessing the p->fe.reg in avr_uart.c (in avr_uart_init()) for ATmega16m1, the system segfaulted.

In avr_register_io_read, we see the following lines:

	avr_io_addr_t a = AVR_DATA_TO_IO(addr);
	if (avr->io[a].r.param || avr->io[a].r.c) {

AVR_DATA_TO_IO just subtracts 0x20 from addr. addr is passed in from avr_uart_init as p->fe.reg. But in ATmega16m1, p->fe is not initialized, so it is set to zero. When we subtract 0x20 from 0, and then do avr->io[a], we have an array-out-of-bounds error.

The following previously segfaulted, but no longer does:

./simavr/run_avr -m atmega16m1 -f 4000000 ~/path/to/binary.elf

jack-greenberg avatar Aug 21 '22 00:08 jack-greenberg

Possibly addresses #335?

jack-greenberg avatar Aug 21 '22 00:08 jack-greenberg

Ah whoops, I mis-wrote this (I actually found this a few months ago but forgot to submit it).

In avr_register_io_read, we see the following lines:

	avr_io_addr_t a = AVR_DATA_TO_IO(addr);
	if (avr->io[a].r.param || avr->io[a].r.c) {

AVR_DATA_TO_IO just subtracts 0x20 from addr. addr is passed in from avr_uart_init as p->fe.reg. But in ATmega16m1, p->fe is not initialized, so it is set to zero. When we subtract 0x20 from 0, and then do avr->io[a], we have an array-out-of-bounds error.

Does that make sense? I will update the PR body.

jack-greenberg avatar Aug 21 '22 20:08 jack-greenberg

Oh, I see. Now it makes sense to me.

edgar-bonet avatar Aug 21 '22 20:08 edgar-bonet