-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Disallow Unsafe Declarations #229
base: master
Are you sure you want to change the base?
Conversation
Please add your real name and contact info to the author field. |
|
||
## Rationale | ||
|
||
Foreign code that is interfaced cannot be guaranteed to be safe. It should always be assumed as unsafe. Especially C declarations as the types used are not mangled into the name of the function. It would be possible to link to a C function with the incorrect parameter types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all extern C/C++ functions are foreign code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless there is a body it is foreign code. If you use C or C++ extern and access the function through a declaration instead of importing it, I don't think that should be a valid use case to consider. You can just import it instead or mark it as a D function and have a different wrapper for C/C++ if you need that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not true:
// foo.di
extern(C) foo(int i);
// foo.d
extern(C) foo(int i) {
// look ma, I have a body here
return i * 2;
|
||
## Rationale | ||
|
||
Foreign code that is interfaced cannot be guaranteed to be safe. It should always be assumed as unsafe. Especially C declarations as the types used are not mangled into the name of the function. It would be possible to link to a C function with the incorrect parameter types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should always be assumed as unsafe.
That's the thesis of the DIP, the rationale section should explain in detail why that assumption is useful.
It would be possible to link to a C function with the incorrect parameter types.
You can also link a D function with different signature by giving a false mangle.
Once the OS / build system / foreign code is compromised, there's nothing the programming language can do, so I would say it's best to assume those behave well, because if they don't, all bets are off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once the OS / build system / foreign code is compromised, there's nothing the programming language can do, so I would say it's best to assume those behave well, because if they don't, all bets are off.
I’d use the last part to come to the very opposite conclusion. @safe
means that the compiler checked it up to @trusted
calls. Ideally, this means functions with pragma(mangle,…)
should not be allowed to be marked @safe
because it simply may not be. If your implementation is @safe
, make a shallow wrapper like this:
pragma(mangle, "body")
extern(C) void body_func() @trusted => safe_body();
void safe_body() @safe { … }
It is a little lengthy, but it’s the shortest way which is honest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to think a function definition should always be allowed to be @safe
. Only a declaration without a body should be limited to @trusted
or @system
, if it has non-D ABI or custom mangling. Binary mangling of @trusted
and @safe
should be merged so they would link to each other even in D mangling.
It still does mean that a false @safe
D mangling could be given at the definition site without using @trusted
, but that's a small enough hole that i think it's better than requiring the safe_body
workaround. If we require the workaround, people are likely to just implement body_func
with a @trusted
body.
Forbidding |
I need your name and contact info, please. I don't understand why Walter's name is on it. Could you elaborate? |
Here's how you can break extern functions:
|
I don't think there is any good reason to prevent annotating an external function pragma(mangle, "abs") private extern(C) @system int abs_(int);
@trusted int abs(int arg){return abs_(arg);} This, in addition to the breakage, is much more verbose and ugly. I don't see what this accomplishes. Now, preventing |
No description provided.