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

Compare type sizes in druntime with ImportC #16571

Merged
merged 1 commit into from
Jul 8, 2024

Conversation

tim-dlang
Copy link
Contributor

Types for bindings of C libraries in druntime need to match the C headers. Small differences can result in hard to debug problems.

This test tries to automatically find types with a wrong size. This is done by also getting type sizes from C headers using ImportC and comparing them. Differences between the sizes can have different reasons:

  • Bugs in ImportC (e.g. for bitfields) can result in a wrong size
  • Type definitions in druntime can be wrong
  • Different preprocessor options could be used, like _FILE_OFFSET_BITS
  • Size differences can be fine, because some structs contain a member for the size or a version

Members of structs and unions with the same name are also compared. For types with potential problems a comparison of the layout is printed.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @tim-dlang! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16571"

@tim-dlang tim-dlang marked this pull request as draft June 9, 2024 14:18
@tim-dlang
Copy link
Contributor Author

Some issues found with this test:
ImportC: Bitfield layout wrong for int64 on 32-bit Linux (https://issues.dlang.org/show_bug.cgi?id=24592)
ImportC: Packed struct has wrong layout (https://issues.dlang.org/show_bug.cgi?id=24594)
Some structs in core.sys.posix.sys.types should be union instead of struct (https://issues.dlang.org/show_bug.cgi?id=24593)

@kinke
Copy link
Contributor

kinke commented Jun 11, 2024

Very cool! 👍

Types for bindings of C libraries in druntime need to match the
C headers. Small differences can result in hard to debug problems.

This test tries to automatically find types with a wrong size.
This is done by also getting type sizes from C headers using ImportC
and comparing them. Differences between the sizes can have different reasons:
* Bugs in ImportC (e.g. for bitfields) can result in a wrong size
* Type definitions in druntime can be wrong
* Different preprocessor options could be used, like _FILE_OFFSET_BITS
* Size differences can be fine, because some structs contain a member
    for the size or a version

Members of structs and unions with the same name are also compared.
For types with potential problems a comparison of the layout is printed.

The test contains a list of known problems, which are only treated as
warnings and not errors.
@tim-dlang
Copy link
Contributor Author

The test now contains a list of known problems, which are only treated as warnings and not errors. This would allow to merge the test before fixing the problems. New problems would still be found by the test.

@tim-dlang tim-dlang changed the title WIP: Compare type sizes in druntime with ImportC Compare type sizes in druntime with ImportC Jul 7, 2024
@tim-dlang tim-dlang marked this pull request as ready for review July 7, 2024 10:39
@thewilsonator thewilsonator merged commit 67996ab into dlang:master Jul 8, 2024
40 of 41 checks passed
@tim-dlang tim-dlang deleted the importc_compare branch July 8, 2024 18:21
thewilsonator pushed a commit to thewilsonator/dmd that referenced this pull request Oct 7, 2024
Types for bindings of C libraries in druntime need to match the
C headers. Small differences can result in hard to debug problems.

This test tries to automatically find types with a wrong size.
This is done by also getting type sizes from C headers using ImportC
and comparing them. Differences between the sizes can have different reasons:
* Bugs in ImportC (e.g. for bitfields) can result in a wrong size
* Type definitions in druntime can be wrong
* Different preprocessor options could be used, like _FILE_OFFSET_BITS
* Size differences can be fine, because some structs contain a member
    for the size or a version

Members of structs and unions with the same name are also compared.
For types with potential problems a comparison of the layout is printed.

The test contains a list of known problems, which are only treated as
warnings and not errors.
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.

4 participants