Bug 95242 - objc_msgSend and IMP should be cast appropriately before using
Summary: objc_msgSend and IMP should be cast appropriately before using
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Platform (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Pratik Solanki
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2012-08-28 14:08 PDT by Pratik Solanki
Modified: 2012-08-31 13:48 PDT (History)
8 users (show)

See Also:


Attachments
Patch (23.91 KB, patch)
2012-08-28 14:25 PDT, Pratik Solanki
no flags Details | Formatted Diff | Diff
Patch (29.12 KB, patch)
2012-08-29 10:29 PDT, Pratik Solanki
no flags Details | Formatted Diff | Diff
Patch (34.56 KB, patch)
2012-08-30 11:33 PDT, Pratik Solanki
no flags Details | Formatted Diff | Diff
Fix for llvm-gcc (21.78 KB, patch)
2012-08-31 12:37 PDT, Pratik Solanki
no flags Details | Formatted Diff | Diff
Patch (1.36 KB, patch)
2012-08-31 13:41 PDT, Pratik Solanki
benjamin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Pratik Solanki 2012-08-28 14:08:42 PDT
Future compilers/runtimes will warn if we try to use objc_msgSend without the correct casts. We should add casts in WebKit/WebCore when we call objc_msgSend or IMP.
Comment 1 Pratik Solanki 2012-08-28 14:09:01 PDT
<rdar://problem/10906849>
Comment 2 Pratik Solanki 2012-08-28 14:25:46 PDT
Created attachment 161060 [details]
Patch
Comment 3 Benjamin Poulain 2012-08-28 14:54:42 PDT
Comment on attachment 161060 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=161060&action=review

> Source/WebKit/mac/WebView/WebDelegateImplementationCaching.mm:87
> -        return objc_msgSend(delegate, selector, self);
> +        return reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend)(delegate, selector, self);

What about making a bunch of webkit_objc_msgSend like this?:
template<typename Arg1Type, typename Arg2Type>
id webkit_objc_msgSend(id target, SEL selector, Arg1Type arg1, Arg2Type arg2)
{
    return reinterpret_cast<id (*)(id, SEL, Arg1Type, Arg2Type)>(objc_msgSend)(target, selector, arg1, arg2);
}
Comment 4 Benjamin Poulain 2012-08-28 16:03:13 PDT
Maybe we should also modify the style checker to prevent bare use of objc_msgSend?
Comment 5 Benjamin Poulain 2012-08-28 22:33:08 PDT
Comment on attachment 161060 [details]
Patch

