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

FIX(positional-audio): Force 8 bytes alignment for CCameraAngles in GTAV plugin #5850

Conversation

davidebeatrici
Copy link
Member

@davidebeatrici davidebeatrici commented Sep 1, 2022

https://en.cppreference.com/w/cpp/language/alignas

This fixes compilation when the implicit alignment is not 8 bytes.

It can be the case with 32 bit targets.


Fixes #5849.

@davidebeatrici
Copy link
Member Author

davidebeatrici commented Sep 1, 2022

Turns out the #pragma is ignored in this specific case, you can test by passing -m32 to the compiler:

#include <cstdint>
#include <cstdio>

using ptr_t = uint64_t;

#pragma pack(push, 8)
struct CCameraAngles {
	uint8_t pad1[960];
	ptr_t playerAngles; // CPlayerAngles *
	uint8_t pad2[60];
};
#pragma pack(pop)

static_assert(sizeof(CCameraAngles) == 0x408, "");

int main() {
	printf("%zu\n", sizeof(CCameraAngles));

	return 0;
}
$ g++ -m32 test.cpp 
test.cpp:14:37: error: static assertion failed
   14 | static_assert(sizeof(CCameraAngles) == 0x408, "");
      |               ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
test.cpp:14:37: note: the comparison reduces to ‘(1028 == 1032)’

The standard alignas specifier appears to work though, which is very nice as it looks better and #pragma stuff should be generally avoided.

Unfortunately there's a limitation, which would be why I didn't use it in the first place:

If the strictest (largest) alignas on a declaration is weaker than the alignment it would have without any alignas specifiers (that is, weaker than its natural alignment or weaker than alignas on another declaration of the same object or type), the program is ill-formed

plugins/gtav/structs.h:90:8: error: requested alignment is less than minimum alignment of 8 for type 'CPed'
struct alignas(4) CPed : public rage::fwEntity {
       ^
plugins/gtav/structs.h:215:1: error: static_assert failed due to requirement 'sizeof(CPed) == 5428' ""
static_assert(sizeof(CPed) == 0x1534, "");
^             ~~~~~~~~~~~~~~~~~~~~~~

Kind of disappointing.

…TAV plugin

https://en.cppreference.com/w/cpp/language/alignas

This fixes compilation when the implicit alignment is not 8 bytes.

It can be the case with 32 bit targets.
@davidebeatrici davidebeatrici force-pushed the positional-audio-plugin-gtav-32-bit-fix branch from ab9de2e to 13c051b Compare September 1, 2022 21:33
@davidebeatrici davidebeatrici merged commit 7e95998 into mumble-voip:master Sep 2, 2022
@davidebeatrici davidebeatrici deleted the positional-audio-plugin-gtav-32-bit-fix branch September 2, 2022 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Does not build on i586 (GTAV plugin fails)
2 participants