@@ -17,7 +17,7 @@ struct process *current_process = 0;
1717
1818static struct process * processes [PEACHOS_MAX_PROCESSES ] = {};
1919
20- struct process_allocation * process_get_allocation_by_start_addr (struct process * process , void * addr , struct process_allocation * allocation_out );
20+ int process_get_allocation_by_start_addr (struct process * process , void * addr , struct process_allocation * allocation_out );
2121
2222int process_free_process (struct process * process );
2323int process_close_file_handles (struct process * process );
@@ -159,17 +159,27 @@ static bool process_is_process_pointer(struct process *process, void *ptr)
159159
160160static void process_allocation_unjoin (struct process * process , void * ptr )
161161{
162- for (int i = 0 ; i < PEACHOS_MAX_PROGRAM_ALLOCATIONS ; i ++ )
162+ size_t total_allocations = vector_count (process -> allocations );
163+ for (size_t i = 0 ; i < total_allocations ; i ++ )
163164 {
164- if (process -> allocations [i ].ptr == ptr )
165+ struct process_allocation allocation ;
166+ int res = vector_at (process -> allocations , i , & allocation , sizeof (allocation ));
167+ if (res < 0 )
168+ {
169+ break ;
170+ }
171+
172+ if (allocation .ptr == ptr )
165173 {
166- process -> allocations [i ].ptr = 0x00 ;
167- process -> allocations [i ].size = 0 ;
174+ allocation .ptr = NULL ;
175+ allocation .end = NULL ;
176+ allocation .size = 0 ;
177+ vector_overwrite (process -> allocations , i , & allocation , sizeof (allocation ));
168178 }
169179 }
170180}
171181
172- struct process_allocation * process_get_allocation_by_start_addr (struct process * process , void * addr , struct process_allocation * allocation_out )
182+ int process_get_allocation_by_start_addr (struct process * process , void * addr , struct process_allocation * allocation_out )
173183{
174184 size_t total_allocations = vector_count (process -> allocations );
175185 for (size_t i = 0 ; i < total_allocations ; i ++ )
@@ -278,6 +288,10 @@ int process_free_process(struct process *process)
278288 process_free_program_data (process );
279289 process_close_file_handles (process );
280290
291+ // Free the process allocations
292+ vector_free (process -> allocations );
293+ process -> allocations = NULL ;
294+
281295 // Free the process stack memory.
282296 if (process -> stack )
283297 {
@@ -382,7 +396,7 @@ void process_free(struct process *process, void *ptr)
382396 return ;
383397 }
384398
385- int res = paging_map_to (process -> task -> paging_desc , allocation .ptr , allocation .ptr , paging_align_address (allocation .ptr + allocation .size ), 0x00 );
399+ res = paging_map_to (process -> task -> paging_desc , allocation .ptr , allocation .ptr , paging_align_address (allocation .ptr + allocation .size ), 0x00 );
386400 if (res < 0 )
387401 {
388402 return ;
@@ -638,6 +652,86 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
638652 return res ;
639653}
640654
655+ bool process_is_stack_memory (struct process * process , void * addr )
656+ {
657+ return (uintptr_t ) addr >= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END &&
658+ (uintptr_t ) addr <= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START ;
659+ }
660+
661+ int process_get_allocation_by_addr (struct process * process , void * addr , struct process_allocation_request * allocation_request_out )
662+ {
663+ // Null the request
664+ memset (allocation_request_out , 0 , sizeof (struct process_allocation_request ));
665+
666+ // Is this stack memory?
667+ if (process_is_stack_memory (process , addr ))
668+ {
669+ // we have stack memory
670+ uint64_t addr_int = (uint64_t ) addr ;
671+ uint64_t stack_size = PEACHOS_USER_PROGRAM_STACK_SIZE ;
672+ // START OF THE STACK IS HIGHER IN MEMORY REMEMBER
673+ uint64_t total_bytes_left = PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START - addr_int ;
674+ allocation_request_out -> allocation .ptr = (void * ) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END ;
675+ allocation_request_out -> allocation .end = (void * ) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START ;
676+ allocation_request_out -> allocation .size = stack_size ;
677+ allocation_request_out -> flags |= PROCESS_ALLOCATION_REQUEST_IS_STACK_MEMORY ;
678+ allocation_request_out -> peek .addr = addr ;
679+ allocation_request_out -> peek .end = (void * ) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START ;
680+ allocation_request_out -> peek .total_bytes_left = total_bytes_left ;
681+ return 0 ;
682+ }
683+
684+ // Not a stack address then check the heap
685+ size_t total_allocations = vector_count (process -> allocations );
686+ for (size_t i = 0 ; i < total_allocations ; i ++ )
687+ {
688+ struct process_allocation allocation ;
689+ int res = vector_at (process -> allocations , i , & allocation , sizeof (allocation ));
690+ if (res < 0 )
691+ {
692+ break ;
693+ }
694+
695+ uint64_t allocation_addr = (uint64_t ) allocation .ptr ;
696+ uint64_t allocation_addr_end = (uint64_t ) allocation .end ;
697+ if ((uint64_t ) addr >= allocation_addr &&
698+ ((uint64_t ) addr ) <= allocation_addr_end )
699+ {
700+ size_t bytes_used = (uint64_t ) addr - allocation_addr ;
701+ size_t bytes_left = allocation_addr_end - bytes_used ;
702+ allocation_request_out -> allocation = allocation ;
703+ allocation_request_out -> peek .addr = addr ;
704+ allocation_request_out -> peek .end = (void * ) allocation_addr_end ;
705+ allocation_request_out -> peek .total_bytes_left = bytes_left ;
706+ return 0 ;
707+ }
708+ }
709+
710+ return - EIO ;
711+ }
712+
713+ int process_validate_memory_or_terminate (struct process * process , void * virt_addr , size_t space_needed )
714+ {
715+ int res = 0 ;
716+ struct process_allocation_request allocation_request ;
717+ res = process_get_allocation_by_addr (process , virt_addr , & allocation_request );
718+ if (res < 0 )
719+ {
720+ goto out ;
721+ }
722+
723+ if (allocation_request .peek .total_bytes_left < space_needed )
724+ {
725+ res = - EINVARG ;
726+ goto out ;
727+ }
728+ out :
729+ if (res < 0 )
730+ {
731+ process_terminate (process );
732+ }
733+ return res ;
734+ }
641735int process_fread (struct process * process , void * virt_ptr , uint64_t size , uint64_t nmemb , int fd )
642736{
643737 int res = 0 ;
0 commit comments