V4.9.3 The Stack That Ate the Variables

HardDebug20 minap_soc v1.5 · recorded bootReview the topic

A test of nested calls runs for seventy thousand cycles and dies in a trap, with no line printed. Somewhere the stack and the program's variables met. Find the first transfer that went where it should not.

TB_ARG was 3000 for this run. The retire trace has one trap: at pc 800002d4, mcause 2. The stack starts at 0x8001_0000 and grows down. This image's variables run from 0x80000398 (limit) to the end of .bss. external is the kit's table of 17 interrupt handlers; deepest is the last variable.

The firmware of the recorded run (main.c).
#include "tb.h"

volatile unsigned limit = 8;                       /* the depth the test means to reach */
volatile unsigned deepest;

/* Count down through nested calls, keeping a little state in each frame. */
static unsigned nest(unsigned n)
{
    volatile unsigned frame[4];
    frame[0] = n; frame[1] = n + 1; frame[2] = n + 2; frame[3] = n + 3;
    if (n == 0) return frame[0];
    if (n > deepest) deepest = n;
    return nest(n - 1) + frame[1] - frame[0] - 1;
}

int main(void)
{
    unsigned depth = tb_arg(0);                    /* the bench asks for a depth */
    if (nest(depth) != 0) return 1;
    tb_puts("deepest ");
    tb_put_hex(deepest);
    tb_putc('\n');
    return deepest == depth && depth <= limit ? 0 : 2;
}

nest starts with addi sp, sp, -32 and sw a0, 0(sp). Its frame is 32 bytes; its first store is frame[0] at the new sp. It saves ra at 28(sp) and writes deepest with sw a0, -1972(gp). The full specification holds the listing of nest.

The trace below shows 51 of 12194 retired loads and stores, cycles 190 to 72945. Name the first transfer that went wrong: its cycle, the wrong part, its address and its register or variable. Then choose the cause.

Full specification
nest in the listing of this image (riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0): address and instruction.
8000029c <nest>:
8000029c:  addi  sp,sp,-32
800002a0:  sw  a0,0(sp)
800002a4:  addi  a5,a0,1
800002a8:  sw  a5,4(sp)
800002ac:  addi  a5,a0,2
800002b0:  sw  a5,8(sp)
800002b4:  addi  a5,a0,3
800002b8:  sw  ra,28(sp)
800002bc:  sw  a5,12(sp)
800002c0:  bnez  a0,800002d4 <nest+0x38>
800002c4:  lw  a0,0(sp)
800002c8:  lw  ra,28(sp)
800002cc:  addi  sp,sp,32
800002d0:  ret
800002d4:  lw  a4,-1972(gp)
800002d8:  bgeu  a4,a0,800002e0 <nest+0x44>
800002dc:  sw  a0,-1972(gp)
800002e0:  addi  a0,a0,-1
800002e4:  jal  8000029c <nest>
800002e8:  lw  a5,4(sp)
800002ec:  lw  a4,0(sp)
800002f0:  addi  a5,a5,-1
800002f4:  sub  a5,a5,a4
800002f8:  add  a0,a5,a0
800002fc:  j  800002c8 <nest+0x2c>

The register list names every address the answer may use. Addresses are in hex without 0x in the trace; write them with or without 0x in the form.

Data in the trace is the word as the bus carried it. A load's data is what came back; a store's is what was written.

Diagnose the Failing Run

Recorded Console and Result

ARG 1: 00000bb8
verdict=fail
reason=status_fail
status=bad0ffff
cycles=73232
traps=1
interrupts=0
mismatches=0

Retired Memory Transfers

CyclePC (hex)OperationAddress (hex)Data (hex)
19080000078store1000402000000002
19680000308store8000fffc80000080
1998000030cstore8000fff800000000
20280000310store8000fff400000000
21580000294load1000402c00000bb8
224800002a0store8000ffd000000bb8
227800002a8store8000ffd400000bb9
234800002b0store8000ffd800000bba
237800002b8store8000ffec80000320
243800002bcstore8000ffdc00000bbb
72703800002b0store80000438000003dd
72706800002b8store8000044c800002e8
72712800002bcstore8000043c000003de
72718800002d4load800003e400000bb8
72729800002a0store80000410000003da
72732800002a8store80000414000003db
72739800002b0store80000418000003dc
72742800002b8store8000042c800002e8
72748800002bcstore8000041c000003dd
72754800002d4load800003e400000bb8
72765800002a0store800003f0000003d9
72768800002a8store800003f4000003da
72775800002b0store800003f8000003db
72778800002b8store8000040c800002e8
72784800002bcstore800003fc000003dc
72790800002d4load800003e400000bb8
72801800002a0store800003d0000003d8
72804800002a8store800003d4000003d9
72811800002b0store800003d8000003da
72814800002b8store800003ec800002e8
72820800002bcstore800003dc000003db
72826800002d4load800003e400000bb8
72837800002a0store800003b0000003d7
72840800002a8store800003b4000003d8
72847800002b0store800003b8000003d9
72850800002b8store800003cc800002e8
72856800002bcstore800003bc000003da
72862800002d4load800003e400000bb8
72873800002a0store80000390000003d6
72876800002a8store80000394000003d7
72883800002b0store80000398000003d8
72886800002b8store800003ac800002e8
72892800002bcstore8000039c000003d9
72898800002d4load800003e400000bb8
72909800002a0store80000370000003d5
72912800002a8store80000374000003d6
72919800002b0store80000378000003d7
72922800002b8store8000038c800002e8
72928800002bcstore8000037c000003d8
72934800002d4load800003e400000bb8
72945800002a0store80000350000003d4
Step 1 · The first bad cycle and signal
Step 2 · The cause

Given Files

Use the register map to identify the transfer in the recording.

tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif

hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"

/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
    return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
    *(volatile unsigned *)address = value;
}
#endif

#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif