Skip to content

Commit

Permalink
Fix TrackCache memory crash
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi authored and TokageItLab committed Nov 23, 2023
1 parent bb63963 commit 21ea636
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
79 changes: 79 additions & 0 deletions scene/animation/animation_mixer.cpp
Expand Up @@ -2138,3 +2138,82 @@ AnimationMixer::AnimationMixer() {

AnimationMixer::~AnimationMixer() {
}

void AnimatedValuesBackup::set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data) {
clear_data();

for (const KeyValue<NodePath, AnimationMixer::TrackCache *> &E : p_data) {
data.insert(E.key, get_cache_copy(E.value));
}
}

HashMap<NodePath, AnimationMixer::TrackCache *> AnimatedValuesBackup::get_data() const {
HashMap<NodePath, AnimationMixer::TrackCache *> ret;
for (const KeyValue<NodePath, AnimationMixer::TrackCache *> &E : data) {
ret.insert(E.key, get_cache_copy(E.value));
}
return ret;
}

void AnimatedValuesBackup::clear_data() {
for (KeyValue<NodePath, AnimationMixer::TrackCache *> &K : data) {
memdelete(K.value);
}
data.clear();
}

AnimationMixer::TrackCache *AnimatedValuesBackup::get_cache_copy(AnimationMixer::TrackCache *p_cache) const {
switch (p_cache->type) {
case Animation::TYPE_VALUE: {
AnimationMixer::TrackCacheValue *src = static_cast<AnimationMixer::TrackCacheValue *>(p_cache);
AnimationMixer::TrackCacheValue *tc = memnew(AnimationMixer::TrackCacheValue);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheValue));

Check failure on line 2170 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheValue'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2170 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheValue'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_POSITION_3D:
case Animation::TYPE_ROTATION_3D:
case Animation::TYPE_SCALE_3D: {
AnimationMixer::TrackCacheTransform *src = static_cast<AnimationMixer::TrackCacheTransform *>(p_cache);
AnimationMixer::TrackCacheTransform *tc = memnew(AnimationMixer::TrackCacheTransform);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheTransform));

Check failure on line 2179 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheTransform'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2179 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheTransform'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_BLEND_SHAPE: {
AnimationMixer::TrackCacheBlendShape *src = static_cast<AnimationMixer::TrackCacheBlendShape *>(p_cache);
AnimationMixer::TrackCacheBlendShape *tc = memnew(AnimationMixer::TrackCacheBlendShape);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheBlendShape));

Check failure on line 2186 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheBlendShape'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2186 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheBlendShape'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_METHOD: {
AnimationMixer::TrackCacheMethod *src = static_cast<AnimationMixer::TrackCacheMethod *>(p_cache);
AnimationMixer::TrackCacheMethod *tc = memnew(AnimationMixer::TrackCacheMethod);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheMethod));

Check failure on line 2193 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheMethod'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2193 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheMethod'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_BEZIER: {
AnimationMixer::TrackCacheBezier *src = static_cast<AnimationMixer::TrackCacheBezier *>(p_cache);
AnimationMixer::TrackCacheBezier *tc = memnew(AnimationMixer::TrackCacheBezier);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheBezier));

Check failure on line 2200 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheBezier'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2200 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheBezier'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_AUDIO: {
AnimationMixer::TrackCacheAudio *src = static_cast<AnimationMixer::TrackCacheAudio *>(p_cache);
AnimationMixer::TrackCacheAudio *tc = memnew(AnimationMixer::TrackCacheAudio);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheAudio));

Check failure on line 2207 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheAudio'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2207 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheAudio'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}

case Animation::TYPE_ANIMATION: {
AnimationMixer::TrackCacheAnimation *src = static_cast<AnimationMixer::TrackCacheAnimation *>(p_cache);
AnimationMixer::TrackCacheAnimation *tc = memnew(AnimationMixer::TrackCacheAnimation);
memcpy((void *)tc, src, sizeof(AnimationMixer::TrackCacheAnimation));

Check failure on line 2214 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheAnimation'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]

Check failure on line 2214 in scene/animation/animation_mixer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

source of this 'memcpy' call is a pointer to dynamic class 'TrackCacheAnimation'; vtable pointer will be copied [-Werror,-Wdynamic-class-memaccess]
return tc;
}
}
return nullptr;
}
13 changes: 6 additions & 7 deletions scene/animation/animation_mixer.h
Expand Up @@ -388,14 +388,13 @@ class AnimatedValuesBackup : public RefCounted {
HashMap<NodePath, AnimationMixer::TrackCache *> data;

public:
void set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data) { data = p_data; };
HashMap<NodePath, AnimationMixer::TrackCache *> get_data() const { return data; };
void set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data);
HashMap<NodePath, AnimationMixer::TrackCache *> get_data() const;
void clear_data();

~AnimatedValuesBackup() {
for (KeyValue<NodePath, AnimationMixer::TrackCache *> &K : data) {
memdelete(K.value);
}
}
AnimationMixer::TrackCache *get_cache_copy(AnimationMixer::TrackCache *p_cache) const;

~AnimatedValuesBackup() { clear_data(); }
};

VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeProcess);
Expand Down

0 comments on commit 21ea636

Please sign in to comment.