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

i#3544 RV64: Rebase the dcontext pointer. #7235

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

mariospaok4
Copy link
Contributor

The dcontext_t struct is larger than the biggest valid displacement of the load and store instructions.
By rebasing the pointer by 0x800, we can access the entire struct, because the displacement can be in the range of -0x800 to 0x7ff.

The dcontext_t struct is larger than the biggest valid displacement of the load and store instructions.
By rebasing the pointer by 0x800, we van access the entire struct,
because the dispacement can be in the renge of -0x800 to 0x7ff.
@mariospaok4 mariospaok4 marked this pull request as draft January 30, 2025 09:19
@mariospaok4
Copy link
Contributor Author

mariospaok4 commented Feb 7, 2025

I know that these patches are quite "hacky". However, I could not find a better way to work around some issues.
Currently we use only arithmetic, we can use a separate TLS slot for the displaced dcontext, if it is deemed worthwhile.
That being said, only riscv is affected by the patch and it fixes dr_insert_read_tls_field.
We can add sample client that demonstrates this.
It also seems that no previously passing tests fail.

@mariospaok4 mariospaok4 marked this pull request as ready for review February 7, 2025 10:50
* substracting 0x800 from the offset in the struct.
*/
#ifdef RISCV64
# define CONTEXT_REBASE_OFFT 0x800
Copy link
Member

Choose a reason for hiding this comment

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

Suggest a better name such as CONTEXT_HEAD_OFFSET.

I wonder whether a negative offset will be more descriptive than using subtraction everywhere, which gives a hint about the memory layout (REG_DCTX points to the middle of dcontext_t, yielding a negative offset).

* and it does not run windows, we can work around that with conditional compilation.
*/
#if (CONTEXT_REBASE_OFFT != 0)
(*entry)(((void *)dcontext) + CONTEXT_REBASE_OFFT);
Copy link
Member

Choose a reason for hiding this comment

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

Conversion between REG_DCTX and the real head of dcontext_t occurs several times. Suggest wrap these into macros. (CONTEXT_PTR_TO_HEAD() and CONTEXT_HEAD_TO_PTR).

Comment on lines +2382 to +2384
#if (CONTEXT_REBASE_OFFT != 0)
set_thread_private_dcontext(NULL);
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Existing code compares thread private dcontext with NULL for validation check, thus this is required or they will get NULL - CONTEXT_REBASE_OFFSET and wrongly assumes thread private dcontext is ready.

Do I understand it correcly? If so, some comments to explain this call seem helpful.

@ziyao233 ziyao233 requested a review from ksco February 8, 2025 08:46
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.

2 participants