Skip to content

[Feature]: config_tables: Use the zerocopy crate to Reduce Unsafe Code Blocks #893

@VivekYadav7272

Description

@VivekYadav7272

Feature Overview

The zerocopy crate provides safe (wrapped unsafe) memory manipulations. This offers the ability to remove a lot of unsafe blocks in Patina and rely on zerocopy to convert to and from bytes, etc.

Solution Overview

Use zerocopy in config_tables module to remove unsafe usage (I'm assuming the ones that pertains to pointer-object casting?)

Alternatives Considered

No response

Urgency

Medium

Are you going to implement the feature request?

I will implement the feature

Do you need maintainer feedback?

Maintainer feedback requested

Anything else?

It'd be nice to get more clarification on exactly the kind of unsafe usage we want prevented. To be specific, for example:

In config_tables/memory_attributes_table.rs:

match core_allocate_pool(efi::BOOT_SERVICES_DATA, size_of::<efi::MemoryAttributesTable>()) {
            Ok(empty_ptr) => {
                if let Some(empty_mat) = unsafe { (empty_ptr as *mut efi::MemoryAttributesTable).as_mut() } {
...
  1. Now this sort of unsafe usage seems like a good candidate for using zerocopy. However, regardless, it will still require a cast to a byte slice, which would be an unsafe operation. That could probably also be eliminated if we have a Box-like allocation API -- is that something that's in-scope for this feature or should we let the unsafe needed to cast into a byte slice remain?
  2. Also, looks like zerocopy is primarily driven by derive macros and you can't provide custom implementations for those traits. But that means we have a recursive dependency for all children members to also have it implemented. In this case specifically, pub struct MemoryAttributesTable(*mut efi::MemoryAttributesTable); can't implement it until efi::MemoryAttributesTable does, which is an external dependency. Do I duplicate the struct? Is there some other way to implement these traits that I'm missing? Sorry if that's a stupid question.

Metadata

Metadata

Labels

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions