@@ -1536,6 +1536,11 @@ impl MemoryManager {
15361536 }
15371537 }
15381538
1539+ /// Which process's address space this MemoryManager manages.
1540+ pub fn pid ( & self ) -> nix:: unistd:: Pid {
1541+ self . pid
1542+ }
1543+
15391544 /// Initialize the MemoryMapper, allowing for more efficient access. Needs a
15401545 /// running thread.
15411546 pub fn init_mapper ( & mut self , thread : & mut impl Thread ) {
@@ -1802,6 +1807,90 @@ impl MemoryManager {
18021807 }
18031808}
18041809
1810+ /// Memory allocated by Shadow, in a remote address space.
1811+ pub struct AllocdMem < T >
1812+ where
1813+ T : Pod ,
1814+ {
1815+ ptr : TypedPluginPtr < T > ,
1816+ }
1817+
1818+ impl < T > AllocdMem < T >
1819+ where
1820+ T : Pod ,
1821+ {
1822+ /// Allocate memory in the current active process.
1823+ pub fn new ( len : usize ) -> Self {
1824+ let bytes_len = len * std:: mem:: size_of :: < T > ( ) ;
1825+ let prot = libc:: PROT_READ | libc:: PROT_WRITE ;
1826+
1827+ // Allocate the memory in the plugin of the active thread. We don't
1828+ // bother with trying to mmap it into Shadow as well; since the memory
1829+ // will typically only be accessed once by Shadow (to write to it),
1830+ // doing so isn't worth the overhead or complexity.
1831+ let ptr = Worker :: with_active_thread_mut ( |thread| {
1832+ thread
1833+ . native_mmap (
1834+ PluginPtr :: from ( 0usize ) ,
1835+ bytes_len,
1836+ prot,
1837+ libc:: MAP_ANONYMOUS | libc:: MAP_PRIVATE ,
1838+ -1 ,
1839+ 0 ,
1840+ )
1841+ . unwrap ( )
1842+ } ) ;
1843+ // Add region to known mappings.
1844+ Worker :: with_active_process_memory_mut ( |mem| {
1845+ if let Some ( mapper) = & mut mem. memory_mapper {
1846+ let base = usize:: from ( ptr) ;
1847+ let mutations = mapper. regions . insert (
1848+ base..( base + bytes_len) ,
1849+ Region {
1850+ shadow_base : std:: ptr:: null_mut ( ) ,
1851+ prot,
1852+ sharing : Sharing :: Private ,
1853+ original_path : None ,
1854+ } ,
1855+ ) ;
1856+ // Shouldn't have overwritten any previous known mappings.
1857+ debug_assert_eq ! ( mutations. len( ) , 0 ) ;
1858+ }
1859+ } ) ;
1860+ Self {
1861+ ptr : TypedPluginPtr :: < T > :: new ( ptr, len) . unwrap ( ) ,
1862+ }
1863+ }
1864+
1865+ /// Pointer to the allocated memory.
1866+ pub fn ptr ( & self ) -> TypedPluginPtr < T > {
1867+ self . ptr
1868+ }
1869+ }
1870+
1871+ impl < T > Drop for AllocdMem < T >
1872+ where
1873+ T : Pod ,
1874+ {
1875+ fn drop ( & mut self ) {
1876+ Worker :: with_active_thread_mut ( |thread| {
1877+ thread
1878+ . native_munmap ( self . ptr . ptr ( ) , self . ptr . len ( ) )
1879+ . unwrap ( )
1880+ } ) ;
1881+ // Add region to known mappings.
1882+ Worker :: with_active_process_memory_mut ( |mem| {
1883+ if let Some ( mapper) = & mut mem. memory_mapper {
1884+ let base = usize:: from ( self . ptr . ptr ( ) ) ;
1885+ let bytes_len = self . ptr . len ( ) * std:: mem:: size_of :: < T > ( ) ;
1886+ let mutations = mapper. regions . clear ( base..( base + bytes_len) ) ;
1887+ // Should've dropped exactly one entry.
1888+ debug_assert_eq ! ( mutations. len( ) , 1 ) ;
1889+ }
1890+ } ) ;
1891+ }
1892+ }
1893+
18051894mod export {
18061895 use super :: * ;
18071896
@@ -1821,6 +1910,23 @@ mod export {
18211910 mm. as_mut ( ) . map ( |mm| Box :: from_raw ( mm) ) ;
18221911 }
18231912
1913+ #[ no_mangle]
1914+ pub unsafe extern "C" fn allocdmem_new ( len : usize ) -> * mut AllocdMem < u8 > {
1915+ Box :: into_raw ( Box :: new ( AllocdMem :: new ( len) ) )
1916+ }
1917+
1918+ #[ no_mangle]
1919+ pub unsafe extern "C" fn allocdmem_free ( allocd_mem : * mut AllocdMem < u8 > ) {
1920+ allocd_mem
1921+ . as_mut ( )
1922+ . map ( |allocd_mem| Box :: from_raw ( allocd_mem) ) ;
1923+ }
1924+
1925+ #[ no_mangle]
1926+ pub unsafe extern "C" fn allocdmem_pluginPtr ( allocd_mem : * const AllocdMem < u8 > ) -> c:: PluginPtr {
1927+ allocd_mem. as_ref ( ) . unwrap ( ) . ptr ( ) . ptr ( ) . into ( )
1928+ }
1929+
18241930 /// Initialize the MemoryMapper if it isn't already initialized. `thread` must
18251931 /// be running and ready to make native syscalls.
18261932 #[ no_mangle]
0 commit comments