@@ -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 ;
@@ -53,9 +55,69 @@ pub mod guest_logger;
5355pub mod host_comm;
5456pub mod memory;
5557
58+ // Globals
59+ #[ cfg( feature = "mem_profile" ) ]
60+ struct ProfiledLockedHeap < const ORDER : usize > ( LockedHeap < ORDER > ) ;
61+ #[ cfg( feature = "mem_profile" ) ]
62+ unsafe impl < const ORDER : usize > alloc:: alloc:: GlobalAlloc for ProfiledLockedHeap < ORDER > {
63+ unsafe fn alloc ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
64+ let addr = unsafe { self . 0 . alloc ( layout) } ;
65+ unsafe {
66+ core:: arch:: asm!( "out dx, al" ,
67+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
68+ in( "rax" ) layout. size( ) as u64 ,
69+ in( "rcx" ) addr as u64 ) ;
70+ }
71+ addr
72+ }
73+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : core:: alloc:: Layout ) {
74+ unsafe {
75+ core:: arch:: asm!( "out dx, al" ,
76+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
77+ in( "rax" ) layout. size( ) as u64 ,
78+ in( "rcx" ) ptr as u64 ) ;
79+ self . 0 . dealloc ( ptr, layout)
80+ }
81+ }
82+ unsafe fn alloc_zeroed ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
83+ let addr = unsafe { self . 0 . alloc_zeroed ( layout) } ;
84+ unsafe {
85+ core:: arch:: asm!( "out dx, al" ,
86+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
87+ in( "rax" ) layout. size( ) as u64 ,
88+ in( "rcx" ) addr as u64 ) ;
89+ }
90+ addr
91+ }
92+ unsafe fn realloc (
93+ & self ,
94+ ptr : * mut u8 ,
95+ layout : core:: alloc:: Layout ,
96+ new_size : usize ,
97+ ) -> * mut u8 {
98+ let new_ptr = unsafe { self . 0 . realloc ( ptr, layout, new_size) } ;
99+ unsafe {
100+ core:: arch:: asm!( "out dx, al" ,
101+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
102+ in( "rax" ) layout. size( ) as u64 ,
103+ in( "rcx" ) ptr) ;
104+ core:: arch:: asm!( "out dx, al" ,
105+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
106+ in( "rax" ) new_size as u64 ,
107+ in( "rcx" ) new_ptr) ;
108+ }
109+ new_ptr
110+ }
111+ }
112+
56113// === Globals ===
114+ #[ cfg( not( feature = "mem_profile" ) ) ]
57115#[ global_allocator]
58116pub ( crate ) static HEAP_ALLOCATOR : LockedHeap < 32 > = LockedHeap :: < 32 > :: empty ( ) ;
117+ #[ cfg( feature = "mem_profile" ) ]
118+ #[ global_allocator]
119+ pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > =
120+ ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
59121
60122pub ( crate ) static mut GUEST_HANDLE : GuestHandle = GuestHandle :: new ( ) ;
61123pub ( crate ) static mut REGISTERED_GUEST_FUNCTIONS : GuestFunctionRegister =
@@ -128,7 +190,11 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
128190
129191 let heap_start = ( * peb_ptr) . guest_heap . ptr as usize ;
130192 let heap_size = ( * peb_ptr) . guest_heap . size as usize ;
131- HEAP_ALLOCATOR
193+ #[ cfg( not( feature = "mem_profile" ) ) ]
194+ let heap_allocator = & HEAP_ALLOCATOR ;
195+ #[ cfg( feature = "mem_profile" ) ]
196+ let heap_allocator = & HEAP_ALLOCATOR . 0 ;
197+ heap_allocator
132198 . try_lock ( )
133199 . expect ( "Failed to access HEAP_ALLOCATOR" )
134200 . init ( heap_start, heap_size) ;
0 commit comments