Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IoGetCurrentIrpStackLocation function in ntddk.rs #251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

0xflux
Copy link

@0xflux 0xflux commented Dec 6, 2024

Summary

This PR adds the IoGetCurrentIrpStackLocation function, which retrieves a pointer to the caller's I/O stack location from a given IRP.

  • The function is implemented to access raw pointers and union fields.
  • Documentation has been added to describe the parameters, return value, and safety considerations.
  • The function includes an assert! to ensure CurrentLocation is valid, guarding against invalid IRPs. This was implemented as per the original C implementation.

Safety

This function requires the caller to guarantee that the provided IRP pointer is valid and properly initialised, as it operates on raw memory.

Future improvements could include replacing the assert! with proper error handling or returning an Option to handle invalid IRPs gracefully. Given that the original implementation in the C libraries does not implement this behaviour, I have not added it to this commit, but can amend if required.

C reference

For reference, the implementation in wdm.h is:

FORCEINLINE
__drv_aliasesMem
PIO_STACK_LOCATION
IoGetCurrentIrpStackLocation(
    _In_ PIRP Irp
)
/*--

Routine Description:

    This routine is invoked to return a pointer to the current stack location
    in an I/O Request Packet (IRP).

Arguments:

    Irp - Pointer to the I/O Request Packet.

Return Value:

    The function value is a pointer to the current stack location in the
    packet.

--*/
{
    NT_ASSERT(Irp->CurrentLocation <= Irp->StackCount + 1);
    return Irp->Tail.Overlay.CurrentStackLocation;
}

This commit adds the `IoGetCurrentIrpStackLocation` function, which retrieves a pointer to the caller's I/O stack location from a given IRP.

- The function is implemented to access raw pointers and union fields.
- Documentation has been added to describe the parameters, return value, and safety considerations.
- The function includes an `assert!` to ensure `CurrentLocation` is valid, guarding against invalid IRPs. This was implemented as per the original C implementation.

## Safety

This function requires the caller to guarantee that the provided IRP pointer is valid and properly initialised, as it operates on raw memory.

Future improvements could include replacing the `assert!` with proper error handling or returning an `Option` to handle invalid IRPs gracefully. Given that the original implementation in the C libraries does not implement this behaviour, I have not added it to this commit, but can amend if required.
@0xflux
Copy link
Author

0xflux commented Dec 6, 2024

@microsoft-github-policy-service agree

@0xflux 0xflux mentioned this pull request Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant