Skip to content

Commit 4d6c2c6

Browse files
authored
Merge pull request #320 from delta-G/irqBoundsCheck
bounds check for last_interrupt_index
2 parents 027656d + 1d0792d commit 4d6c2c6

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

Diff for: cores/arduino/IRQManager.cpp

+78-24
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ bool IRQManager::addADCScanEnd(ADC_Container *adc, Irq_f fnc /*= nullptr*/) {
6767
/* set the displacement to the "programmable" part of the table */
6868
irq_ptr += FIXED_IRQ_NUM;
6969
bool rv = true;
70-
71-
if (adc->cfg.scan_end_irq == FSP_INVALID_VECTOR) {
70+
if (last_interrupt_index >= PROG_IRQ_NUM){
71+
rv = false;
72+
}
73+
else if (adc->cfg.scan_end_irq == FSP_INVALID_VECTOR) {
7274
if(set_adc_end_link_event(last_interrupt_index, adc->cfg.unit)) {
7375
adc->cfg.scan_end_ipl = TIMER_PRIORITY;
7476
adc->cfg.scan_end_irq = (IRQn_Type)last_interrupt_index;
@@ -101,8 +103,10 @@ bool IRQManager::addADCScanEndB(ADC_Container *adc, Irq_f fnc /*= nullptr*/) {
101103
/* set the displacement to the "programmable" part of the table */
102104
irq_ptr += FIXED_IRQ_NUM;
103105
bool rv = true;
104-
105-
if (adc->cfg.scan_end_b_irq == FSP_INVALID_VECTOR) {
106+
if (last_interrupt_index >= PROG_IRQ_NUM){
107+
rv = false;
108+
}
109+
else if (adc->cfg.scan_end_b_irq == FSP_INVALID_VECTOR) {
106110
if(set_adc_end_b_link_event(last_interrupt_index, adc->cfg.unit)) {
107111
adc->cfg.scan_end_b_ipl = TIMER_PRIORITY;
108112
adc->cfg.scan_end_b_irq = (IRQn_Type)last_interrupt_index;
@@ -125,8 +129,10 @@ bool IRQManager::addADCWinCmpA(ADC_Container *adc, Irq_f fnc /*= nullptr*/) {
125129
/* set the displacement to the "programmable" part of the table */
126130
irq_ptr += FIXED_IRQ_NUM;
127131
bool rv = true;
128-
129-
if( ((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_a_irq == FSP_INVALID_VECTOR) {
132+
if (last_interrupt_index >= PROG_IRQ_NUM){
133+
rv = false;
134+
}
135+
else if( ((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_a_irq == FSP_INVALID_VECTOR) {
130136
if(set_adc_win_a_link_event(last_interrupt_index, adc->cfg.unit)) {
131137
((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_a_ipl = TIMER_PRIORITY;
132138
((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_a_irq = (IRQn_Type)last_interrupt_index;
@@ -150,8 +156,10 @@ bool IRQManager::addADCWinCmpB(ADC_Container *adc, Irq_f fnc /*= nullptr*/) {
150156
/* set the displacement to the "programmable" part of the table */
151157
irq_ptr += FIXED_IRQ_NUM;
152158
bool rv = true;
153-
154-
if (((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_b_irq == FSP_INVALID_VECTOR) {
159+
if (last_interrupt_index >= PROG_IRQ_NUM){
160+
rv = false;
161+
}
162+
else if (((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_b_irq == FSP_INVALID_VECTOR) {
155163
if(set_adc_win_b_link_event(last_interrupt_index, adc->cfg.unit)) {
156164
((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_b_ipl = TIMER_PRIORITY;
157165
((adc_extended_cfg_t *)(adc->cfg.p_extend))->window_b_irq = (IRQn_Type)last_interrupt_index;
@@ -177,8 +185,10 @@ bool IRQManager::addTimerOverflow(TimerIrqCfg_t &cfg, Irq_f fnc /* = nullptr */)
177185
/* set the displacement to the "programmable" part of the table */
178186
irq_ptr += FIXED_IRQ_NUM;
179187
bool rv = true;
180-
181-
if (cfg.base_cfg->cycle_end_irq == FSP_INVALID_VECTOR) {
188+
if (last_interrupt_index >= PROG_IRQ_NUM){
189+
rv = false;
190+
}
191+
else if (cfg.base_cfg->cycle_end_irq == FSP_INVALID_VECTOR) {
182192
if(cfg.gpt_ext_cfg != nullptr) {
183193
if(set_gpt_over_link_event(last_interrupt_index, cfg.base_cfg->channel)) {
184194
cfg.base_cfg->cycle_end_ipl = TIMER_PRIORITY;
@@ -224,7 +234,7 @@ bool IRQManager::addTimerUnderflow(TimerIrqCfg_t &cfg, Irq_f fnc /*= nullptr*/)
224234
irq_ptr += FIXED_IRQ_NUM;
225235
bool rv = true;
226236

227-
if(cfg.agt_ext_cfg != nullptr) {
237+
if((cfg.agt_ext_cfg != nullptr) || (last_interrupt_index >= PROG_IRQ_NUM)) {
228238
/* not supported for AGT */
229239
rv = false;
230240
}
@@ -258,7 +268,7 @@ bool IRQManager::addTimerCompareCaptureA(TimerIrqCfg_t &cfg, Irq_f fnc /*= nullp
258268
irq_ptr += FIXED_IRQ_NUM;
259269
bool rv = true;
260270

261-
if(cfg.agt_ext_cfg != nullptr) {
271+
if((cfg.agt_ext_cfg != nullptr) || (last_interrupt_index >= PROG_IRQ_NUM)) {
262272
/* not supported for AGT */
263273
rv = false;
264274
}
@@ -292,7 +302,7 @@ bool IRQManager::addTimerCompareCaptureB(TimerIrqCfg_t &cfg, Irq_f fnc /*= nullp
292302
irq_ptr += FIXED_IRQ_NUM;
293303
bool rv = true;
294304

295-
if(cfg.agt_ext_cfg != nullptr) {
305+
if((cfg.agt_ext_cfg != nullptr) || (last_interrupt_index >= PROG_IRQ_NUM)) {
296306
/* not supported for AGT */
297307
rv = false;
298308
}
@@ -326,8 +336,10 @@ bool IRQManager::addDMA(dmac_extended_cfg_t &cfg, Irq_f fnc /* = nullptr */) {
326336
/* set the displacement to the "programmable" part of the table */
327337
irq_ptr += FIXED_IRQ_NUM;
328338
bool rv = true;
329-
330-
if (cfg.irq == FSP_INVALID_VECTOR) {
339+
if (last_interrupt_index >= PROG_IRQ_NUM){
340+
rv = false;
341+
}
342+
else if (cfg.irq == FSP_INVALID_VECTOR) {
331343
/* to check correctness of the channel */
332344
if(set_dma_link_event(last_interrupt_index, cfg.channel)) {
333345
cfg.ipl = DMA_PRIORITY;
@@ -357,6 +369,11 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
357369
irq_ptr += FIXED_IRQ_NUM;
358370
bool rv = true;
359371

372+
if(last_interrupt_index >= PROG_IRQ_NUM){
373+
rv = false;
374+
goto end_config;
375+
}
376+
360377
__disable_irq();
361378
/* **********************************************************************
362379
USB
@@ -493,7 +510,12 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
493510
********************************************************************** */
494511
else if(p == IRQ_SCI_UART && cfg != NULL) {
495512
uart_cfg_t *p_cfg = (uart_cfg_t *)cfg;
513+
496514
if (p_cfg->txi_irq == FSP_INVALID_VECTOR) {
515+
if (last_interrupt_index + UART_INTERRUPT_COUNT > PROG_IRQ_NUM){
516+
rv = false;
517+
goto end_config;
518+
}
497519
/* TX interrupt */
498520
p_cfg->txi_ipl = UART_SCI_PRIORITY;
499521
p_cfg->txi_irq = (IRQn_Type)last_interrupt_index;
@@ -579,13 +601,18 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
579601

580602
#if WIRE_HOWMANY > 0
581603
/* I2C true NOT SCI */
582-
else if(p == IRQ_I2C_MASTER && cfg != NULL) {
604+
else if(p == IRQ_I2C_MASTER && cfg != NULL) {
605+
583606
I2CIrqReq_t *p_cfg = (I2CIrqReq_t *)cfg;
584607
i2c_master_cfg_t *mcfg = (i2c_master_cfg_t *)p_cfg->mcfg;
585608
i2c_slave_cfg_t *scfg = (i2c_slave_cfg_t *)p_cfg->scfg;
586609
mcfg->ipl = I2C_MASTER_PRIORITY;
587610

588611
if (mcfg->txi_irq == FSP_INVALID_VECTOR) {
612+
if (last_interrupt_index + WIRE_MASTER_INTERRUPT_COUNT > PROG_IRQ_NUM){
613+
rv = false;
614+
goto end_config;
615+
}
589616
/* TX interrupt */
590617
mcfg->txi_irq = (IRQn_Type)last_interrupt_index;
591618
scfg->txi_irq = (IRQn_Type)last_interrupt_index;
@@ -626,11 +653,15 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
626653
R_BSP_IrqEnable (mcfg->eri_irq);
627654
}
628655
/* I2C SCI MASTER (only) */
629-
else if(p == IRQ_SCI_I2C_MASTER && cfg != NULL) {
656+
else if(p == IRQ_SCI_I2C_MASTER && cfg != NULL) {
630657
I2CIrqReq_t *p_cfg = (I2CIrqReq_t *)cfg;
631658
i2c_master_cfg_t *mcfg = (i2c_master_cfg_t *)p_cfg->mcfg;
632659
mcfg->ipl = I2C_MASTER_PRIORITY;
633-
if (mcfg->txi_irq == FSP_INVALID_VECTOR) {
660+
if (mcfg->txi_irq == FSP_INVALID_VECTOR) {
661+
if (last_interrupt_index + WIRE_SCI_MASTER_INTERRUPT_COUNT > PROG_IRQ_NUM) {
662+
rv = false;
663+
goto end_config;
664+
}
634665
/* TX interrupt */
635666
mcfg->txi_irq = (IRQn_Type)last_interrupt_index;
636667
*(irq_ptr + last_interrupt_index) = (uint32_t)sci_i2c_txi_isr;
@@ -674,8 +705,12 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
674705
i2c_slave_cfg_t *scfg = (i2c_slave_cfg_t *)p_cfg->scfg;
675706
scfg->ipl = I2C_SLAVE_PRIORITY;
676707
scfg->eri_ipl = I2C_SLAVE_PRIORITY;
677-
678-
if (scfg->txi_irq == FSP_INVALID_VECTOR) {
708+
709+
if (scfg->txi_irq == FSP_INVALID_VECTOR) {
710+
if (last_interrupt_index + WIRE_SLAVE_INTERRUPT_COUNT > PROG_IRQ_NUM) {
711+
rv = false;
712+
goto end_config;
713+
}
679714
/* TX interrupt */
680715
mcfg->txi_irq = (IRQn_Type)last_interrupt_index;
681716
scfg->txi_irq = (IRQn_Type)last_interrupt_index;
@@ -732,12 +767,16 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
732767
/* **********************************************************************
733768
SPI MASTER
734769
********************************************************************** */
735-
else if(p == IRQ_SPI_MASTER && cfg != NULL) {
770+
else if(p == IRQ_SPI_MASTER && cfg != NULL) {
736771
spi_instance_ctrl_t * p_ctrl = reinterpret_cast<SpiMasterIrqReq_t *>(cfg)->ctrl;
737772
spi_cfg_t * p_cfg = reinterpret_cast<SpiMasterIrqReq_t *>(cfg)->cfg;
738773
uint8_t const hw_channel = reinterpret_cast<SpiMasterIrqReq_t *>(cfg)->hw_channel;
739774

740775
if (p_cfg->txi_irq == FSP_INVALID_VECTOR) {
776+
if (last_interrupt_index + SPI_INTERRUPT_COUNT > PROG_IRQ_NUM) {
777+
rv = false;
778+
goto end_config;
779+
}
741780
/* TX interrupt */
742781
p_cfg->txi_irq = (IRQn_Type)last_interrupt_index;
743782
p_cfg->txi_ipl = SPI_MASTER_PRIORITY;
@@ -780,12 +819,16 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
780819
/* **********************************************************************
781820
SCI SPI MASTER
782821
********************************************************************** */
783-
else if(p == IRQ_SCI_SPI_MASTER && cfg != NULL) {
822+
else if(p == IRQ_SCI_SPI_MASTER && cfg != NULL) {
784823
sci_spi_instance_ctrl_t * p_ctrl = reinterpret_cast<SciSpiMasterIrqReq_t *>(cfg)->ctrl;
785824
spi_cfg_t * p_cfg = reinterpret_cast<SciSpiMasterIrqReq_t *>(cfg)->cfg;
786825
uint8_t const hw_channel = reinterpret_cast<SciSpiMasterIrqReq_t *>(cfg)->hw_channel;
787826

788827
if (p_cfg->txi_irq == FSP_INVALID_VECTOR) {
828+
if (last_interrupt_index + SPI_INTERRUPT_COUNT > PROG_IRQ_NUM) {
829+
rv = false;
830+
goto end_config;
831+
}
789832
/* TX interrupt */
790833
p_cfg->txi_irq = (IRQn_Type)last_interrupt_index;
791834
p_cfg->txi_ipl = SPI_MASTER_PRIORITY;
@@ -829,12 +872,16 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
829872
/* **********************************************************************
830873
CAN
831874
********************************************************************** */
832-
else if(p == IRQ_CAN && cfg != NULL) {
875+
else if(p == IRQ_CAN && cfg != NULL) {
833876
can_instance_ctrl_t * p_ctrl = reinterpret_cast<CanIrqReq_t *>(cfg)->ctrl;
834877
can_cfg_t * p_cfg = reinterpret_cast<CanIrqReq_t *>(cfg)->cfg;
835878
p_cfg->ipl = CAN_PRIORITY; /* All interrupts share the same priority. */
836879

837880
if (p_cfg->error_irq == FSP_INVALID_VECTOR) {
881+
if (last_interrupt_index + CAN_INTERRUPT_COUNT > PROG_IRQ_NUM) {
882+
rv = false;
883+
goto end_config;
884+
}
838885
/* Error interrupt */
839886
p_cfg->error_irq = (IRQn_Type)last_interrupt_index;
840887
*(irq_ptr + last_interrupt_index) = (uint32_t)can_error_isr;
@@ -889,6 +936,10 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
889936
p_cfg->ipl = CAN_PRIORITY; /* All interrupts share the same priority. */
890937

891938
if (p_cfg->error_irq == FSP_INVALID_VECTOR) {
939+
if (last_interrupt_index + CANFD_INTERRUPT_COUNT > PROG_IRQ_NUM) {
940+
rv = false;
941+
goto end_config;
942+
}
892943
/* Error interrupt */
893944
p_cfg->error_irq = (IRQn_Type)last_interrupt_index;
894945
*(irq_ptr + last_interrupt_index) = (uint32_t)canfd_error_isr;
@@ -924,7 +975,10 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) {
924975
sdmmc_cfg_t *sd_cfg = (sdmmc_cfg_t *)cfg;
925976
/* SDCARD_ACCESS */
926977
if(sd_cfg->access_irq == FSP_INVALID_VECTOR) {
927-
978+
if (last_interrupt_index + SD_INTERRUPT_COUNT > PROG_IRQ_NUM){
979+
rv = false;
980+
goto end_config;
981+
}
928982
sd_cfg->access_irq = (IRQn_Type)last_interrupt_index;
929983
sd_cfg->access_ipl = SDCARD_ACCESS_PRIORITY;
930984
*(irq_ptr + last_interrupt_index) = (uint32_t)sdhimmc_accs_isr;

Diff for: cores/arduino/IRQManager.h

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#if SERIAL_HOWMANY > 0
1414
#include "r_uart_api.h"
15+
#define UART_INTERRUPT_COUNT 4
1516
#endif
1617

1718
#if EXT_INTERRUPTS_HOWMANY > 0
@@ -78,6 +79,9 @@ typedef struct i2c_irq_req {
7879
i2c_master_cfg_t *mcfg;
7980
i2c_slave_cfg_t *scfg;
8081
} I2CIrqReq_t;
82+
#define WIRE_MASTER_INTERRUPT_COUNT 4
83+
#define WIRE_SLAVE_INTERRUPT_COUNT 4
84+
#define WIRE_SCI_MASTER_INTERRUPT_COUNT 3
8185
#endif
8286

8387
#if SPI_HOWMANY > 0
@@ -95,6 +99,7 @@ typedef struct sci_spi_master_irq {
9599
spi_cfg_t * cfg;
96100
uint8_t hw_channel;
97101
} SciSpiMasterIrqReq_t;
102+
#define SPI_INTERRUPT_COUNT 4
98103
#endif
99104

100105
#if CAN_HOWMANY > 0
@@ -103,6 +108,7 @@ typedef struct can_irq {
103108
can_instance_ctrl_t * ctrl;
104109
can_cfg_t * cfg;
105110
} CanIrqReq_t;
111+
#define CAN_INTERRUPT_COUNT 3
106112
#endif /* CAN_HOWMANY > 0 */
107113

108114
#if CANFD_HOWMANY > 0
@@ -111,8 +117,11 @@ typedef struct canfd_irq {
111117
canfd_instance_ctrl_t * ctrl;
112118
can_cfg_t * cfg;
113119
} CanFdIrqReq_t;
120+
#define CANFD_INTERRUPT_COUNT 3
114121
#endif /* CANFD_HOWMANY > 0 */
115122

123+
#define SD_INTERRUPT_COUNT 2
124+
116125
typedef struct usb {
117126
uint32_t num_of_irqs_required;
118127
uint32_t address_of_handler;

0 commit comments

Comments
 (0)