scx_lavd 1.1.0

A Latency-criticality Aware Virtual Deadline (LAVD) scheduler based on sched_ext, which is a Linux kernel feature which enables implementing kernel thread schedulers in BPF and dynamically loading them. https://github.com/sched-ext/scx/tree/main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
 * SPDX-License-Identifier: GPL-2.0
 * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
 * Copyright (c) 2025 Emil Tsalapatis <etsal@meta.com>
 */

#include <alloc/common.h>

#include <alloc/asan.h>
#include <alloc/buddy.h>
#include <alloc/bump.h>
#include <alloc/stack.h>

#include "selftest.h"

#ifdef BPF_ARENA_ASAN

#define ST_PAGES 64

#define ASAN_MAP_STATE(addr)                                                 \
	do {                                                                 \
		bpf_printk("%s:%d ASAN %lx -> (val: %x gran: %x set: [%s])", \
			   __func__, __LINE__, addr,                         \
			   asan_shadow_value((addr)), ASAN_GRANULE(addr),    \
			   asan_shadow_set((addr)) ? "yes" : "no");          \
	} while (0)

/*
 * Emit an error and force the current function to exit if the ASAN
 * violation state is unexpected. Reset the violation state after.
 */
#define ASAN_VALIDATE_ADDR(cond, addr)                                       \
	do {                                                                 \
		asm volatile("" ::: "memory");                               \
		if ((asan_violated != 0) != (cond)) {                        \
			bpf_printk("%s:%d ASAN asan_violated %lx", __func__, \
				   __LINE__, (u64)asan_violated);            \
			ASAN_MAP_STATE((addr));                              \
			return -EINVAL;                                      \
		}                                                            \
		asan_violated = 0;                                           \
	} while (0)

#define ASAN_VALIDATE()                                                 \
	do {                                                            \
		if ((asan_violated)) {                                  \
			bpf_printk("%s:%d Found ASAN violation at %lx", \
				   __func__, __LINE__, asan_violated);  \
			return -EINVAL;                                 \
		}                                                       \
	} while (0)

struct blob {
	volatile u8 mem[59];
	u8 oob;
};

int asan_test_static_blob_one(void)
{
	volatile struct blob __arena *blob;
	const size_t alignment = 1;

	blob = bump_alloc(sizeof(blob) - 1, alignment);
	if (!blob)
		return -ENOMEM;

	blob->mem[0] = 0xba;
	ASAN_VALIDATE_ADDR(false, &blob->mem[0]);

	blob->oob = 0;
	ASAN_VALIDATE_ADDR(true, &blob->oob);

	blob = (volatile struct blob __arena *)&blob->oob;
	blob->mem[0] = 0xba;
	ASAN_VALIDATE_ADDR(true, &blob->mem[0]);

	blob->oob = 4;
	ASAN_VALIDATE_ADDR(true, &blob->oob);

	/*
	 * Go even further, cast the OOB variable into
	 * another struct blob and access its own oob.
	 */
	blob = (volatile struct blob __arena *)&blob->oob;
	blob->oob = 5;
	ASAN_VALIDATE_ADDR(true, &blob->oob);

	return 0;
}

int asan_test_static_blob(void)
{
	const int iters = 20;
	int ret, i;

	ret = bump_init(ST_PAGES);
	if (ret) {
		bpf_printk("bump_init failed with %d", ret);
		return ret;
	}

	for (i = 0; i < iters && can_loop; i++) {
		ret = asan_test_static_blob_one();
		if (ret) {
			bpf_printk("%s:%d Failed on iteration %d", __func__,
				   __LINE__, i);
			return ret;
		}
	}

	bump_destroy();

	ASAN_VALIDATE();

	return 0;
}

int asan_test_static_array_one(void)
{
	size_t bytes = 37;
	size_t overrun = 13;
	size_t alignment = 1;
	char __arena *mem;
	int i;

	mem = bump_alloc(sizeof(*mem) * bytes, alignment);
	if (!mem)
		return -ENOMEM;

	for (i = 0; i < bytes + overrun && can_loop; i++) {
		mem[i] = 0xba;
		ASAN_VALIDATE_ADDR(i >= bytes, &mem[i]);
	}

	ASAN_VALIDATE();

	return 0;
}

int asan_test_static_array(void)
{
	const size_t iters = 20;
	int ret, i;

	ret = bump_init(ST_PAGES);
	if (ret) {
		bpf_printk("bump_init failed with %d", ret);
		return ret;
	}

	for (i = 0; i < iters && can_loop; i++) {
		ret = asan_test_static_array_one();
		if (ret) {
			bpf_printk("%s:%d Failed on iteration %d", __func__,
				   __LINE__, i);
			return ret;
		}
	}

	bump_destroy();

	return 0;
}

