Skip to content

Commit 1a2b6f2

Browse files
committed
heartbleed: use 64-bit mcycle read to avoid overflow in legacy
The legacy demo unintentionally aliased a 32-bit `rdcycle` function to `rdcycle64`, and used its 32-bit output as a 64-bit value. This caused an overflow in `mcycle` after a few minutes of running, which sometimes left the demo stuck and unable to progress in its legacy component. Replace this function with an actual 64-bit mcycle read using the low/high registers appropriately. Signed-off-by: Alex Jones <[email protected]>
1 parent 9f58e47 commit 1a2b6f2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

examples/heartbleed/legacy/heartbleed.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,19 @@ void write_to_uart(const char *format, ...)
5252
write_to_uart(__VA_ARGS__); \
5353
}
5454

55-
#define rdcycle64 get_mcycle
55+
uint64_t rdcycle64()
56+
{
57+
uint32_t mcycle_high, mcycle_low;
58+
__asm__ volatile("1: "
59+
"csrr t0, mcycleh\n"
60+
"csrr %0, mcycle\n"
61+
"csrr %1, mcycleh\n"
62+
"bne t0, %1, 1b"
63+
: "=r"(mcycle_low), "=r"(mcycle_high)
64+
:
65+
: "t0");
66+
return ((uint64_t)mcycle_high << 32) | mcycle_low;
67+
}
5668

5769
/**
5870
* @brief Read the current GPIO joystick state.

0 commit comments

Comments
 (0)