Skip to content

Commit e2e4635

Browse files
youennfphiln
authored andcommitted
VP9 additional changes related to CVE-2023-5217
rdar://116293231 Reviewed by Jean-Yves Avenard. Cherry-picking patches that do hardening of VP9 encoder reconfiguration: - 02ab555e992c191e5c509ed87b3cc48ed915b447 - 263682c9a29395055f3b3afe2d97be1828a6223f I had to update CHECK_MEM_ERROR call site since we need to pass cm currently, while they do pass cm->error upstream. While we do not think we are exercising this code path of reconfiguring while encoding, it is future proof and low risk to cherry-pick these changes. * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/test/resize_test.cc: * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/common/vp9_alloccommon.c: (free_seg_map): (vp9_free_context_buffers): (vp9_alloc_context_buffers): * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/encoder/vp9_encoder.c: (free_copy_partition_data): (vp9_change_config): Originally-landed-as: 267815.170@safari-7617-branch (505f26eea3a5). rdar://117811019 Canonical link: https://commits.webkit.org/270154@main
1 parent e547ef8 commit e2e4635

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/test/resize_test.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ void ScaleForFrameNumber(unsigned int frame, unsigned int initial_w,
101101
*h = initial_h;
102102
return;
103103
}
104-
if (frame < 100) {
105-
*w = initial_w * 7 / 10;
106-
*h = initial_h * 16 / 10;
107-
return;
108-
}
104+
*w = initial_w * 7 / 10;
105+
*h = initial_h * 16 / 10;
109106
return;
110107
}
111108
if (frame < 10) {
@@ -578,7 +575,7 @@ TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
578575
}
579576
}
580577

581-
TEST_P(ResizeRealtimeTest, DISABLED_TestExternalResizeSmallerWidthBiggerSize) {
578+
TEST_P(ResizeRealtimeTest, TestExternalResizeSmallerWidthBiggerSize) {
582579
ResizingVideoSource video;
583580
video.flag_codec_ = true;
584581
video.smaller_width_larger_size_ = true;

Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/common/vp9_alloccommon.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static void free_seg_map(VP9_COMMON *cm) {
6565
vpx_free(cm->seg_map_array[i]);
6666
cm->seg_map_array[i] = NULL;
6767
}
68+
cm->seg_map_alloc_size = 0;
6869

6970
cm->current_frame_seg_map = NULL;
7071
cm->last_frame_seg_map = NULL;
@@ -106,6 +107,7 @@ void vp9_free_context_buffers(VP9_COMMON *cm) {
106107
cm->above_context = NULL;
107108
vpx_free(cm->above_seg_context);
108109
cm->above_seg_context = NULL;
110+
cm->above_context_alloc_cols = 0;
109111
vpx_free(cm->lf.lfm);
110112
cm->lf.lfm = NULL;
111113
}
@@ -131,13 +133,6 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
131133
cm->free_mi(cm);
132134
if (cm->alloc_mi(cm, new_mi_size)) goto fail;
133135
}
134-
135-
if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
136-
// Create the segmentation map structure and set to 0.
137-
free_seg_map(cm);
138-
if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
139-
}
140-
141136
if (cm->above_context_alloc_cols < cm->mi_cols) {
142137
vpx_free(cm->above_context);
143138
cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
@@ -152,6 +147,12 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
152147
cm->above_context_alloc_cols = cm->mi_cols;
153148
}
154149

150+
if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
151+
// Create the segmentation map structure and set to 0.
152+
free_seg_map(cm);
153+
if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
154+
}
155+
155156
if (vp9_alloc_loop_filter(cm)) goto fail;
156157

157158
return 0;

Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/encoder/vp9_encoder.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,17 @@ static void alloc_copy_partition_data(VP9_COMP *cpi) {
19701970
}
19711971
}
19721972

1973+
static void free_copy_partition_data(VP9_COMP *cpi) {
1974+
vpx_free(cpi->prev_partition);
1975+
cpi->prev_partition = NULL;
1976+
vpx_free(cpi->prev_segment_id);
1977+
cpi->prev_segment_id = NULL;
1978+
vpx_free(cpi->prev_variance_low);
1979+
cpi->prev_variance_low = NULL;
1980+
vpx_free(cpi->copied_frame_cnt);
1981+
cpi->copied_frame_cnt = NULL;
1982+
}
1983+
19731984
void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
19741985
VP9_COMMON *const cm = &cpi->common;
19751986
RATE_CONTROL *const rc = &cpi->rc;
@@ -2049,6 +2060,8 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
20492060
new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
20502061
if (cm->mi_alloc_size < new_mi_size) {
20512062
vp9_free_context_buffers(cm);
2063+
vp9_free_pc_tree(&cpi->td);
2064+
vpx_free(cpi->mbmi_ext_base);
20522065
alloc_compressor_data(cpi);
20532066
realloc_segmentation_maps(cpi);
20542067
cpi->initial_width = cpi->initial_height = 0;
@@ -2064,8 +2077,18 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
20642077
update_frame_size(cpi);
20652078

20662079
if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
2067-
memset(cpi->consec_zero_mv, 0,
2068-
cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
2080+
vpx_free(cpi->consec_zero_mv);
2081+
CHECK_MEM_ERROR(
2082+
cm, cpi->consec_zero_mv,
2083+
vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
2084+
2085+
vpx_free(cpi->skin_map);
2086+
CHECK_MEM_ERROR(
2087+
cm, cpi->skin_map,
2088+
vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));
2089+
2090+
free_copy_partition_data(cpi);
2091+
alloc_copy_partition_data(cpi);
20692092
if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
20702093
vp9_cyclic_refresh_reset_resize(cpi);
20712094
rc->rc_1_frame = 0;

0 commit comments

Comments
 (0)