|  | 
| 3 | 3 | 
 | 
| 4 | 4 | //! Methods to construct page tables on x64. | 
| 5 | 5 | 
 | 
|  | 6 | +use crate::Error; | 
| 6 | 7 | use crate::IdentityMapSize; | 
| 7 | 8 | use zerocopy::FromBytes; | 
| 8 | 9 | use zerocopy::Immutable; | 
| @@ -345,8 +346,10 @@ impl PageTableBuilder { | 
| 345 | 346 |         self, | 
| 346 | 347 |         page_table: &mut [PageTable], | 
| 347 | 348 |         flattened_page_table: &'a mut [u8], | 
| 348 |  | -    ) -> &'a [u8] { | 
| 349 |  | -        assert!(flattened_page_table.len() == (page_table.len() * PAGE_TABLE_SIZE)); | 
|  | 349 | +    ) -> Result<&'a [u8], Error> { | 
|  | 350 | +        if flattened_page_table.len() != (page_table.len() * PAGE_TABLE_SIZE) { | 
|  | 351 | +            return Err(Error::BadBufferSize); | 
|  | 352 | +        } | 
| 350 | 353 | 
 | 
| 351 | 354 |         const SIZE_512_GB: u64 = 0x8000000000; | 
| 352 | 355 | 
 | 
| @@ -488,7 +491,11 @@ impl PageTableBuilder { | 
| 488 | 491 |         } | 
| 489 | 492 | 
 | 
| 490 | 493 |         // Flatten page table vec into u8 vec | 
| 491 |  | -        flatten_page_table(page_table, flattened_page_table, page_table_index + 1) | 
|  | 494 | +        Ok(flatten_page_table( | 
|  | 495 | +            page_table, | 
|  | 496 | +            flattened_page_table, | 
|  | 497 | +            page_table_index + 1, | 
|  | 498 | +        )) | 
| 492 | 499 |     } | 
| 493 | 500 | } | 
| 494 | 501 | 
 | 
| @@ -537,8 +544,10 @@ impl IdentityMapBuilder { | 
| 537 | 544 |         self, | 
| 538 | 545 |         page_table: &mut [PageTable], | 
| 539 | 546 |         flattened_page_table: &'a mut [u8], | 
| 540 |  | -    ) -> &'a [u8] { | 
| 541 |  | -        assert!(flattened_page_table.len() == (page_table.len() * PAGE_TABLE_SIZE)); | 
|  | 547 | +    ) -> Result<&'a [u8], Error> { | 
|  | 548 | +        if flattened_page_table.len() != (page_table.len() * PAGE_TABLE_SIZE) { | 
|  | 549 | +            return Err(Error::BadBufferSize); | 
|  | 550 | +        } | 
| 542 | 551 |         // Allocate page tables. There are up to 6 total page tables: | 
| 543 | 552 |         //      1 PML4E (Level 4) (omitted if the address bias is non-zero) | 
| 544 | 553 |         //      1 PDPTE (Level 3) | 
| @@ -615,7 +624,11 @@ impl IdentityMapBuilder { | 
| 615 | 624 |         } | 
| 616 | 625 | 
 | 
| 617 | 626 |         // Flatten page table vec into u8 vec | 
| 618 |  | -        flatten_page_table(page_table, flattened_page_table, page_table_count) | 
|  | 627 | +        Ok(flatten_page_table( | 
|  | 628 | +            page_table, | 
|  | 629 | +            flattened_page_table, | 
|  | 630 | +            page_table_count, | 
|  | 631 | +        )) | 
| 619 | 632 |     } | 
| 620 | 633 | } | 
| 621 | 634 | 
 | 
|  | 
0 commit comments