Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 61080cb

Browse files
committed
Allow use of pinned heap registers
1 parent 0b51fe7 commit 61080cb

File tree

15 files changed

+110
-15
lines changed

15 files changed

+110
-15
lines changed

benchmarks/lucet-benchmarks/src/context.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ fn context_init(c: &mut Criterion) {
99

1010
c.bench_function("context_init", move |b| {
1111
b.iter(|| {
12-
ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
12+
ContextHandle::create_and_init(&mut *stack, f as usize, &[], std::ptr::null_mut())
13+
.unwrap();
1314
})
1415
});
1516
}
@@ -22,7 +23,13 @@ fn context_swap_return(c: &mut Criterion) {
2223
b.iter_batched(
2324
|| {
2425
let mut stack = vec![0u64; 1024].into_boxed_slice();
25-
let child = ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
26+
let child = ContextHandle::create_and_init(
27+
&mut *stack,
28+
f as usize,
29+
&[],
30+
std::ptr::null_mut(),
31+
)
32+
.unwrap();
2633
(stack, child)
2734
},
2835
|(stack, mut child)| unsafe {
@@ -44,8 +51,13 @@ fn context_init_swap_return(c: &mut Criterion) {
4451
|| vec![0u64; 1024].into_boxed_slice(),
4552
|mut stack| {
4653
let mut parent = ContextHandle::new();
47-
let mut child =
48-
ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
54+
let mut child = ContextHandle::create_and_init(
55+
&mut *stack,
56+
f as usize,
57+
&[],
58+
std::ptr::null_mut(),
59+
)
60+
.unwrap();
4961
unsafe { Context::swap(&mut parent, &mut child) };
5062
stack
5163
},
@@ -332,8 +344,13 @@ fn context_init_swap_return_many_args(c: &mut Criterion) {
332344
|| vec![0u64; 1024].into_boxed_slice(),
333345
|mut stack| {
334346
let mut parent = ContextHandle::new();
335-
let mut child =
336-
ContextHandle::create_and_init(&mut *stack, f as usize, &args).unwrap();
347+
let mut child = ContextHandle::create_and_init(
348+
&mut *stack,
349+
f as usize,
350+
&args,
351+
std::ptr::null_mut(),
352+
)
353+
.unwrap();
337354
unsafe { Context::swap(&mut parent, &mut child) };
338355
stack
339356
},

lucet-module/src/module_data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub struct ModuleFeatures {
5959
pub lzcnt: bool,
6060
pub popcnt: bool,
6161
pub instruction_count: bool,
62+
pub pinned_heap: bool,
6263
_hidden: (),
6364
}
6465

@@ -75,6 +76,7 @@ impl ModuleFeatures {
7576
lzcnt: false,
7677
popcnt: false,
7778
instruction_count: false,
79+
pinned_heap: false,
7880
_hidden: (),
7981
}
8082
}

lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ macro_rules! alloc_tests {
655655
inst.alloc_mut().stack_u64_mut(),
656656
heap_touching_child as usize,
657657
&[Val::CPtr(heap_ptr)],
658+
heap_ptr,
658659
)
659660
.expect("context init succeeds");
660661
Context::swap(&mut parent, &mut child);
@@ -704,6 +705,7 @@ macro_rules! alloc_tests {
704705
inst.alloc_mut().stack_u64_mut(),
705706
stack_pattern_child as usize,
706707
&[Val::CPtr(heap_ptr)],
708+
heap_ptr,
707709
)
708710
.expect("context init succeeds");
709711
Context::swap(&mut parent, &mut child);

lucet-runtime/lucet-runtime-internals/src/context/context_asm.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ _lucet_context_swap:
118118
mov %r14, (6*8)(%rdi)
119119
mov %r15, (7*8)(%rdi)
120120
mov %rsi, (8*8)(%rdi)
121+
mov %r8, (9*8)(%rdi)
121122

122123
movdqu %xmm0, (10*8 + 0*16)(%rdi)
123124
movdqu %xmm1, (10*8 + 1*16)(%rdi)
@@ -137,6 +138,7 @@ _lucet_context_swap:
137138
mov (5*8)(%rsi), %r13
138139
mov (6*8)(%rsi), %r14
139140
mov (7*8)(%rsi), %r15
141+
mov (9*8)(%rsi), %r8
140142

141143
movdqu (10*8 + 0*16)(%rsi), %xmm0
142144
movdqu (10*8 + 1*16)(%rsi), %xmm1

lucet-runtime/lucet-runtime-internals/src/context/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub(crate) struct GpRegs {
3434
r14: u64,
3535
r15: u64,
3636
pub(crate) rsi: u64,
37+
r8: u64,
3738
}
3839

3940
impl GpRegs {
@@ -48,6 +49,7 @@ impl GpRegs {
4849
r14: 0,
4950
r15: 0,
5051
rsi: 0,
52+
r8: 0,
5153
}
5254
}
5355
}
@@ -208,9 +210,10 @@ impl ContextHandle {
208210
stack: &mut [u64],
209211
fptr: usize,
210212
args: &[Val],
213+
heap: *mut core::ffi::c_void,
211214
) -> Result<ContextHandle, Error> {
212215
let mut child = ContextHandle::new();
213-
Context::init(stack, &mut child, fptr, args)?;
216+
Context::init(stack, &mut child, fptr, args, heap)?;
214217
Ok(child)
215218
}
216219
}
@@ -303,6 +306,7 @@ impl Context {
303306
/// &mut child,
304307
/// entrypoint as usize,
305308
/// &[Val::U64(120), Val::F32(3.14)],
309+
/// std::ptr::null_mut(),
306310
/// );
307311
/// assert!(res.is_ok());
308312
/// ```
@@ -326,6 +330,7 @@ impl Context {
326330
/// &mut child,
327331
/// entrypoint as usize,
328332
/// &[Val::U64(120), Val::F32(3.14)],
333+
/// std::ptr::null_mut(),
329334
/// );
330335
/// assert!(res.is_ok());
331336
/// ```
@@ -367,6 +372,7 @@ impl Context {
367372
child: &mut Context,
368373
fptr: usize,
369374
args: &[Val],
375+
heap: *mut core::ffi::c_void,
370376
) -> Result<(), Error> {
371377
Context::init_with_callback(
372378
stack,
@@ -375,6 +381,7 @@ impl Context {
375381
ptr::null_mut(),
376382
fptr,
377383
args,
384+
heap,
378385
)
379386
}
380387

@@ -393,6 +400,7 @@ impl Context {
393400
callback_data: *mut Instance,
394401
fptr: usize,
395402
args: &[Val],
403+
heap: *mut core::ffi::c_void,
396404
) -> Result<(), Error> {
397405
if !stack_is_aligned(stack) {
398406
return Err(Error::UnalignedStack);
@@ -475,6 +483,10 @@ impl Context {
475483
// even at the entrypoint of the guest.
476484
child.gpr.rbp = child as *const Context as u64;
477485

486+
// Heap pinning: r15 is not used to pass any parameters on Windows/POSIX abis, we simply set this to be the value of the heap always.
487+
// This value will be used only when the lucet module loaded is compiled requiring use of the pinned heap register.
488+
child.gpr.r15 = heap as u64;
489+
478490
Ok(())
479491
}
480492

@@ -547,6 +559,7 @@ impl Context {
547559
/// &mut child,
548560
/// entrypoint as usize,
549561
/// &[],
562+
/// std::ptr::null_mut(),
550563
/// ).unwrap();
551564
///
552565
/// unsafe { Context::swap(&mut parent, &mut child); }

lucet-runtime/lucet-runtime-internals/src/context/tests/c_child.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ macro_rules! init_and_swap {
5555
&mut *$stack,
5656
$fn as usize,
5757
&[$( $args ),*],
58+
std::ptr::null_mut(),
5859
).unwrap()));
5960

6061
child_regs = child;

lucet-runtime/lucet-runtime-internals/src/context/tests/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ fn init_rejects_unaligned() {
3131
let mut stack_unaligned = unsafe { slice::from_raw_parts_mut(ptr, len) };
3232

3333
// now we have the unaligned stack, let's make sure it blows up right
34-
let res = ContextHandle::create_and_init(&mut stack_unaligned, dummy as usize, &[]);
34+
let res = ContextHandle::create_and_init(
35+
&mut stack_unaligned,
36+
dummy as usize,
37+
&[],
38+
std::ptr::null_mut(),
39+
);
3540

3641
if let Err(Error::UnalignedStack) = res {
3742
assert!(true);

lucet-runtime/lucet-runtime-internals/src/context/tests/rust_child.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ macro_rules! init_and_swap {
5151
&mut *$stack,
5252
$fn as usize,
5353
&[$( $args ),*],
54+
std::ptr::null_mut(),
5455
).unwrap();
5556
CHILD = Some(child);
5657

lucet-runtime/lucet-runtime-internals/src/instance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ impl Instance {
972972

973973
self.entrypoint = Some(func);
974974

975-
let mut args_with_vmctx = vec![Val::from(self.alloc.slot().heap)];
975+
let heap = self.alloc.slot().heap;
976+
let mut args_with_vmctx = vec![Val::from(heap)];
976977
args_with_vmctx.extend_from_slice(args);
977978

978979
let self_ptr = self as *mut _;
@@ -983,6 +984,7 @@ impl Instance {
983984
self_ptr,
984985
func.ptr.as_usize(),
985986
&args_with_vmctx,
987+
heap,
986988
)?;
987989

988990
self.install_activator();

lucet-runtime/lucet-runtime-tests/src/stack.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ macro_rules! stack_tests {
162162
expect_stack_overflow(
163163
// Same note as `expect_ok_locals64_481`
164164
stack_testcase(64 - 4).expect("generate stack_testcase 64"),
165-
481,
165+
// TODO: pick some high enough number that this test overflows still
166+
// cranelift changes made 480 the wrong magic number
167+
591,
166168
true,
167169
);
168170
}

0 commit comments

Comments
 (0)