STM32 gotchas
111. Some interrupts invoke incorrect ISR (if VTOR is not properly aligned)

This issue has been discussed here for 'H7 and here for 'F4.

The interrupt vector table can be relocated in Cortex-Mx (except Cortex-M0) using the processor-core register SCB->VTOR (Vector Table Offset Register). This is used e.g. when interrupts have to be moved to RAM (for higher execution speed, or because of FLASH unavailability e.g. during FLASH programming), or to a different than default area of FLASH, often when custom bootloaders are used.

VTOR has 128-byte granularity and its lowermost 7 bits are fixed at zero. According to ARM's materials, when interrupt occurs, the appropriate vector is picked by adding the interrupt's number multiplied by four, to VTOR, then the interrupt vector is picked from the resulting address. ARMŽ v7-M Architecture Reference Manual ARM DDI 0403E.b (ID120114) gives the following pseudocode for this process:

ExceptionTaken(integer ExceptionNumber)
[...]
bits(32) VectorTable = VTOR<31:7>:'0000000';
tmp = MemA[VectorTable+4*ExceptionNumber,4];
BranchTo(tmp AND 0xFFFFFFFE<31:0>);

where MemA[] roughly means "check address for alignment and priviledges, and then return the word from that address".

This process implies, that the vector table might start at any address of 0xXXXX'XX00 or 0xXXXX'XX80.

However, from the reported behaviour it is obvious, that the VTOR value is not added to ExceptionNumber*4, rather, it is bitwise-OR-ed to it. From hardware point of view this makes sense - bitwise OR is faster than addition, and also implements in a significantly lower gate count; while the result is the same, as long as the vector table fits into the "granularity" of its location.

Unfortunately, practical vector tables in any Cortex-M3/M4/M7 implementation never fits into 128 bytes. Consequence of using VTOR value not having enough zeros at its lowermost bits to fit the offset of all vectors is, that for interrupts with higher numbers, incorrect ISR will be invoked.

This fact - without explanation - is documented in ST's Programming Manuals to individual Cortex-Mx cores in the following sentence below VTOR description:

When setting TBLOFF, the user must align the offset to the number of exception entries in the vector table.

where TBLOFF is the only bitfield in VTOR.

Confusingly, the rest of VTOR description (incorrectly) states that the lowermost 9 bits are always zero, implying 128-word (512-bit) alignment requirement, which in some implementations with larger vector table is insufficient.