Clearing the review flag, Pratik has an update.
Comment 6 Pratik Solanki 2012-08-29 10:29:18 PDT
Created attachment 161264 [details]
Patch
Comment 7 Pratik Solanki 2012-08-29 10:30:46 PDT
(In reply to comment #3)
> (From update of attachment 161060 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=161060&action=review
> 
> > Source/WebKit/mac/WebView/WebDelegateImplementationCaching.mm:87
> > -        return objc_msgSend(delegate, selector, self);
> > +        return reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend)(delegate, selector, self);
> 
> What about making a bunch of webkit_objc_msgSend like this?:
> template<typename Arg1Type, typename Arg2Type>
> id webkit_objc_msgSend(id target, SEL selector, Arg1Type arg1, Arg2Type arg2)
> {
>     return reinterpret_cast<id (*)(id, SEL, Arg1Type, Arg2Type)>(objc_msgSend)(target, selector, arg1, arg2);
> }

This was a great idea. New patch uploaded with this template code.
Comment 8 David Kilzer (:ddkilzer) 2012-08-29 12:49:42 PDT
Comment on attachment 161264 [details]
Patch

r=me
Comment 9 Benjamin Poulain 2012-08-29 13:56:42 PDT
Comment on attachment 161264 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=161264&action=review

> Source/WebKit/mac/WebView/WebDelegateImplementationCaching.mm:86
>  
> +template<typename Arg1Type>
> +id webkit_objc_msgSend(id target, SEL selector, Arg1Type arg1)
> +{
> +    return reinterpret_cast<id (*)(id, SEL, Arg1Type)>(objc_msgSend)(target, selector, arg1);
> +}

I would put the templates in a new header in WTF. Then I would ban the use of objc_msgSend through the style checker.

I am afraid new code with objc_msgSend might come without the reviewer knowing about this change.
Comment 10 Alexey Proskuryakov 2012-08-29 14:20:25 PDT
This template is limited in what it can do - specifically, it assumes that the functions returns an id.
Comment 11 WebKit Review Bot 2012-08-29 18:56:59 PDT
Comment on attachment 161264 [details]
Patch

Attachment 161264 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/13686455

New failing tests:
CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread
Comment 12 Pratik Solanki 2012-08-30 10:33:30 PDT
How did this fail? Searching for CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread on the log linked to above, I see

[----------] 1 test from CCLayerTreeHostTestAtomicCommitWithPartialUpdate
[ RUN      ] CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread
[       OK ] CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread (120 ms)
[----------] 1 test from CCLayerTreeHostTestAtomicCommitWithPartialUpdate (120 ms total)

What failed?
Comment 13 Benjamin Poulain 2012-08-30 10:41:06 PDT
(In reply to comment #12)
> How did this fail? Searching for CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread on the log linked to above, I see
> 
> [----------] 1 test from CCLayerTreeHostTestAtomicCommitWithPartialUpdate
> [ RUN      ] CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread
> [       OK ] CCLayerTreeHostTestAtomicCommitWithPartialUpdate.runMultiThread (120 ms)
> [----------] 1 test from CCLayerTreeHostTestAtomicCommitWithPartialUpdate (120 ms total)
> 
> What failed?

It is likely a sick bot. You patch does not touch anything on Linux.
Comment 14 Pratik Solanki 2012-08-30 11:33:34 PDT
Created attachment 161524 [details]
Patch
Comment 15 Benjamin Poulain 2012-08-30 12:14:50 PDT
Comment on attachment 161524 [details]
Patch

Very nice!
Comment 16 Pratik Solanki 2012-08-30 14:40:49 PDT
Committed r127193: <http://trac.webkit.org/changeset/127193>
Comment 17 Pratik Solanki 2012-08-31 10:18:39 PDT
Looks like llvm-gcc doesn't like my patch. From <http://webkit-commit-queue.appspot.com/results/13724238>

In file included from /Volumes/Data/WebKit/Source/WebCore/page/mac/EventHandlerMac.mm:53:
/Volumes/Data/WebKit/WebKitBuild/Release/usr/local/include/wtf/ObjcRuntimeExtras.h:29: error: default template arguments may not be used in function templates
/Volumes/Data/WebKit/WebKitBuild/Release/usr/local/include/wtf/ObjcRuntimeExtras.h:35: error: no default argument for 'Arg1Type'
/Volumes/Data/WebKit/WebKitBuild/Release/usr/local/include/wtf/ObjcRuntimeExtras.h:35: error: default template arguments may not be used in function templates
....
and more such errors like this. I'm looking into it.
Comment 18 Pratik Solanki 2012-08-31 10:56:27 PDT
Ok. I can reproduce the build failure by switching to llvm-gcc-4.2 on Mountain Lion (and removing a few -W arguments to the compiler). The template format is

template<typename RetType = id, typename Arg1Type>
RetType wtfObjcMsgSend(id target, SEL selector, Arg1Type arg1)
{
    return reinterpret_cast<RetType (*)(id, SEL, Arg1Type)>(objc_msgSend)(target, selector, arg1);
}

If I remove the " = id" part, it's happy. I could do that. And then have each call site explicitly specify the return type. Alternatively, what are the chances we can get the EWS bot updated with a more recent toolchain?
Comment 19 Benjamin Poulain 2012-08-31 11:50:52 PDT
> If I remove the " = id" part, it's happy. I could do that. And then have each call site explicitly specify the return type. Alternatively, what are the chances we can get the EWS bot updated with a more recent toolchain?

I am afraid we have to support the old compilers.
If the EWS have an old SDK, chances are many people do too.

I think the best is add an explanation in a FIXME and remove the default arg :( :(
Comment 20 Jessie Berlin 2012-08-31 12:04:43 PDT
(In reply to comment #19)
> > If I remove the " = id" part, it's happy. I could do that. And then have each call site explicitly specify the return type. Alternatively, what are the chances we can get the EWS bot updated with a more recent toolchain?
> 
> I am afraid we have to support the old compilers.
> If the EWS have an old SDK, chances are many people do too.
> 
> I think the best is add an explanation in a FIXME and remove the default arg :( :(

I agree with Benjamin. Please either do that soon or roll out http://trac.webkit.org/changeset/127193 (it is causing the mac-ews queue to get clogged).
Comment 21 Pratik Solanki 2012-08-31 12:06:32 PDT
Working on this now. I should have a patch soon.
Comment 22 Pratik Solanki 2012-08-31 12:37:03 PDT
Reopening to attach new patch.
Comment 23 Pratik Solanki 2012-08-31 12:37:06 PDT
Created attachment 161752 [details]
Fix for llvm-gcc
Comment 24 Benjamin Poulain 2012-08-31 12:41:18 PDT
Comment on attachment 161752 [details]
Fix for llvm-gcc

Looks good.

I think we should land this so we can take the EWS back online for everyone. Then we fix the EWS and we can eventually revert the patch.
Comment 25 Pratik Solanki 2012-08-31 13:11:40 PDT
Committed r127306: <http://trac.webkit.org/changeset/127306>
Comment 26 Pratik Solanki 2012-08-31 13:41:55 PDT
Reopening to attach new patch.
Comment 27 Pratik Solanki 2012-08-31 13:41:57 PDT
Created attachment 161762 [details]
Patch
Comment 28 Benjamin Poulain 2012-08-31 13:45:24 PDT
Comment on attachment 161762 [details]
Patch

Damn GCC.
Comment 29 Pratik Solanki 2012-08-31 13:48:23 PDT
Committed r127313: <http://trac.webkit.org/changeset/127313>