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/fix wd start casts #741

Merged
merged 5 commits into from
Apr 7, 2020

Conversation

YuuichiNakamura
Copy link
Contributor

This is a PR of issue #740.

@xiaoxiang781216 xiaoxiang781216 linked an issue Apr 6, 2020 that may be closed by this pull request
@yamt
Copy link
Contributor

yamt commented Apr 6, 2020

i'm not sure if this is an improvement.
wd_start/wd_expiration still treat these arguments as of wdparm_t.

@YuuichiNakamura
Copy link
Contributor Author

It is related to the calling convention of some processors.

In many RISC processors, some arguments of the function call are passed by the CPU registers and the remaining arguments by the stack. It is same in both fixed and variadic functions.

But, some processors such as AVR have the different calling convention for the variadic function.
It passes all arguments by the stack, not using the registers.
So if the pointer to the fixed argument function casts to the pointer to the variadic function, the argument cannot be passed correctly.
This PR removes such casts.

@yamt
Copy link
Contributor

yamt commented Apr 6, 2020

does it conform to C? how can functions without prototypes work?

can you give me a pointer to the AVR calling convention doc? just curious.

are you going to fix all such casts in the tree?

@YuuichiNakamura
Copy link
Contributor Author

https://onlinedocs.microchip.com/pr/GUID-317042D4-BCCE-4065-BB05-AC4312DBC2C4-en-US-1/index.html?GUID-D7A8D3E7-DF5F-4CD6-8764-B5039BC9E5FE

"13.What registers are used by the C compiler?" describes the AVR calling convention and the document describes that:

Arguments to functions with variable argument lists (printf etc.) are all passed on stack,

I have confirmed by avr-gcc that the description is correct.

As I wrote in issue #740, this PR only fixes architecture independent codes.
There are many type casting in arch/, boards/, and drivers/. But because almost casts are in the code only for arm processor, they doesn't occur the problem.

@YuuichiNakamura
Copy link
Contributor Author

Originally the problem is pointed out by Sebastian Perta in:
http:https://mail-archives.apache.org/mod_mbox/nuttx-dev/202003.mbox/%3CTYAPR01MB2797027AA22C5187BD5EEAAAE0F40%40TYAPR01MB2797.jpnprd01.prod.outlook.com%3E

I don't have a information about Renesas RX ABI, but I think that it have similar calling conventions.

@xiaoxiang781216
Copy link
Contributor

@YuuichiNakamura, how about we remove the all cast? Many people add the new feature by coping the similar code. The bad usage will spread again if we don't completely remove them.

* specifically to this thread that is known to be waiting on the signal.
*/
va_start(ap, arg1);
signo = (uint32_t)va_arg(ap, uint32_t);
Copy link
Contributor

@patacongo patacongo Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual type is wdparm_t. It will be a 64-bit uintpr_t on 64-bit machines, and 32-bits on others. I don't think you can guarantee that uint32_t will always be correct. Depending on endian-ness, this could grab the wrong 32-bits of the 32-bit value.

My guess is that this will work on the 64-bit simulation, but only because it is little endian and you will probably get the least significant 32-bits which hold the correct signal number.

The existing code has a similar issue since the parameter was received as a uint32_t but does work fine on a 64-bit simulator. I think that was only lucky.

Copy link
Contributor

@patacongo patacongo Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this make more sense:

int signo;
...
signo = (int)va_arg(ap, wdparm_t);

Since the width of the argument is wdparm_t, not necessarily 32-bits, and because the signal number started out as an integer to begin with.

Copy link
Contributor

@patacongo patacongo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the change looks good. I would like you to consider the change that I suggested in the comments:

Actual type is wdparm_t. It will be a 64-bit uintpr_t on 64-bit machines, and 32-bits on others. I don't think you can guarantee that uint32_t will always be correct. Depending on endian-ness, this could grab the wrong 32-bits of the 32-bit value. Wouldn't this make more sense:

int signo;
...
signo = (int)va_arg(ap, wdparm_t);

Since the width of the argument is wdparm_t, not necessarily 32-bits, and because the signal number started out as an integer to begin with.

@YuuichiNakamura
Copy link
Contributor Author

@xiaoxiang781216 I got it. I'll add the commits to remove all casts.

@YuuichiNakamura
Copy link
Contributor Author

@patacongo
I think that it also work fine even on 64-bit big endian processor because the argument smaller than 64-bit is promoted to 64-bit in argument passing.

But, in this case, wdparm_t should be used as your suggestion because wd_start() treats the all arguments as wdparm_t. I'll fix it.

@xiaoxiang781216
Copy link
Contributor

@YuuichiNakamura could you merge "Fix types of the arguments for wd_start() callback" into "Remove type casting in pthread_condtimedwait()"? You can try:
1.git rebase --interactive
2.git push -f
And add new patch to fix nxstyle issue as much as possible.

@YuuichiNakamura
Copy link
Contributor Author

@xiaoxiang781216 Thanks. I'll arrange the commits and force push again.
Now I'm fixing the styles...

@xiaoxiang781216
Copy link
Contributor

LGTM.

@jerpelea
Copy link
Contributor

jerpelea commented Apr 7, 2020

LGTM

@patacongo patacongo merged commit 9029e4d into apache:master Apr 7, 2020
@YuuichiNakamura YuuichiNakamura deleted the feature/fix-wd_start-casts branch April 8, 2020 01:26
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.

problem with nxsig_timeout (sig_timedwait.c)
5 participants