microsoft / microsoft/wil

Token query types and performance optimizations

Open
#332 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
3k
Forks
300
Avg merge
19h 12m
Merged PRs (30d)
1

Description

Hello,

The WIL library defines some token classes without fixed sizes. For example:

https://github.com/microsoft/wil/blob/a8cd266d5c76a19309fdad55ebda97adb556daa8/include/wil/token_helpers.h#L38-L57

TOKEN_USER was defined FixedSize = false when it has a fixed size defined in the Windows SDK:

#define TOKEN_USER_MAX_SIZE (sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE)

The others also have types in the Windows SDK:

#define TOKEN_OWNER_MAX_SIZE (sizeof(TOKEN_OWNER) + SECURITY_MAX_SID_SIZE)
#define TOKEN_PRIMARY_GROUP_MAX_SIZE (sizeof(TOKEN_PRIMARY_GROUP) + SECURITY_MAX_SID_SIZE)
#define TOKEN_TOKEN_DEFAULT_DACL_MAX_SIZE (sizeof(TOKEN_DEFAULT_DACL) + SECURITY_MAX_SID_SIZE)

As an example this WIL code:

wil::unique_tokeninfo_ptr<TOKEN_USER> user;
RETURN_IF_FAILED(wil::get_token_information_nothrow(user, GetCurrentProcessToken()));

Is expanded into:
https://github.com/microsoft/wil/blob/a8cd266d5c76a19309fdad55ebda97adb556daa8/include/wil/token_helpers.h#L177-L188

We query TOKEN_USER and TOKEN_OWNER hundreds of times a day and since WIL doesn't use the fixed sizes for these types it's instead using multiple system calls to query the size, then multiple system calls to allocate virtual memory (also waiting/blocking the process heap lock) which is a significantly more slower and larger overhead than necessary.

The WIL templates should be doing something like this instead:

    ULONG returnLength;
    UCHAR tokenUserBuffer[TOKEN_USER_MAX_SIZE];
    PTOKEN_USER tokenUser = (PTOKEN_USER)tokenUserBuffer;

    GetTokenInformation(
        TokenHandle,
        TokenUser,
        tokenUser,
        sizeof(tokenUserBuffer),
        &returnLength
        );

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in include/wil/token_helpers.h, especially the token class definitions around lines 38-57 and the allocation path around lines 177-188. Compare the Windows SDK maximum-size macros for TOKEN_USER, TOKEN_OWNER, TOKEN_PRIMARY_GROUP, and TOKEN_DEFAULT_DACL with the current template behavior. Done means these fixed-size token queries avoid unnecessary size queries and virtual-memory allocations while retaining correct token information handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.