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

Library Forwarding: Test interaction of struct repacking and assume_compatible_data_layout #3374

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ThunkLibs/Generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int main(int argc, char* const argv[]) {
AdjustedArgs.push_back(std::string { "--target=" } + platform + "-linux-unknown");
AdjustedArgs.push_back("-isystem");
AdjustedArgs.push_back(std::string { "/usr/" } + platform + "-linux-gnu/include/");
AdjustedArgs.push_back("-DGUEST_THUNK_LIBRARY");
return AdjustedArgs;
};
GuestTool.appendArgumentsAdjuster(append_guest_args);
Expand Down
1 change: 0 additions & 1 deletion ThunkLibs/GuestLibs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function(generate NAME SOURCE_FILE)
# Interface target for the user to add include directories
add_library(${NAME}-guest-deps INTERFACE)
target_include_directories(${NAME}-guest-deps INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/../include")
target_compile_definitions(${NAME}-guest-deps INTERFACE GUEST_THUNK_LIBRARY)
if (BITNESS EQUAL 32)
target_compile_definitions(${NAME}-guest-deps INTERFACE IS_32BIT_THUNK)
endif ()
Expand Down
4 changes: 3 additions & 1 deletion ThunkLibs/libfex_thunk_test/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ struct ReorderingType {
};

ReorderingType MakeReorderingType(uint32_t a, uint32_t b);
uint32_t GetReorderingTypeMember(ReorderingType*, int index);
uint32_t GetReorderingTypeMember(const ReorderingType*, int index);
void ModifyReorderingTypeMembers(ReorderingType* data);
uint32_t QueryOffsetOf(ReorderingType*, int index);

// Uses assume_compatible_data_layout to skip repacking
uint32_t GetReorderingTypeMemberWithoutRepacking(const ReorderingType*, int index);

/// Interfaces used to test assisted struct repacking

Expand Down
6 changes: 5 additions & 1 deletion ThunkLibs/libfex_thunk_test/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ ReorderingType MakeReorderingType(uint32_t a, uint32_t b) {
return ReorderingType { .a = a, .b = b };
}

uint32_t GetReorderingTypeMember(ReorderingType* data, int index) {
uint32_t GetReorderingTypeMember(const ReorderingType* data, int index) {
if (index == 0) {
return data->a;
} else {
return data->b;
}
}

uint32_t GetReorderingTypeMemberWithoutRepacking(const ReorderingType* data, int index) {
return GetReorderingTypeMember(data, index);
}

void ModifyReorderingTypeMembers(ReorderingType* data) {
data->a += 1;
data->b += 2;
Expand Down
2 changes: 2 additions & 0 deletions ThunkLibs/libfex_thunk_test/libfex_thunk_test_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ template<> struct fex_gen_config<GetUnionTypeA> {};

template<> struct fex_gen_config<MakeReorderingType> {};
template<> struct fex_gen_config<GetReorderingTypeMember> {};
template<> struct fex_gen_config<GetReorderingTypeMemberWithoutRepacking> {};
template<> struct fex_gen_param<GetReorderingTypeMemberWithoutRepacking, 0, const ReorderingType*> : fexgen::assume_compatible_data_layout {};
template<> struct fex_gen_config<ModifyReorderingTypeMembers> {};

template<> struct fex_gen_config<QueryOffsetOf> : fexgen::custom_host_impl {};
Expand Down
7 changes: 7 additions & 0 deletions unittests/FEXLinuxTests/tests/thunks/thunk_testlib.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define GUEST_THUNK_LIBRARY

#include <dlfcn.h>

#include <stdexcept>
Expand Down Expand Up @@ -27,6 +29,7 @@ struct Fixture {

GET_SYMBOL(MakeReorderingType);
GET_SYMBOL(GetReorderingTypeMember);
GET_SYMBOL(GetReorderingTypeMemberWithoutRepacking);
GET_SYMBOL(ModifyReorderingTypeMembers);
GET_SYMBOL(QueryOffsetOf);

Expand Down Expand Up @@ -65,6 +68,10 @@ TEST_CASE_METHOD(Fixture, "Automatic struct repacking") {
CHECK(GetReorderingTypeMember(&test_struct, 0) == 0x1234);
CHECK(GetReorderingTypeMember(&test_struct, 1) == 0x5678);

// Test that we can force reinterpreting the data in guest layout as host layout
CHECK(GetReorderingTypeMemberWithoutRepacking(&test_struct, 0) == 0x5678);
CHECK(GetReorderingTypeMemberWithoutRepacking(&test_struct, 1) == 0x1234);

// Test repacking of output pointers
ModifyReorderingTypeMembers(&test_struct);
CHECK(GetReorderingTypeMember(&test_struct, 0) == 0x1235);
Expand Down
Loading