65816-llvm-mos/demos/layer2Stress.c
2026-05-25 21:00:32 -05:00

48 lines
1.2 KiB
C

// layer2Stress.c - Layer 2 ptr32 deref miscompile reproducer.
//
// Verifies that *_StackRelIndY uses (the Layer 2 deref pseudo) survive
// W65816StackRelToImg's hot-slot promotion. Each helper writes its
// result to a known address; runInMame.sh --check verifies all four.
#include <stdint.h>
__attribute__((noinline)) uint16_t indexedRead(const uint16_t *arr, uint16_t i) {
return arr[i];
}
__attribute__((noinline)) uint16_t strLen(const char *p) {
uint16_t n = 0;
while (*p) {
p++;
n++;
}
return n;
}
__attribute__((noinline)) uint16_t sumByteToZero(const uint8_t *p) {
uint16_t s = 0;
while (*p) {
s += *p;
p++;
}
return s;
}
static const uint16_t gArr[] = { 100, 200, 300, 400, 500 };
static const uint8_t gBytes[] = { 10, 20, 30, 40, 50, 0 };
static const char gString[] = "Hello, world!";
int main(void) {
*(volatile uint16_t *)0x70 = strLen(gString);
*(volatile uint16_t *)0x72 = sumByteToZero(gBytes);
*(volatile uint16_t *)0x74 = indexedRead(gArr, 3);
*(volatile uint16_t *)0x76 = 0xBEEF;
for (volatile uint32_t s = 0; s < 200000UL; s++) {
}
return 0;
}