BC Soft Errors

Reviewed By: javache

Differential Revision: D4131340

fbshipit-source-id: b0827fc99d94985fbbb2a1b03ddc4d6feab10bff
This commit is contained in:
Ashok Menon
2016-11-11 05:21:37 -08:00
committed by Facebook Github Bot
parent 7a073d3b13
commit 8288bc2006
5 changed files with 63 additions and 21 deletions

View File

@@ -11,6 +11,7 @@
#import "RCTBridge.h"
#import "RCTConvert.h"
#import "RCTJSCWrapper.h"
#import "RCTSourceCode.h"
#import "RCTUtils.h"
#import "RCTPerformanceLogger.h"
@@ -23,13 +24,13 @@ static uint64_t const RCTBCBundleMagicNumber = 0xFF4865726D657300;
NSString *const RCTJavaScriptLoaderErrorDomain = @"RCTJavaScriptLoaderErrorDomain";
RCTScriptTag RCTParseMagicNumber(RCTMagicNumber magic)
RCTScriptTag RCTParseTypeFromHeader(RCTBundleHeader header)
{
if (NSSwapLittleIntToHost(magic.first4) == RCTRAMBundleMagicNumber) {
if (NSSwapLittleIntToHost(header.RAMMagic) == RCTRAMBundleMagicNumber) {
return RCTScriptRAMBundle;
}
if (NSSwapLittleLongLongToHost(magic.first8) == RCTBCBundleMagicNumber) {
if (NSSwapLittleLongLongToHost(header.BCMagic) == RCTBCBundleMagicNumber) {
return RCTScriptBCBundle;
}
@@ -61,6 +62,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
int64_t sourceLength;
NSError *error;
NSData *data = [self attemptSynchronousLoadOfBundleAtURL:scriptURL
runtimeBCVersion:JSNoBytecodeFileFormatVersion
sourceLength:&sourceLength
error:&error];
if (data) {
@@ -80,6 +82,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
}
+ (NSData *)attemptSynchronousLoadOfBundleAtURL:(NSURL *)scriptURL
runtimeBCVersion:(int32_t)runtimeBCVersion
sourceLength:(int64_t *)sourceLength
error:(NSError **)error
{
@@ -126,8 +129,8 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
return nil;
}
RCTMagicNumber magicNumber = {.allBytes = 0};
size_t readResult = fread(&magicNumber, sizeof(magicNumber), 1, bundle);
RCTBundleHeader header = {};
size_t readResult = fread(&header, sizeof(header), 1, bundle);
fclose(bundle);
if (readResult != 1) {
if (error) {
@@ -139,8 +142,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
return nil;
}
RCTScriptTag tag = RCTParseMagicNumber(magicNumber);
if (tag == RCTScriptString) {
RCTScriptTag tag = RCTParseTypeFromHeader(header);
switch (tag) {
case RCTScriptRAMBundle:
break;
case RCTScriptString:
if (error) {
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
code:RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously
@@ -148,6 +155,21 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
@"Cannot load text/javascript files synchronously"}];
}
return nil;
case RCTScriptBCBundle:
if (header.BCVersion != runtimeBCVersion) {
if (error) {
NSString *errDesc =
[NSString stringWithFormat:@"BC Version Mismatch. Expect: %d, Actual: %d",
runtimeBCVersion, header.BCVersion];
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
code:RCTJavaScriptLoaderErrorBCVersion
userInfo:@{NSLocalizedDescriptionKey: errDesc}];
}
return nil;
}
break;
}
struct stat statInfo;
@@ -163,7 +185,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
if (sourceLength) {
*sourceLength = statInfo.st_size;
}
return [NSData dataWithBytes:&magicNumber length:sizeof(magicNumber)];
return [NSData dataWithBytes:&header length:sizeof(header)];
}
static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoadProgressBlock onProgress, RCTSourceLoadBlock onComplete)