[iOS] Zero out NSError to avoid heap corruptions for the OSS builds (#65355)
authorTao Xu <taox@fb.com>
Mon, 20 Sep 2021 23:24:12 +0000 (16:24 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Mon, 20 Sep 2021 23:31:23 +0000 (16:31 -0700)
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
torch/csrc/jit/backends/coreml/objc/PTMCoreMLBackend.mm
torch/csrc/jit/backends/coreml/objc/PTMCoreMLExecutor.mm

index f71d35f..51423f5 100644 (file)
@@ -77,7 +77,7 @@ using namespace at::native::metal;
   }
   id<MTLFunction> 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<MTLFunction> func = [_library newFunctionWithName:[NSString stringWithUTF8String:kernel.c_str()]
                                         constantValues:constantValues
                                                  error:&errors];
index 720b137..02884ae 100644 (file)
@@ -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];
index 7647e16..533d664 100644 (file)
@@ -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 =
 
 - (id<MLFeatureProvider>)forwardWithInputs:
     (const std::vector<PTMCoreMLFeatureSpecs>&)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, *)) {
 
     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];
   }