Skip to content

Unexpected error 38 in std.os.windows.RtlGenRandom #23666

Closed

Activity

  1. added
    bug
    contributor friendly
    os-windows
    standard library
    on Apr 26, 2025
  2. added this to the 0.15.0 milestone on Apr 26, 2025
  3. changed the title [-]Unexpected error 1453 in `std.os.windows.RtlGenRandom`[/-] [+]Unexpected error 38 in `std.os.windows.RtlGenRandom`[/+] on Apr 26, 2025
  4. alexrp commented on Jun 4, 2025

    @alexrp
    MemberAuthor

    It's not actually clear what this error means in this case, which makes mapping it to a Zig error difficult. @marler8997 @squeek502 any ideas?

  5. squeek502 commented on Jun 4, 2025

    @squeek502
    Member

    Not sure, it seems like it might be related to running out of system resources, as that CI run was also hitting errors like:

    error.Unexpected: GetLastError(1455): The paging file is too small for this operation to complete.
    
    error: OutOfMemory
    

    Not sure if it's helpful, but:

  6. alexrp commented on Jun 4, 2025

    @alexrp
    MemberAuthor

    I guess returning error.SystemResources would be the way to go then. 🤷

  7. squeek502 commented on Jun 4, 2025

    @squeek502
    Member

    If this StackOverflow answer is accurate then RtlGenRandom calls ProcessPrng which is documented to not be able to fail 🙃

  8. squeek502 commented on Jun 4, 2025

    @squeek502
    Member

    My only other guess as to what might be happening is that calling this function causes a dll to be loaded (bcryptprimitives.dll, confirmed this with NtTrace), and that is what's failing in some way due to system resources running out, and that tangential error happens to be bubbling up here (however, still not sure how failing to load the dll would cause Reached the end of the file).

    I'm not entirely sure that's how that would work, though, and not totally sure how to go about testing that theory.

  9. squeek502 commented on Jun 5, 2025

    @squeek502
    Member

    After looking into this, IMO continuing to use std.os.windows.unexpectedError seems fairly appropriate here. Could maybe add a comment here like this for clarification:

    // This function has been seen to fail in the wild with error code 38 (Reached the end of the file),
    // but that happened while the system was running out of system resources and that failure
    // has not been reproduced. Additionally, RtlGenRandom internally calls ProcessPrng which
    // is documented to not be able to fail, so any error is truly unexpected.
    
  10. self-assigned this
    on Jun 5, 2025
  11. andrewrk commented on Jun 11, 2025

    @andrewrk
    Member

    I think this should be solved by swapping out our call of RtlGenRandom with ProcessPrng from bcryptPrimitives.dll which will avoid unnecessarily loading advapi32.dll.

    Is there any lower level API from a different DLL that that calls into observable with procmon / NtTrace?

    Although windows 7 is already older than our minimum supported windows, it wouldn't hurt to do a version check and call into RtlGenRandom when the target range is even wider than that. In that case I suggest mapping error code 38 to @panic with the justification that it's rare to hit and that's outside our minimum version supported anyway so we don't want it in the error set.

  12. alexrp commented on Jun 11, 2025

    @alexrp
    MemberAuthor

    Is there any lower level API from a different DLL that that calls into observable with procmon / NtTrace?

    No, ProcessPrng contains all the interesting implementation code.

    Although windows 7 is already older than our minimum supported windows, it wouldn't hurt to do a version check and call into RtlGenRandom when the target range is even wider than that. In that case I suggest mapping error code 38 to @panic with the justification that it's rare to hit and that's outside our minimum version supported anyway so we don't want it in the error set.

    I thought bcryptprimitives.dll was introduced in Windows 7? (And I don't think we need to go out of our way to support anything older than that.)

  13. mlugg commented on Jun 11, 2025

    @mlugg
    Member

    Not that it frankly means much, but Microsoft's docs do indicate that ProcessPrng is available on Windows 7:
    Image

  14. andrewrk commented on Jun 11, 2025

    @andrewrk
    Member

    Right, I'm saying it would be a fairly minimal maintenance burden to spend a couple lines handling when earlier than Windows 7 is targeted.

  15. removed their assignment
    on Jun 16, 2025
  16. relapids commented on Jun 17, 2025

    @relapids

    As far as I can tell from looking at the disassembly of the function (and what it calls), RtlGenRandom does not set the LastError value, so what is being reported is whatever stale value happens to be in there from a previous API call that does set it. Likely if you explicitly SetLastError before that call, that's what you will see reported instead of 38 or w/e. This is pretty normal for the Rtl* family of APIs (if it were to return an error code, it would do so via an NTSTATUS return instead).

  17. modified the milestones: urgent, 0.15.0 on Aug 8, 2025
  18. added a commit that references this issue on Aug 8, 2025
    0d0f09f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcontributor friendlyos-windowsstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions