Skip to content

Commit 87f21b1

Browse files
Fix stack overflow when calling beforeSend during object post-loading on mobile (#782)
* Fix calling `beforeSend` during object post-loading on mobile * Update changelog * Refactor post-loading check * Invert post-loading check and add corresponding logging --------- Co-authored-by: Stefan Jandl <[email protected]>
1 parent 5592398 commit 87f21b1

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
- Fix incorrect game log attachment on Android ([#743](https://github.com/getsentry/sentry-unreal/pull/743))
2323
- Fix assertion during screenshot capturing in a thread that can't use Slate ([#756](https://github.com/getsentry/sentry-unreal/pull/756))
24+
- Fix stack overflow when calling `beforeSend` during object post-loading on mobile ([#782](https://github.com/getsentry/sentry-unreal/pull/782))
2425
- Fix invalid syntax in symbol upload batch script ([#801](https://github.com/getsentry/sentry-unreal/pull/801))
2526

2627
### Dependencies

plugin-dev/Source/Sentry/Private/Android/Jni/SentryJniAndroid.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#include "Android/SentrySamplingContextAndroid.h"
1111

1212
#include "Android/AndroidJNI.h"
13-
#include "UObject/GarbageCollection.h"
1413

14+
#include "SentryDefines.h"
1515
#include "SentryEvent.h"
1616
#include "SentryHint.h"
1717
#include "SentryBeforeSendHandler.h"
1818
#include "SentryTraceSampler.h"
1919
#include "SentrySamplingContext.h"
20+
#include "UObject/GarbageCollection.h"
21+
#include "UObject/UObjectThreadContext.h"
2022

2123
JNI_METHOD void Java_io_sentry_unreal_SentryBridgeJava_onConfigureScope(JNIEnv* env, jclass clazz, jlong callbackId, jobject scope)
2224
{
@@ -33,12 +35,20 @@ JNI_METHOD jobject Java_io_sentry_unreal_SentryBridgeJava_onBeforeSend(JNIEnv* e
3335
{
3436
FGCScopeGuard GCScopeGuard;
3537

38+
if (FUObjectThreadContext::Get().IsRoutingPostLoad)
39+
{
40+
UE_LOG(LogSentrySdk, Log, TEXT("Executing `beforeSend` handler is not allowed when post-loading."));
41+
return event;
42+
}
43+
3644
USentryBeforeSendHandler* handler = reinterpret_cast<USentryBeforeSendHandler*>(objAddr);
3745

3846
USentryEvent* EventToProcess = USentryEvent::Create(MakeShareable(new SentryEventAndroid(event)));
3947
USentryHint* HintToProcess = USentryHint::Create(MakeShareable(new SentryHintAndroid(hint)));
4048

41-
return handler->HandleBeforeSend(EventToProcess, HintToProcess) ? event : nullptr;
49+
USentryEvent* ProcessedEvent = handler->HandleBeforeSend(EventToProcess, HintToProcess);
50+
51+
return ProcessedEvent ? event : nullptr;
4252
}
4353

4454
JNI_METHOD jfloat Java_io_sentry_unreal_SentryBridgeJava_onTracesSampler(JNIEnv* env, jclass clazz, jlong objAddr, jobject samplingContext)

plugin-dev/Source/Sentry/Private/Apple/AppleSentrySubsystem.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "GenericPlatform/GenericPlatformOutputDevices.h"
2828
#include "HAL/FileManager.h"
2929
#include "UObject/GarbageCollection.h"
30+
#include "UObject/UObjectThreadContext.h"
3031
#include "Utils/SentryLogUtils.h"
3132

3233
void FAppleSentrySubsystem::InitWithSettings(const USentrySettings* settings, USentryBeforeSendHandler* beforeSendHandler, USentryTraceSampler* traceSampler)
@@ -62,8 +63,18 @@ void FAppleSentrySubsystem::InitWithSettings(const USentrySettings* settings, US
6263
};
6364
options.beforeSend = ^SentryEvent* (SentryEvent* event) {
6465
FGCScopeGuard GCScopeGuard;
66+
67+
if (FUObjectThreadContext::Get().IsRoutingPostLoad)
68+
{
69+
UE_LOG(LogSentrySdk, Log, TEXT("Executing `beforeSend` handler is not allowed when post-loading."));
70+
return event;
71+
}
72+
6573
USentryEvent* EventToProcess = USentryEvent::Create(MakeShareable(new SentryEventApple(event)));
66-
return beforeSendHandler->HandleBeforeSend(EventToProcess, nullptr) ? event : nullptr;
74+
75+
USentryEvent* ProcessedEvent = beforeSendHandler->HandleBeforeSend(EventToProcess, nullptr);
76+
77+
return ProcessedEvent ? event : nullptr;
6778
};
6879
for (auto it = settings->InAppInclude.CreateConstIterator(); it; ++it)
6980
{

plugin-dev/Source/Sentry/Private/GenericPlatform/GenericPlatformSentrySubsystem.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,16 @@ sentry_value_t FGenericPlatformSentrySubsystem::OnBeforeSend(sentry_value_t even
102102

103103
FGCScopeGuard GCScopeGuard;
104104

105-
USentryEvent* EventToProcess = USentryEvent::Create(Event);
106-
107-
USentryEvent* ProcessedEvent = EventToProcess;
108-
if (!FUObjectThreadContext::Get().IsRoutingPostLoad)
105+
if (FUObjectThreadContext::Get().IsRoutingPostLoad)
109106
{
110-
// Executing UFUNCTION is allowed only when not post-loading
111-
ProcessedEvent = GetBeforeSendHandler()->HandleBeforeSend(EventToProcess, nullptr);
107+
UE_LOG(LogSentrySdk, Log, TEXT("Executing `beforeSend` handler is not allowed when post-loading."));
108+
return event;
112109
}
113110

111+
USentryEvent* EventToProcess = USentryEvent::Create(Event);
112+
113+
USentryEvent* ProcessedEvent = GetBeforeSendHandler()->HandleBeforeSend(EventToProcess, nullptr);
114+
114115
return ProcessedEvent ? event : sentry_value_new_null();
115116
}
116117

0 commit comments

Comments
 (0)