GithubHelp home page GithubHelp logo

cxiao / rust_type_layout_helper_bn Goto Github PK

View Code? Open in Web Editor NEW
27.0 4.0 1.0 254 KB

An extremely experimental Binary Ninja importer for the type layout information emitted by the -Zprint-type-sizes flag of the Rust compiler.

License: MIT License

Python 100.00%
binary-ninja binary-ninja-plugin reverse-engineering rust rust-lang

rust_type_layout_helper_bn's Issues

#[repr(C)] union types are not imported correctly

When importing the following types:

print-type-size type: `std::sys_common::net::SocketAddrCRepr`: 28 bytes, alignment: 4 bytes
print-type-size     variant `SocketAddrCRepr`: 28 bytes
print-type-size         field `.v4`: 16 bytes
print-type-size         field `.v6`: 28 bytes, offset: 0 bytes, alignment: 4 bytes
print-type-size type: `std::sys::windows::c::IO_STATUS_BLOCK_union`: 8 bytes, alignment: 8 bytes
print-type-size     variant `IO_STATUS_BLOCK_union`: 8 bytes
print-type-size         field `.Status`: 4 bytes
print-type-size         field `.Pointer`: 8 bytes, offset: 0 bytes, alignment: 8 bytes

We see the following errors regarding size mismatches:

Size of created variant struct (44 bytes) does not match size of parsed Rust variant struct (28 bytes)
Created struct for Rust type std::sys_common::net::SocketAddrCRepr has size (44 bytes) which does not match size of parsed Rust type (28 bytes)
Size of created variant struct (12 bytes) does not match size of parsed Rust variant struct (8 bytes)
Created struct for Rust type std::sys::windows::c::IO_STATUS_BLOCK_union has size (12 bytes) which does not match size of parsed Rust type (8 bytes)

The original types definitions in Rust are:

#[repr(C)]
pub(crate) union SocketAddrCRepr {
    v4: c::sockaddr_in,
    v6: c::sockaddr_in6,
}

#[repr(C)]
union IO_STATUS_BLOCK_union {
    Status: NTSTATUS,
    Pointer: *mut c_void,
}

Source for SocketAddrCRepr (Rust 1.67.1): https://github.com/rust-lang/rust/blob/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/sys_common/net.rs#L720
Source for IO_STATUS_BLOCK_union (Rust 1.67.1): https://github.com/rust-lang/rust/blob/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/sys/windows/c.rs#LL335C8-L335C8

The created type definitions in Binary Ninja are the following. Rather than being created as an union with two members, these types are incorrectly created as a union containing a struct with two members. This causes the created type size to be too large.

struct std::sys_common::net::SocketAddrCRepr __packed
{
    union __packed
    {
        struct std::sys_common::net::SocketAddrCRepr::SocketAddrCRepr SocketAddrCRepr;
    } std::sys_common::net::SocketAddrCRepr::variants;
};

struct std::sys_common::net::SocketAddrCRepr::SocketAddrCRepr __packed
{
    int128_t .v4;
    char .v6[0x1c];
};

struct std::sys::windows::c::IO_STATUS_BLOCK_union __packed
{
    union __packed
    {
        struct std::sys::windows::c::IO_STATUS_BLOCK_union::IO_STATUS_BLOCK_union IO_STATUS_BLOCK_union;
    } std::sys::windows::c::IO_STATUS_BLOCK_union::variants;
};

struct std::sys::windows::c::IO_STATUS_BLOCK_union::IO_STATUS_BLOCK_union __packed
{
    int32_t .Status;
    int64_t .Pointer;
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.