int asan_test_static_all(void)
{
	const int iters = 50;
	int ret, i;

	ret = bump_init(ST_PAGES);
	if (ret) {
		bpf_printk("bump_init failed with %d", ret);
		return ret;
	}

	for (i = 0; i < iters && can_loop; i++) {
		ret = asan_test_static_array_one();
		if (ret) {
			bpf_printk("%s:%d Failed on iteration %d", __func__,
				   __LINE__, i);
			return ret;
		}

		ret = asan_test_static_blob_one();
		if (ret) {
			bpf_printk("%s:%d Failed on iteration %d", __func__,
				   __LINE__, i);
			return ret;
		}
	}

	bump_destroy();

	return 0;
}

#define STACK_PAGES_PER_ALLOC (4)
#define STACK_ALLOCS (4)

/* 
 * Keep this test-related array in BSS to avoid
 * overly burdening the function stack.
 */
u64 __arena stk_blks[STACK_ALLOCS];

/*
 * Spinlock used by the stack allocator.
 */
private(ST_STACK) struct stk st_stack;
u64 __arena st_asan_lock;

static __maybe_unused void stk_blks_dump(void)
{
	int i;

	for (i = 0; i < STACK_ALLOCS && can_loop; i++)
		bpf_printk("[%d] 0x%lx", i, stk_blks[i]);
}

struct qsort_limits {
	int lo;
	int hi;
};

__always_inline void swap(unsigned int i, unsigned int j)
{
	u64 tmp;

	tmp = stk_blks[i];
	stk_blks[i] = stk_blks[j];
	stk_blks[j] = tmp;
}

__weak int qsort_partition(unsigned int lo, unsigned hi)
{
	unsigned int i;
	u64 pivotval;
	int pivot;

	if (lo >= STACK_ALLOCS || hi >= STACK_ALLOCS) {
		bpf_printk("%s:%d invalid lo/hi indices %d/%d", __func__,
			   __LINE__, lo, hi);
		return 0;
	}

	pivotval = stk_blks[hi];
	pivot = lo;

	for (i = lo; i < hi && can_loop; i++) {
		if (stk_blks[i] > pivotval)
			continue;

		swap(i, pivot);
		pivot += 1;
	}

	swap(pivot, hi);

	return pivot;
}

__weak int qsort_stack_blocks(void)
{
	struct qsort_limits stack[STACK_ALLOCS];
	struct qsort_limits limits;
	int stackind = 0;
	int pivot;

	limits = (struct qsort_limits){ 0, STACK_ALLOCS - 1 };
	stack[stackind++] = limits;

	while (stackind > 0 && can_loop) {
		if (stackind <= 0 || stackind > STACK_ALLOCS) {
			bpf_printk("%s:%d invalid stack index %d", __func__,
				   __LINE__, stackind);
			return 0;
		}

		limits = stack[--stackind];
		if (limits.lo >= limits.hi)
			continue;

		pivot = qsort_partition(limits.lo, limits.hi);
		stack[stackind++] = (struct qsort_limits){
			.lo = limits.lo,
			.hi = pivot - 1,
		};

		if (stackind <= 0 || stackind >= STACK_ALLOCS) {
			bpf_printk("%s:%d invalid stack index", __func__,
				   __LINE__);
			return 0;
		}

		stack[stackind++] = (struct qsort_limits){
			.lo = pivot + 1,
			.hi = limits.hi,
		};
	}

	return 0;
}

int asan_test_stack_uaf_oob_single(u8 __arena __arg_arena *alloced,
				   u8 __arena __arg_arena *freed)
{
	const size_t overshoot = 5;
	int i;

	/* Use after free check. */
	stk_free(&st_stack, freed);

	bpf_for(i, 0, PAGE_SIZE) {
		freed[i] = 0xba;
		ASAN_VALIDATE_ADDR(true, &freed[i]);
	}

	/* 
	 * Out of bounds check. Assuming the blocks before were
	 * allocated consecutively, past the end of the block
	 * the memory is guaranteed to be freed.
	 */
	bpf_for(i, 0, PAGE_SIZE + overshoot) {
		alloced[i] = 0xba;
		ASAN_VALIDATE_ADDR(i >= PAGE_SIZE, &alloced[i]);
	}

	return 0;
}

