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

[Feature Request]: tooling to warn for 32 bit issues #26559

Open
jabraham17 opened this issue Jan 17, 2025 · 3 comments
Open

[Feature Request]: tooling to warn for 32 bit issues #26559

jabraham17 opened this issue Jan 17, 2025 · 3 comments

Comments

@jabraham17
Copy link
Member

jabraham17 commented Jan 17, 2025

I have had this same issue multiple times in the past week. The primary problem I have been having is calling extern C code with a Chapel type.

For example, the following code is perfectly fine on a 64 bit system because c_long is defined to be an int(64).

use CTypes;
//void myFunc(long x);
extern proc myFunc(x: c_long);

var x = 10;
myFunc(x);

But on a 32 bit system, c_long is a int(32), and so this code fails to compile.

It would be nice to have some kind of tooling (either a compiler warning or a lint warning) that you are trying to pass a Chapel int to a system dependent sized C int. I could imagine something that checks if a Chapel type is being passed to a type that resided in ChapelSysCTypes.

@bradcray
Copy link
Member

What is the C prototype for myFunc()? (I'm guessing "simply long")? Do you control the C interface, or is it something from a library?

@jabraham17
Copy link
Member Author

In my above example, yes the C prototype of myFunc would be long. I've updated the OP to make that clearer. The bug here is not in the C code, but rather its myFunc(x), because x is always a 64 bit integer.

I have hit this several times using the Python C API, which I have no control over.

@bradcray
Copy link
Member

I agree it'd be nice to have tooling help with this. I'd argue that there isn't really a "bug" here, but that it's part of the ongoing pains that C signed themselves up for by not making their base types have well-defined size (exacerbated by people using them in public interfaces rather than the cstdint types instead). On past supercomputer systems, we used to have the opposite problem where the C compilers would define int to be 64-bits, and lots of existing code would assume it was 32 bits and break as a result.

The safe way to use such functions in Chapel would either be to declare x to be of type c_long as well or to always cast x to c_long when passing it to myFunc, either of which should make the code fully portable. That said, I realize that these are not very ergonomic, so am not suggesting that's what you (or an end-user) would necessarily would want to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants