From de235ea2f1acdd12da04924dce3adcc634afdf8b Mon Sep 17 00:00:00 2001 From: SniperJoe Date: Wed, 1 May 2024 13:38:47 +0300 Subject: [PATCH] Fix linux segfault (#2090) * fix too long message in messagebox * fix segfault due to concurrency --------- Co-authored-by: Nikita Morozov --- .gitignore | 4 ++ .../cpp/alvr_server/ChaperoneUpdater.cpp | 14 +++-- alvr/server/cpp/alvr_server/alvr_server.cpp | 18 ++++-- .../cpp/alvr_server/include/openvr_math.h | 58 +++++++++---------- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index f6ef4c3f64..4b66fc5315 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,7 @@ node_modules .flatpak* ALVR-Launcher + +#allows adding CMake files for some IDEs to parse C++ files correctly +CMakeLists.txt +cmake-build-debug/ \ No newline at end of file diff --git a/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp b/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp index 704f999a6c..2877ffc57f 100644 --- a/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp +++ b/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp @@ -2,6 +2,7 @@ #include "Logger.h" #include "bindings.h" #include +#include #ifndef __APPLE__ // Workaround symbol clash in openvr.h / openvr_driver.h @@ -84,14 +85,19 @@ void _SetChaperoneArea(float areaWidth, float areaHeight) { } #ifdef __linux__ -vr::HmdMatrix34_t GetInvZeroPose() { - vr::HmdMatrix34_t mat; +std::unique_ptr GetInvZeroPose() { + std::unique_lock lock(chaperone_mutex); + if (!isOpenvrInit) + { + return nullptr; + } + std::unique_ptr mat; // revert pulls live into working copy vr::VRChaperoneSetup()->RevertWorkingCopy(); if (vr::VRCompositor()->GetTrackingSpace() == vr::TrackingUniverseStanding) { - vr::VRChaperoneSetup()->GetWorkingStandingZeroPoseToRawTrackingPose(&mat); + vr::VRChaperoneSetup()->GetWorkingStandingZeroPoseToRawTrackingPose(mat.get()); } else { - vr::VRChaperoneSetup()->GetWorkingSeatedZeroPoseToRawTrackingPose(&mat); + vr::VRChaperoneSetup()->GetWorkingSeatedZeroPoseToRawTrackingPose(mat.get()); } return mat; } diff --git a/alvr/server/cpp/alvr_server/alvr_server.cpp b/alvr/server/cpp/alvr_server/alvr_server.cpp index 434abe46fa..05b556f039 100644 --- a/alvr/server/cpp/alvr_server/alvr_server.cpp +++ b/alvr/server/cpp/alvr_server/alvr_server.cpp @@ -25,10 +25,15 @@ #ifdef __linux__ #include "include/openvr_math.h" -vr::HmdMatrix34_t GetInvZeroPose(); +std::unique_ptr GetInvZeroPose(); -vr::HmdMatrix34_t GetRawZeroPose() { - return vrmath::matInv33(GetInvZeroPose()); +std::unique_ptr GetRawZeroPose() { + auto invZeroPose = GetInvZeroPose(); + if (invZeroPose == nullptr) + { + return nullptr; + } + return vrmath::matInv33(std::move(invZeroPose)); } bool IsOpenvrClientReady(); @@ -227,8 +232,11 @@ class DriverProvider : public vr::IServerTrackedDeviceProvider { || event.eventType == vr::VREvent_StandingZeroPoseReset || event.eventType == vr::VREvent_SceneApplicationChanged || event.eventType == VendorEvent_ALVRDriverResync) { - if (hmd && hmd->m_poseHistory && IsOpenvrClientReady()) { - hmd->m_poseHistory->SetTransform(GetRawZeroPose()); + if (hmd && hmd->m_poseHistory) { + auto rawZeroPose = GetRawZeroPose(); + if (rawZeroPose != nullptr) { + hmd->m_poseHistory->SetTransform(*rawZeroPose); + } } } #endif diff --git a/alvr/server/cpp/alvr_server/include/openvr_math.h b/alvr/server/cpp/alvr_server/include/openvr_math.h index 2a7726f3c1..1a6993d0bb 100644 --- a/alvr/server/cpp/alvr_server/include/openvr_math.h +++ b/alvr/server/cpp/alvr_server/include/openvr_math.h @@ -1,7 +1,7 @@ #pragma once #include - +#include inline vr::HmdQuaternion_t operator+(const vr::HmdQuaternion_t& lhs, const vr::HmdQuaternion_t& rhs) { return { @@ -298,42 +298,42 @@ namespace vrmath { return result; } - inline vr::HmdMatrix34_t matInv33(const vr::HmdMatrix34_t &matrix) { - vr::HmdMatrix34_t result; - float cofac00 = matrix.m[1][1] * matrix.m[2][2] - matrix.m[1][2] * matrix.m[2][1]; - float cofac10 = matrix.m[1][2] * matrix.m[2][0] - matrix.m[1][0] * matrix.m[2][2]; - float cofac20 = matrix.m[1][0] * matrix.m[2][1] - matrix.m[1][1] * matrix.m[2][0]; + inline std::unique_ptr matInv33(std::unique_ptr matrix) { + std::unique_ptr result; + float cofac00 = matrix->m[1][1] * matrix->m[2][2] - matrix->m[1][2] * matrix->m[2][1]; + float cofac10 = matrix->m[1][2] * matrix->m[2][0] - matrix->m[1][0] * matrix->m[2][2]; + float cofac20 = matrix->m[1][0] * matrix->m[2][1] - matrix->m[1][1] * matrix->m[2][0]; - float det = matrix.m[0][0] * cofac00 + matrix.m[0][1] * cofac10 + matrix.m[0][2] * cofac20; + float det = matrix->m[0][0] * cofac00 + matrix->m[0][1] * cofac10 + matrix->m[0][2] * cofac20; if (det == 0) { vr::HmdMatrix34_t result = { { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 } } }; - return result; + return std::make_unique(result); } float invDet = 1.0f / det; - float cofac01 = matrix.m[0][2] * matrix.m[2][1] - matrix.m[0][1] * matrix.m[2][2]; - float cofac02 = matrix.m[0][1] * matrix.m[1][2] - matrix.m[0][2] * matrix.m[1][1]; - float cofac11 = matrix.m[0][0] * matrix.m[2][2] - matrix.m[0][2] * matrix.m[2][0]; - float cofac12 = matrix.m[0][2] * matrix.m[1][0] - matrix.m[0][0] * matrix.m[1][2]; - float cofac21 = matrix.m[0][1] * matrix.m[2][0] - matrix.m[0][0] * matrix.m[2][1]; - float cofac22 = matrix.m[0][0] * matrix.m[1][1] - matrix.m[0][1] * matrix.m[1][0]; - - result.m[0][0] = invDet * cofac00; - result.m[0][1] = invDet * cofac01; - result.m[0][2] = invDet * cofac02; - result.m[0][3] = 0.0f; - - result.m[1][0] = invDet * cofac10; - result.m[1][1] = invDet * cofac11; - result.m[1][2] = invDet * cofac12; - result.m[1][3] = 0.0f; - - result.m[2][0] = invDet * cofac20; - result.m[2][1] = invDet * cofac21; - result.m[2][2] = invDet * cofac22; - result.m[2][3] = 0.0f; + float cofac01 = matrix->m[0][2] * matrix->m[2][1] - matrix->m[0][1] * matrix->m[2][2]; + float cofac02 = matrix->m[0][1] * matrix->m[1][2] - matrix->m[0][2] * matrix->m[1][1]; + float cofac11 = matrix->m[0][0] * matrix->m[2][2] - matrix->m[0][2] * matrix->m[2][0]; + float cofac12 = matrix->m[0][2] * matrix->m[1][0] - matrix->m[0][0] * matrix->m[1][2]; + float cofac21 = matrix->m[0][1] * matrix->m[2][0] - matrix->m[0][0] * matrix->m[2][1]; + float cofac22 = matrix->m[0][0] * matrix->m[1][1] - matrix->m[0][1] * matrix->m[1][0]; + + result->m[0][0] = invDet * cofac00; + result->m[0][1] = invDet * cofac01; + result->m[0][2] = invDet * cofac02; + result->m[0][3] = 0.0f; + + result->m[1][0] = invDet * cofac10; + result->m[1][1] = invDet * cofac11; + result->m[1][2] = invDet * cofac12; + result->m[1][3] = 0.0f; + + result->m[2][0] = invDet * cofac20; + result->m[2][1] = invDet * cofac21; + result->m[2][2] = invDet * cofac22; + result->m[2][3] = 0.0f; return result; }