static int asan_sort_stack_blocks()
{
	int i;

	qsort_stack_blocks();

	if (!stk_blks[0]) {
		bpf_printk("NULL stack block pointer");
		return -EINVAL;
	}

	for (i = 1; i < STACK_ALLOCS; i++) {
		if (!stk_blks[i]) {
			bpf_printk("missing block");
			return -EINVAL;
		}

		if (stk_blks[i] != stk_blks[i - 1] + PAGE_SIZE) {
			bpf_printk("allocations not consecutive");
			return -EINVAL;
		}
	}

	return 0;
}

__weak int asan_test_stack_uaf_oob(void)
{
	u64 base = (u64)(-1);
	const u64 alloc_size = 4096;
	u64 block;
	int ret, i;

	/* Set the stack to support 4KiB allocations. */
	ret = stk_init(&st_stack, (arena_spinlock_t __arena *)&st_asan_lock,
			   alloc_size, STACK_PAGES_PER_ALLOC);
	if (ret) {
		bpf_printk("stk_init failed with %d", ret);
		return ret;
	}

	bpf_for(i, 0, STACK_ALLOCS) {
		block = (u64)stk_alloc(&st_stack);
		if (!block) {
			bpf_printk("allocation %d failed", i);
			return -ENOMEM;
		}

		stk_blks[i] = block;
		base = block < base ? block : base;
	}

	ret = asan_sort_stack_blocks();
	if (ret)
		return ret;

	for (i = 0; i < STACK_ALLOCS && can_loop; i += 2) {
		if (i + 1 >= STACK_ALLOCS)
			break;

		if (stk_blks[i] + alloc_size != stk_blks[i + 1]) {
			bpf_printk("Stack allocations not consecutive");
			return -EINVAL;
		}

		ret = asan_test_stack_uaf_oob_single(
			(u8 __arena *)stk_blks[i],
			(u8 __arena *)stk_blks[i + 1]);
		if (ret)
			return ret;
	}

	stk_destroy(&st_stack);

	return 0;
}

int asan_test_stack(void)
{
	int ret;

	ret = asan_test_stack_uaf_oob();
	if (ret) {
		bpf_printk("%s:%d test failed", __func__, __LINE__);
		return ret;
	}

	return 0;
}

int asan_test_static(void)
{
	int ret;

	ret = asan_test_static_blob();
	if (ret) {
		bpf_printk("%s:%d test failed", __func__, __LINE__);
		return ret;
	}

	ret = asan_test_static_array();
	if (ret) {
		bpf_printk("%s:%d test failed", __func__, __LINE__);
		return ret;
	}

	ret = asan_test_static_all();
	if (ret) {
		bpf_printk("%s:%d test failed", __func__, __LINE__);
		return ret;
	}

	return 0;
}

private(ST_BUDDY) struct buddy st_buddy_asan;

__weak int asan_test_buddy_oob_single(size_t alloc_size)
{
	u8 __arena *mem;
	int i;

	ASAN_VALIDATE();

	mem = buddy_alloc(&st_buddy_asan, alloc_size);
	if (!mem) {
		bpf_printk("buddy_alloc failed for size %lu", alloc_size);
		return -ENOMEM;
	}

	ASAN_VALIDATE();

	bpf_for(i, 0, alloc_size) {
		mem[i] = 0xba;
		ASAN_VALIDATE_ADDR(false, &mem[i]);
	}

	mem[alloc_size] = 0xba;
	ASAN_VALIDATE_ADDR(true, &mem[alloc_size]);

	buddy_free(&st_buddy_asan, mem);

	return 0;
}

__weak int asan_test_buddy_uaf_single(size_t alloc_size)
{
	u8 __arena *mem;
	int i;

	mem = buddy_alloc(&st_buddy_asan, alloc_size);
	if (!mem) {
		bpf_printk("buddy_alloc failed for size %lu", alloc_size);
		return -ENOMEM;
	}

	ASAN_VALIDATE();

	bpf_for(i, 0, alloc_size) {
		mem[i] = 0xba;
		ASAN_VALIDATE_ADDR(false, &mem[i]);
	}

	ASAN_VALIDATE();

	buddy_free(&st_buddy_asan, mem);

	bpf_for(i, 0, alloc_size) {
		/* The header doesn't get poisoned. */
		if (BUDDY_HEADER_OFF <= i &&
		    i < BUDDY_HEADER_OFF + sizeof(struct buddy_header))
			continue;

		mem[i] = 0xba;
		ASAN_VALIDATE_ADDR(true, &mem[i]);
	}

	return 0;
}

