[macos] [metal] Fix use after free in FlutterExternalTextureMetal (flutter/engine#24995)

* [macos] [metal] Fix use after free in FlutterExternalTextureMetal

This also adds a test for the said failure

Fixes: https://github.com/flutter/flutter/issues/78056

* fix review comments
This commit is contained in:
Kaushik Iska
2021-03-16 15:55:28 -05:00
committed by GitHub
parent 4758f75bc9
commit 40c0b51d5e
3 changed files with 89 additions and 9 deletions

View File

@@ -10,6 +10,7 @@
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h"
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/embedder_external_texture_metal.h"
#import "flutter/testing/testing.h"
@@ -18,6 +19,44 @@
#include "third_party/skia/include/core/SkSamplingOptions.h"
#include "third_party/skia/include/core/SkSurface.h"
@interface TestExternalTexture : NSObject <FlutterTexture>
- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height;
@end
@implementation TestExternalTexture {
size_t _width;
size_t _height;
}
- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height {
if (self = [super init]) {
_width = width;
_height = height;
}
return self;
}
- (CVPixelBufferRef)copyPixelBuffer {
return [self pixelBuffer];
}
- (CVPixelBufferRef)pixelBuffer {
NSDictionary* options = @{
// This key is required to generate SKPicture with CVPixelBufferRef in metal.
(NSString*)kCVPixelBufferMetalCompatibilityKey : @YES
};
CVPixelBufferRef pxbuffer = NULL;
CVReturn status =
CVPixelBufferCreate(kCFAllocatorDefault, _width, _width, kCVPixelFormatType_32BGRA,
(__bridge CFDictionaryRef)options, &pxbuffer);
NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer.");
return pxbuffer;
}
@end
namespace flutter::testing {
TEST(FlutterEmbedderExternalTextureUnittests, TestTextureResolution) {
@@ -74,4 +113,50 @@ TEST(FlutterEmbedderExternalTextureUnittests, TestTextureResolution) {
gpuSurface->makeImageSnapshot();
}
TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTexture) {
// Constants.
const size_t width = 100;
const size_t height = 100;
const int64_t texture_id = 1;
// Set up the surface.
FlutterDarwinContextMetal* darwinContextMetal =
[[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice];
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
GrDirectContext* grContext = darwinContextMetal.mainContext.get();
sk_sp<SkSurface> gpuSurface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
// Create a texture.
TestExternalTexture* testExternalTexture = [[TestExternalTexture alloc] initWidth:width
height:height];
FlutterExternalTextureMetal* textureHolder =
[[FlutterExternalTextureMetal alloc] initWithFlutterTexture:testExternalTexture
darwinMetalContext:darwinContextMetal];
// Callback to resolve the texture.
EmbedderExternalTextureMetal::ExternalTextureCallback callback = [&](int64_t texture_id, size_t w,
size_t h) {
EXPECT_TRUE(w == width);
EXPECT_TRUE(h == height);
FlutterMetalExternalTexture* texture = new FlutterMetalExternalTexture();
[textureHolder populateTexture:texture];
EXPECT_TRUE(texture->num_textures == 1);
EXPECT_TRUE(texture->textures != nullptr);
EXPECT_TRUE(texture->pixel_format == FlutterMetalExternalTexturePixelFormat::kRGBA);
return std::unique_ptr<FlutterMetalExternalTexture>(texture);
};
// Render the texture.
std::unique_ptr<flutter::Texture> texture =
std::make_unique<EmbedderExternalTextureMetal>(texture_id, callback);
SkRect bounds = SkRect::MakeWH(info.width(), info.height());
SkSamplingOptions sampling = SkSamplingOptions(SkFilterMode::kNearest);
texture->Paint(*gpuSurface->getCanvas(), bounds, /*freeze=*/false, grContext, sampling);
gpuSurface->makeImageSnapshot();
}
} // namespace flutter::testing

View File

@@ -11,7 +11,7 @@
id<FlutterTexture> _texture;
id<MTLTexture> _mtlTexture;
std::vector<FlutterMetalTextureHandle> _textures;
}
- (instancetype)initWithFlutterTexture:(id<FlutterTexture>)texture
@@ -57,17 +57,14 @@
return NO;
}
_mtlTexture = CVMetalTextureGetTexture(cvMetalTexture);
_textures = {(__bridge FlutterMetalTextureHandle)CVMetalTextureGetTexture(cvMetalTexture)};
CVBufferRelease(cvMetalTexture);
std::vector<FlutterMetalTextureHandle> textures = {
(__bridge FlutterMetalTextureHandle)_mtlTexture};
textureOut->num_textures = 1;
textureOut->height = textureSize.height();
textureOut->width = textureSize.width();
textureOut->pixel_format = FlutterMetalExternalTexturePixelFormat::kRGBA;
textureOut->textures = textures.data();
textureOut->textures = _textures.data();
return YES;
}

View File

@@ -34,9 +34,7 @@
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, 200, 200, kCVPixelFormatType_32BGRA,
(__bridge CFDictionaryRef)options, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer.");
return pxbuffer;
}