From 2465a103b8831ce926b678c14ed892f1af099540 Mon Sep 17 00:00:00 2001 From: Tao Xu Date: Mon, 20 Sep 2021 16:24:12 -0700 Subject: [PATCH] [iOS] Zero out NSError to avoid heap corruptions for the OSS builds (#65355) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/65355 I've been seeing heap corruptions in the CMake builds due to the NSError* not being initialized with `nil`. However, I haven't see this issue for the BUCK builds. ghstack-source-id: 138502708 Test Plan: 1. Test the OSS builds to make sure the heap corruption has gone. 2. Test the Buck build in the playground app 3. Circle CI Reviewed By: hanton Differential Revision: D31048010 fbshipit-source-id: cfd8d614f3f91f09caee4aab61237007ec080481 --- aten/src/ATen/native/metal/MetalContext.mm | 4 ++-- torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm | 2 +- torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm | 9 ++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/aten/src/ATen/native/metal/MetalContext.mm b/aten/src/ATen/native/metal/MetalContext.mm index f71d35f..51423f5 100644 --- a/aten/src/ATen/native/metal/MetalContext.mm +++ b/aten/src/ATen/native/metal/MetalContext.mm @@ -77,7 +77,7 @@ using namespace at::native::metal; } id func = [_library newFunctionWithName:[NSString stringWithUTF8String:kernel.c_str()]]; TORCH_CHECK(func, "Failed to load the Metal Shader function: ", kernel); - NSError* errors; + NSError* errors = nil; state = [_device newComputePipelineStateWithFunction:func error:&errors]; TORCH_CHECK(state, errors.localizedDescription.UTF8String); _pipelineCache[kernel] = state; @@ -122,7 +122,7 @@ using namespace at::native::metal; floatArgIndex++; } } - NSError* errors; + NSError* errors = nil; id func = [_library newFunctionWithName:[NSString stringWithUTF8String:kernel.c_str()] constantValues:constantValues error:&errors]; diff --git a/torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm b/torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm index 720b137..02884ae 100644 --- a/torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm +++ b/torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm @@ -38,7 +38,7 @@ static inline c10::ScalarType scalarType(TensorType type) { static id parse(NSString* jsonStr) { NSData* data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; - NSError* error; + NSError* error = nil; id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; diff --git a/torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm b/torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm index 7647e16..533d664 100644 --- a/torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm +++ b/torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm @@ -68,7 +68,7 @@ _modelPath = [self _save:modelSpecs identifier:[NSString stringWithCString:identifier.c_str() encoding:NSUTF8StringEncoding]]; - NSError* error; + NSError* error = nil; NSURL* compiledModelPath = nil; if (@available(iOS 11.0, macOS 10.13, *)) { compiledModelPath = @@ -113,12 +113,11 @@ - (id)forwardWithInputs: (const std::vector&)inputs { - NSError* error; + NSError* error = nil; PTMCoreMLFeatureProvider* inputFeature = [[PTMCoreMLFeatureProvider alloc] initWithFeatureSpecs:inputs CoreMLVersion:self.coreMLVersion]; if (inputFeature == nil) { - NSLog(@"inputFeature is not initialized."); return nil; } if (@available(iOS 11.0, macOS 10.13, *)) { @@ -136,14 +135,14 @@ return outputFeature; } else { - TORCH_CHECK("Core ML is available on iOS 11.0 and above"); + TORCH_CHECK(false, "Core ML is available on iOS 11.0 and above"); return nil; } } - (BOOL)cleanup { NSFileManager* fileManager = [NSFileManager defaultManager]; - NSError* error; + NSError* error = nil; if (![fileManager fileExistsAtPath:_modelPath]) { [fileManager removeItemAtPath:_modelPath error:&error]; } -- 2.7.4