@@ -28,6 +28,8 @@ use guest_function::register::GuestFunctionRegister;
2828use guest_logger:: init_logger;
2929use hyperlight_common:: flatbuffer_wrappers:: guest_error:: ErrorCode ;
3030use hyperlight_common:: mem:: HyperlightPEB ;
31+ #[ cfg( feature = "mem_profile" ) ]
32+ use hyperlight_common:: outb:: OutBAction ;
3133use hyperlight_guest:: exit:: { abort_with_code_and_message, halt} ;
3234use hyperlight_guest:: guest_handle:: handle:: GuestHandle ;
3335use log:: LevelFilter ;
@@ -54,9 +56,69 @@ pub mod host_comm;
5456pub mod memory;
5557pub mod paging;
5658
59+ // Globals
60+ #[ cfg( feature = "mem_profile" ) ]
61+ struct ProfiledLockedHeap < const ORDER : usize > ( LockedHeap < ORDER > ) ;
62+ #[ cfg( feature = "mem_profile" ) ]
63+ unsafe impl < const ORDER : usize > alloc:: alloc:: GlobalAlloc for ProfiledLockedHeap < ORDER > {
64+ unsafe fn alloc ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
65+ let addr = unsafe { self . 0 . alloc ( layout) } ;
66+ unsafe {
67+ core:: arch:: asm!( "out dx, al" ,
68+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
69+ in( "rax" ) layout. size( ) as u64 ,
70+ in( "rcx" ) addr as u64 ) ;
71+ }
72+ addr
73+ }
74+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : core:: alloc:: Layout ) {
75+ unsafe {
76+ core:: arch:: asm!( "out dx, al" ,
77+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
78+ in( "rax" ) layout. size( ) as u64 ,
79+ in( "rcx" ) ptr as u64 ) ;
80+ self . 0 . dealloc ( ptr, layout)
81+ }
82+ }
83+ unsafe fn alloc_zeroed ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
84+ let addr = unsafe { self . 0 . alloc_zeroed ( layout) } ;
85+ unsafe {
86+ core:: arch:: asm!( "out dx, al" ,
87+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
88+ in( "rax" ) layout. size( ) as u64 ,
89+ in( "rcx" ) addr as u64 ) ;
90+ }
91+ addr
92+ }
93+ unsafe fn realloc (
94+ & self ,
95+ ptr : * mut u8 ,
96+ layout : core:: alloc:: Layout ,
97+ new_size : usize ,
98+ ) -> * mut u8 {
99+ let new_ptr = unsafe { self . 0 . realloc ( ptr, layout, new_size) } ;
100+ unsafe {
101+ core:: arch:: asm!( "out dx, al" ,
102+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
103+ in( "rax" ) layout. size( ) as u64 ,
104+ in( "rcx" ) ptr) ;
105+ core:: arch:: asm!( "out dx, al" ,
106+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
107+ in( "rax" ) new_size as u64 ,
108+ in( "rcx" ) new_ptr) ;
109+ }
110+ new_ptr
111+ }
112+ }
113+
57114// === Globals ===
115+ #[ cfg( not( feature = "mem_profile" ) ) ]
58116#[ global_allocator]
59117pub ( crate ) static HEAP_ALLOCATOR : LockedHeap < 32 > = LockedHeap :: < 32 > :: empty ( ) ;
118+ #[ cfg( feature = "mem_profile" ) ]
119+ #[ global_allocator]
120+ pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > =
121+ ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
60122
61123pub ( crate ) static mut GUEST_HANDLE : GuestHandle = GuestHandle :: new ( ) ;
62124pub ( crate ) static mut REGISTERED_GUEST_FUNCTIONS : GuestFunctionRegister =
@@ -129,7 +191,11 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
129191
130192 let heap_start = ( * peb_ptr) . guest_heap . ptr as usize ;
131193 let heap_size = ( * peb_ptr) . guest_heap . size as usize ;
132- HEAP_ALLOCATOR
194+ #[ cfg( not( feature = "mem_profile" ) ) ]
195+ let heap_allocator = & HEAP_ALLOCATOR ;
196+ #[ cfg( feature = "mem_profile" ) ]
197+ let heap_allocator = & HEAP_ALLOCATOR . 0 ;
198+ heap_allocator
133199 . try_lock ( )
134200 . expect ( "Failed to access HEAP_ALLOCATOR" )
135201 . init ( heap_start, heap_size) ;
0 commit comments