Skip to content

Fix user data alignment in MEMBLOCK#1471

Merged
aquynh merged 2 commits intocapstone-engine:masterfrom
mbikovitsky:patch-1
May 9, 2019
Merged

Fix user data alignment in MEMBLOCK#1471
aquynh merged 2 commits intocapstone-engine:masterfrom
mbikovitsky:patch-1

Conversation

@mbikovitsky
Copy link
Copy Markdown
Contributor

@mbikovitsky mbikovitsky commented May 5, 2019

Kernel memory allocations on Windows should be aligned on MEMORY_ALLOCATION_ALIGNMENT (16 bytes on x64 and 8 bytes on x86).

Kernel memory allocations on Windows should be aligned on MEMORY_ALLOCATION_ALIGNMENT (16 bytes on x64 and 8 bytes on x86).
@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 5, 2019

cool! with this patch, Windows kernel driver works well now?

@mbikovitsky
Copy link
Copy Markdown
Contributor Author

It depends.

For one, the current implementation returns 4-byte aligned memory on x86 (due to the size_t field just before the data array), which means that allocations that require larger alignment (for instance, doubles) are silently broken. On x86 and x64 it is usually not a problem, since these processors are rather forgiving of alignment issues (and according to this, ARM64 is fine as well, though the article refers to user-mode code only). Still, code that uses this memory assumes it is correctly aligned, and may crash if the compiler generated some alignment-sensitive instructions.

On Windows, the kernel memory allocation APIs all return memory that is aligned at least on MEMORY_ALLOCATION_ALIGNMENT, and so it is only logical to do the same. There are actually some data structures that require the 16-byte alignment.

The point is that while the current implementation probably works fine, it is still worth fixing. I don't know whether the driver will work well now, but it will certainly work better :)

@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 5, 2019

@tandasat, can you please ack?

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 6, 2019

I’m traveling now. I’ll check it by the end of this weekend.

@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 7, 2019

@tandasat, at the same time, please could you confirm that in the "v4" branch, your Windows driver still compiles without any issues?

thanks!

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 7, 2019

Will do. If you have the issue for this already, let me know so I can quickly catch up problems if any.

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 8, 2019

@mbikovitsky @aquynh

I notice there are two implementation of windows driver support:
capstone\contrib\windows_kernel, and
capstone\windows

Is the first one usable? When I merged the second one, it did not even compile (and so I had to create the 2nd one). I ask this because the patch is for the first one, and I am thinking of deprecating or deleting it and consolidate Windows kernel support implementation into one. Thoughts?

Note that the patch itself looks reasonable. except that it should be done in capstone\windows\winkernel_mm.c (this is why we want to deprecate one).

@mbikovitsky
Copy link
Copy Markdown
Contributor Author

Now that I look more closely at it, the version in contrib uses std::unique_ptr which is not available for Windows drivers (as is the rest of the C++ standard library). Apart from that, both implementations are very similar with regards to memory allocations. The only major difference I see is in the vsnprintf implementation.

I pushed a commit with the same change in winkernel_mm.c.

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 8, 2019

Thank you @mbikovitsky. The changes look good to me.

@aquynh not to block a merge of this PR but want to hear your thoughts on deleting capstone\contrib\windows_kernel from the v4 branch.

@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 8, 2019

the one under contrib\ is from @zer0mem, but i am not sure he still wants to maintain it.

lets go ahead with this for now.

@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 8, 2019

so i will just merge this?

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 9, 2019

Yes, let’s merge this.

@aquynh aquynh merged commit e05af7a into capstone-engine:master May 9, 2019
aquynh pushed a commit that referenced this pull request May 9, 2019
* Fix user data alignment in MEMBLOCK

Kernel memory allocations on Windows should be aligned on MEMORY_ALLOCATION_ALIGNMENT (16 bytes on x64 and 8 bytes on x86).

* Fix user data alignment in CS_WINKERNEL_MEMBLOCK
aquynh pushed a commit that referenced this pull request May 9, 2019
* Fix user data alignment in MEMBLOCK

Kernel memory allocations on Windows should be aligned on MEMORY_ALLOCATION_ALIGNMENT (16 bytes on x64 and 8 bytes on x86).

* Fix user data alignment in CS_WINKERNEL_MEMBLOCK
@aquynh
Copy link
Copy Markdown
Collaborator

aquynh commented May 9, 2019

merged for all branches "master", "v4" & "next", thanks!

@tandasat so you confirmed that all builds without any issues on "v4" branch?

(i want to release v4.0.2 based on "v4" branch next week)

@tandasat
Copy link
Copy Markdown
Contributor

tandasat commented May 9, 2019

I have not been able to check that yet. I’ll give you an update by the end of this weekend.

@tandasat
Copy link
Copy Markdown
Contributor

I am working on windows-kernel support for v4 in this issue: #1474

@riptl riptl mentioned this pull request Jul 22, 2022
6 tasks
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.

3 participants