Skip to content

Commit e226d8e

Browse files
kyle-githubCopilot
andcommitted
Add volatile to stack buffer to make sure that stores are not eliminated by the compiler.
Co-authored-by: Copilot <copilot@github.com>
1 parent 2816657 commit e226d8e

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

tests/test_yafl_watermark.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,31 @@
1313

1414
#include "../include/yafl.h"
1515

16+
#define TEST_STACK_TOUCH_COMPLETE 0x34
17+
#define TEST_STACK_TOUCH_SUSPEND_BEFORE 0xC9
18+
#define TEST_STACK_TOUCH_SUSPEND_AFTER 0x9C
19+
1620
static const char *alloc_name(yafl_stack_flags_t alloc_flag) { return alloc_flag == YAFL_STACK_FLAGS_VMEM ? "mmap" : "malloc"; }
1721

22+
/* Force observable stack writes so YAFL's own watermark logic has real usage to measure. */
23+
static void touch_volatile_stack_buffer(volatile unsigned char *buffer, size_t size, unsigned char value) {
24+
for(size_t index = 0; index < size; index++) { buffer[index] = value; }
25+
}
26+
1827
static void *complete_after_stack_use(void *arg) {
1928
volatile unsigned char stack_buffer[1536];
2029

21-
memset((void *)stack_buffer, (int)(uintptr_t)arg, sizeof(stack_buffer));
30+
touch_volatile_stack_buffer(stack_buffer, sizeof(stack_buffer), TEST_STACK_TOUCH_COMPLETE);
2231
return arg;
2332
}
2433

2534
static void *suspend_after_stack_use(void *arg) {
2635
volatile unsigned char stack_buffer[768];
2736

28-
memset((void *)stack_buffer, 0x5A, sizeof(stack_buffer));
37+
touch_volatile_stack_buffer(stack_buffer, sizeof(stack_buffer), TEST_STACK_TOUCH_SUSPEND_BEFORE);
2938
arg = yafl_fiber_suspend(arg);
3039

31-
memset((void *)stack_buffer, 0xA5, sizeof(stack_buffer));
40+
touch_volatile_stack_buffer(stack_buffer, sizeof(stack_buffer), TEST_STACK_TOUCH_SUSPEND_AFTER);
3241
return arg;
3342
}
3443

0 commit comments

Comments
 (0)