Skip to content

ProtectMemory called with wrong address in public/CDetour/detourhelpers.h #984

@wrzwicky

Description

@wrzwicky

Help us help you

  • I have checked that my issue doesn't exist yet.
  • I have tried my absolute best to reduce the problem-space and have provided the absolute smallest test-case possible.
  • I can always reproduce the issue with the provided description below.

Environment

(not applicable)

Description

This line in detourhelpers.h:
ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
should be changed to this:
ProtectMemory((void *)addr, 20, PAGE_EXECUTE_READWRITE);
but this may be even more correct:
ProtectMemory((void *)addr, patch->bytes, PAGE_EXECUTE_READWRITE);

UPD: ProtectMemory also may have bug, see comment.

Explanation

A friend is coding own mods for his server, and found what appears to be a bug. The first proposed change above has stopped certain segfaults from occurring.

The second proposed change appears more correct to me from simple inspection of the code.

His explanation:

on mine I've changed it to this:
ProtectMemory((void *)addr, 20, PAGE_EXECUTE_READWRITE);
and the segfaults went away
I mean
it looks like Sourcemod is using it not for generic patches
but for detouring
so it hasn't been an issue for them
detours are installed at the start of functions
within the first 20 bytes
the problem only comes when you try to use that ApplyPatch function for general purpose patching in random locations
and it only protects the first 20 bytes of the function

Note that "on mine" refers to his private copy of detourhelpers.h. He's using ApplyPatch to patch functions, not just create detours, and finding that some calls to ApplyPatch cause segfaults. The proposed fix eliminates these segfaults.

Note that neither of us are familiar with how ProtectMemory is meant to be used, but this appears to be correct from inspection and the elimination of crashes.

Problematic Code (or Steps to Reproduce)

Use ApplyPatch() to change bytes "far" from the start of the function. I assume "far" means "any subsequent memory pages", but again I don't know how it works.

Metadata

Metadata

Assignees

Labels

Buggeneral bugs; can be anythingGood First IssueIssues that are suitable for first-time contributors.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions