Fix the issue that local reference overflows in Yoga 1 (#1308)

Summary:
X-link: https://github.com/facebook/react-native/pull/37929

X-link: https://github.com/facebook/litho/pull/952

Pull Request resolved: https://github.com/facebook/yoga/pull/1308

Long story in short, we're trying to fix an issue with Yoga that could potentially lead to an overflow in the JNI local reference table.

Reviewed By: NickGerleman, astreet

Differential Revision: D46653732

fbshipit-source-id: 0bc34bd5a819037c046c62b651e414b249cbdcb8
This commit is contained in:
Andrew Wang
2023-06-16 06:15:07 -07:00
committed by Facebook GitHub Bot
parent ca4cf852aa
commit f3e9b6bfb0
6 changed files with 24 additions and 5 deletions

View File

@@ -54,6 +54,8 @@ ENUMS = {
"AbsolutePercentageAgainstPaddingEdge",
# Conformance fix: https://github.com/facebook/yoga/pull/1028
"FixAbsoluteTrailingColumnMargin",
# fix JNI local ref overflows
"FixJNILocalRefOverflows",
],
"PrintOptions": [
("Layout", 1 << 0),

View File

@@ -12,7 +12,8 @@ package com.facebook.yoga;
public enum YogaExperimentalFeature {
WEB_FLEX_BASIS(0),
ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE(1),
FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN(2);
FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN(2),
FIX_JNILOCAL_REF_OVERFLOWS(3);
private final int mIntValue;
@@ -29,6 +30,7 @@ public enum YogaExperimentalFeature {
case 0: return WEB_FLEX_BASIS;
case 1: return ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE;
case 2: return FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN;
case 3: return FIX_JNILOCAL_REF_OVERFLOWS;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

View File

@@ -280,7 +280,8 @@ static void YGTransferLayoutOutputsRecursive(
JNIEnv* env,
jobject thiz,
YGNodeRef root,
void* layoutContext) {
void* layoutContext,
bool shouldCleanLocalRef) {
if (!YGNodeGetHasNewLayout(root)) {
return;
}
@@ -346,11 +347,16 @@ static void YGTransferLayoutOutputsRecursive(
env->SetFloatArrayRegion(arrFinal.get(), 0, arrSize, arr);
env->SetObjectField(obj.get(), arrField, arrFinal.get());
if (shouldCleanLocalRef) {
objectClass.reset();
arrFinal.reset();
}
YGNodeSetHasNewLayout(root, false);
for (uint32_t i = 0; i < YGNodeGetChildCount(root); i++) {
YGTransferLayoutOutputsRecursive(
env, thiz, YGNodeGetChild(root, i), layoutContext);
env, thiz, YGNodeGetChild(root, i), layoutContext, shouldCleanLocalRef);
}
}
@@ -372,13 +378,17 @@ static void jni_YGNodeCalculateLayoutJNI(
}
const YGNodeRef root = _jlong2YGNodeRef(nativePointer);
const bool shouldCleanLocalRef =
root->getConfig()->isExperimentalFeatureEnabled(
YGExperimentalFeatureFixJNILocalRefOverflows);
YGNodeCalculateLayoutWithContext(
root,
static_cast<float>(width),
static_cast<float>(height),
YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)),
layoutContext);
YGTransferLayoutOutputsRecursive(env, obj, root, layoutContext);
YGTransferLayoutOutputsRecursive(
env, obj, root, layoutContext, shouldCleanLocalRef);
} catch (const YogaJniException& jniException) {
ScopedLocalRef<jthrowable> throwable = jniException.getThrowable();
if (throwable.get()) {

View File

@@ -57,6 +57,7 @@ export enum ExperimentalFeature {
WebFlexBasis = 0,
AbsolutePercentageAgainstPaddingEdge = 1,
FixAbsoluteTrailingColumnMargin = 2,
FixJNILocalRefOverflows = 3,
}
export enum FlexDirection {
@@ -164,6 +165,7 @@ const constants = {
EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: ExperimentalFeature.WebFlexBasis,
EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE: ExperimentalFeature.AbsolutePercentageAgainstPaddingEdge,
EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN: ExperimentalFeature.FixAbsoluteTrailingColumnMargin,
EXPERIMENTAL_FEATURE_FIX_JNILOCAL_REF_OVERFLOWS: ExperimentalFeature.FixJNILocalRefOverflows,
FLEX_DIRECTION_COLUMN: FlexDirection.Column,
FLEX_DIRECTION_COLUMN_REVERSE: FlexDirection.ColumnReverse,
FLEX_DIRECTION_ROW: FlexDirection.Row,

View File

@@ -109,6 +109,8 @@ const char* YGExperimentalFeatureToString(const YGExperimentalFeature value) {
return "absolute-percentage-against-padding-edge";
case YGExperimentalFeatureFixAbsoluteTrailingColumnMargin:
return "fix-absolute-trailing-column-margin";
case YGExperimentalFeatureFixJNILocalRefOverflows:
return "fix-jnilocal-ref-overflows";
}
return "unknown";
}

View File

@@ -66,7 +66,8 @@ YG_ENUM_SEQ_DECL(
YGExperimentalFeature,
YGExperimentalFeatureWebFlexBasis,
YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge,
YGExperimentalFeatureFixAbsoluteTrailingColumnMargin)
YGExperimentalFeatureFixAbsoluteTrailingColumnMargin,
YGExperimentalFeatureFixJNILocalRefOverflows)
YG_ENUM_SEQ_DECL(
YGFlexDirection,