Skip to content

Commit c8ee076

Browse files
author
Mark Hahnenberg
committed
Dynamically generated JSExport protocols added to a class results in a crash
https://bugs.webkit.org/show_bug.cgi?id=129108 Reviewed by Oliver Hunt. We're not getting any information from the runtime about the types of the methods on these protocols because they didn't exist at compile time. We should handle this gracefully. * API/ObjCCallbackFunction.mm: (objCCallbackFunctionForInvocation): * API/tests/JSExportTests.mm: (+[JSExportTests exportDynamicallyGeneratedProtocolTest]): (runJSExportTests): Canonical link: https://commits.webkit.org/147147@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@164439 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 7fca3de commit c8ee076

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

Source/JavaScriptCore/API/ObjCCallbackFunction.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,9 @@ inline bool skipNumber(const char*& position)
632632

633633
static JSObjectRef objCCallbackFunctionForInvocation(JSContext *context, NSInvocation *invocation, CallbackType type, Class instanceClass, const char* signatureWithObjcClasses)
634634
{
635+
if (!signatureWithObjcClasses)
636+
return nil;
637+
635638
const char* position = signatureWithObjcClasses;
636639

637640
OwnPtr<CallbackResult> result = adoptPtr(parseObjCType<ResultTypeDelegate>(position));

Source/JavaScriptCore/API/tests/JSExportTests.mm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525

2626
#import "JSExportTests.h"
2727

28+
#import <objc/runtime.h>
29+
#import <objc/objc.h>
30+
2831
#if JSC_OBJC_API_ENABLED
2932

3033
extern "C" void checkResult(NSString *description, bool passed);
3134

3235
@interface JSExportTests : NSObject
3336
+ (void) exportInstanceMethodWithIdProtocolTest;
3437
+ (void) exportInstanceMethodWithClassProtocolTest;
38+
+ (void) exportDynamicallyGeneratedProtocolTest;
3539
@end
3640

3741
@protocol TruthTeller
@@ -100,13 +104,33 @@ + (void) exportInstanceMethodWithClassProtocolTest
100104
[context evaluateScript:@"makeTestObject().methodWithClassProtocol(opaqueObject);"];
101105
checkResult(@"Successfully exported instance method", !context.exception);
102106
}
107+
108+
+ (void) exportDynamicallyGeneratedProtocolTest
109+
{
110+
JSContext *context = [[JSContext alloc] init];
111+
Protocol *dynProtocol = objc_allocateProtocol("NSStringJSExport");
112+
Protocol *jsExportProtocol = @protocol(JSExport);
113+
protocol_addProtocol(dynProtocol, jsExportProtocol);
114+
Method method = class_getInstanceMethod([NSString class], @selector(boolValue));
115+
protocol_addMethodDescription(dynProtocol, @selector(boolValue), method_getTypeEncoding(method), YES, YES);
116+
NSLog(@"type encoding = %s", method_getTypeEncoding(method));
117+
protocol_addMethodDescription(dynProtocol, @selector(boolValue), "B@:", YES, YES);
118+
objc_registerProtocol(dynProtocol);
119+
class_addProtocol([NSString class], dynProtocol);
120+
121+
context[@"NSString"] = [NSString class];
122+
context[@"myString"] = @"YES";
123+
JSValue *value = [context evaluateScript:@"myString.boolValue()"];
124+
checkResult(@"Dynamically generated JSExport-ed protocols are ignored", [value isUndefined] && !!context.exception);
125+
}
103126
@end
104127

105128
void runJSExportTests()
106129
{
107130
@autoreleasepool {
108131
[JSExportTests exportInstanceMethodWithIdProtocolTest];
109132
[JSExportTests exportInstanceMethodWithClassProtocolTest];
133+
[JSExportTests exportDynamicallyGeneratedProtocolTest];
110134
}
111135
}
112136

Source/JavaScriptCore/ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2014-02-20 Mark Hahnenberg <mhahnenberg@apple.com>
2+
3+
Dynamically generated JSExport protocols added to a class results in a crash
4+
https://bugs.webkit.org/show_bug.cgi?id=129108
5+
6+
Reviewed by Oliver Hunt.
7+
8+
We're not getting any information from the runtime about the types of the methods on
9+
these protocols because they didn't exist at compile time. We should handle this gracefully.
10+
11+
* API/ObjCCallbackFunction.mm:
12+
(objCCallbackFunctionForInvocation):
13+
* API/tests/JSExportTests.mm:
14+
(+[JSExportTests exportDynamicallyGeneratedProtocolTest]):
15+
(runJSExportTests):
16+
117
2014-02-20 Gabor Rapcsanyi <rgabor@webkit.org>
218

319
ASSERTION FAILED: isUInt16() on ARMv7 after r113253.

0 commit comments

Comments
 (0)