struct buddy_blob {
	volatile u8 mem[48];
	u8 oob;
};

__weak int asan_test_buddy_blob_single(void)
{
	volatile struct buddy_blob __arena *blob;
	const size_t alloc_size = sizeof(struct buddy_blob) - 1;

	blob = buddy_alloc(&st_buddy_asan, alloc_size);
	if (!blob)
		return -ENOMEM;

	blob->mem[0] = 0xba;
	ASAN_VALIDATE_ADDR(false, &blob->mem[0]);

	blob->mem[47] = 0xba;
	ASAN_VALIDATE_ADDR(false, &blob->mem[47]);

	blob->oob = 0;
	ASAN_VALIDATE_ADDR(true, &blob->oob);

	buddy_free(&st_buddy_asan, (void __arena *)blob);

	return 0;
}

__weak int asan_test_buddy_oob(void)
{
	size_t sizes[] = {
		7, 8, 17, 18, 64, 256, 317, 512, 1024,
	};
	int ret, i;

	ret = buddy_init(&st_buddy_asan,
			     (arena_spinlock_t __arena *)&st_asan_lock);
	if (ret) {
		bpf_printk("buddy_init failed with %d", ret);
		return ret;
	}

	bpf_for(i, 0, 7) {
		ret = asan_test_buddy_oob_single(sizes[i]);
		if (ret) {
			bpf_printk("%s:%d Failed for size %lu", __func__,
				   __LINE__, sizes[i]);
			buddy_destroy(&st_buddy_asan);
			return ret;
		}
	}

	buddy_destroy(&st_buddy_asan);

	ASAN_VALIDATE();

	return 0;
}

__weak int asan_test_buddy_uaf(void)
{
	size_t sizes[] = { 16, 32, 64, 128, 256, 512, 128, 1024, 16384 };
	int ret, i;

	ret = buddy_init(&st_buddy_asan,
			     (arena_spinlock_t __arena *)&st_asan_lock);
	if (ret) {
		bpf_printk("buddy_init failed with %d", ret);
		return ret;
	}

	bpf_for(i, 0, 7) {
		ret = asan_test_buddy_uaf_single(sizes[i]);
		if (ret) {
			bpf_printk("%s:%d Failed for size %lu", __func__,
				   __LINE__, sizes[i]);
			buddy_destroy(&st_buddy_asan);
			return ret;
		}
	}

	buddy_destroy(&st_buddy_asan);

	ASAN_VALIDATE();

	return 0;
}

__weak int asan_test_buddy_blob(void)
{
	const int iters = 10;
	int ret, i;

	ret = buddy_init(&st_buddy_asan,
			     (arena_spinlock_t __arena *)&st_asan_lock);
	if (ret) {
		bpf_printk("buddy_init failed with %d", ret);
		return ret;
	}

	for (i = 0; i < iters && can_loop; i++) {
		ret = asan_test_buddy_blob_single();
		if (ret) {
			bpf_printk("%s:%d Failed on iteration %d", __func__,
				   __LINE__, i);
			buddy_destroy(&st_buddy_asan);
			return ret;
		}
	}

	buddy_destroy(&st_buddy_asan);

	ASAN_VALIDATE();

	return 0;
}

int asan_test_buddy(void)
{
	int ret;

	ret = asan_test_buddy_oob();
	if (ret) {
		bpf_printk("%s:%d OOB test failed", __func__, __LINE__);
		return ret;
	}

	ret = asan_test_buddy_uaf();
	if (ret) {
		bpf_printk("%s:%d UAF test failed", __func__, __LINE__);
		return ret;
	}

	ret = asan_test_buddy_blob();
	if (ret) {
		bpf_printk("%s:%d blob test failed", __func__, __LINE__);
		return ret;
	}

	return 0;
}

SEC("syscall")
int asan_test(void)
{
	int ret;

	bpf_printk("ASAN tests starting...");

	ret = asan_test_static();
	if (ret)
		return ret;

	bpf_printk("STATIC test passed...");

	ret = asan_test_stack();
	if (ret)
		return ret;

	bpf_printk("STACK test passed...");

	ret = asan_test_buddy();
	if (ret)
		return ret;

	bpf_printk("BUDDY test passed...");

	bpf_printk("ASAN tests successful.");

	return 0;
}

#else

SEC("syscall")
int asan_test(void)
{
	return -EOPNOTSUPP;
}

#endif /* BPF_ARENA_ASAN */