Bug 81570 - Properly align members in WebInputEvent and subclasses to make Valgrind happy
Summary: Properly align members in WebInputEvent and subclasses to make Valgrind happy
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: UI Events (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Tony Chang
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-19 15:22 PDT by Lei Zhang
Modified: 2012-04-05 11:48 PDT (History)
7 users (show)

See Also:


Attachments
patch to fix the bug (11.57 KB, patch)
2012-03-19 15:23 PDT, Lei Zhang
tony: review-
Details | Formatted Diff | Diff
address comments (5.93 KB, patch)
2012-03-20 15:57 PDT, Lei Zhang
no flags Details | Formatted Diff | Diff
Patch for landing (11.76 KB, patch)
2012-04-04 16:30 PDT, Tony Chang
no flags Details | Formatted Diff | Diff
Patch for landing (6.11 KB, patch)
2012-04-04 16:32 PDT, Tony Chang
no flags Details | Formatted Diff | Diff
Patch (6.14 KB, patch)
2012-04-04 17:00 PDT, Tony Chang
no flags Details | Formatted Diff | Diff
Patch (6.64 KB, patch)
2012-04-05 10:14 PDT, Tony Chang
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Lei Zhang 2012-03-19 15:22:15 PDT
Currently, on 64-bit Linux, WebInputEvents have alignment issues with its data members that leave gaps in the in-memory representation. So even though one has initialized all the data members, Valgrind will still complain about uninitialized memory when Chromium readys WebInputEvents byte-by-byte when sending WebInputEvents over IPC.

To fix this, I've rearranged the data member order and added dummy padding members as needed. To ensure this problem does not occur in the future, I've added COMPILE_ASSERT() checks for all the classes to make sure there's no gaps.
Comment 1 Lei Zhang 2012-03-19 15:23:41 PDT
Created attachment 132684 [details]
patch to fix the bug
Comment 2 Lei Zhang 2012-03-19 15:24:58 PDT
Tony: can you review this?
Comment 3 Tony Chang 2012-03-19 15:38:56 PDT
Should we just use #pragma pack(push, 4) instead?
Comment 4 Tony Chang 2012-03-19 15:53:56 PDT
(In reply to comment #3)
> Should we just use #pragma pack(push, 4) instead?

I'm dumb. Will pointed out that we would still need to reorder variables to make sure doubles come before ints.

Here's another idea from Will: Can we memset the struct before memcpy?
Comment 5 Lei Zhang 2012-03-19 16:06:27 PDT
(In reply to comment #4)
> (In reply to comment #3)
> > Should we just use #pragma pack(push, 4) instead?
> 
> I'm dumb. Will pointed out that we would still need to reorder variables to make sure doubles come before ints.
> 
> Here's another idea from Will: Can we memset the struct before memcpy?

The downside to using memset is that we cannot use initializer lists.
Comment 6 Tony Chang 2012-03-19 16:29:03 PDT
Comment on attachment 132684 [details]
patch to fix the bug

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

You're right, memset won't work in this case.  Reordering seems unavoidable considering how content/public/common/webkit_param_traits.h handles WebInputEvent*.  It might still be worth packing to 4 bytes to avoid having padding ints.

> WebKit/chromium/public/WebInputEvent.h:225
> +    sizeof(WebInputEvent) == (sizeof(WebInputEvent::timeStampSeconds) +
> +                              sizeof(WebInputEvent::size) +

We normally just make a reference struct and make sure the real object has the same size as the reference struct.  See for example the SameSizeAsCSSValue class in WebKit/Source/WebCore/css/CSSValue.cpp.  For your SameSizeAs* class, you can just put doubles in them.
Comment 7 Lei Zhang 2012-03-20 15:57:39 PDT
Created attachment 132917 [details]
address comments
Comment 8 Lei Zhang 2012-04-04 16:10:29 PDT
ping.
Comment 9 Tony Chang 2012-04-04 16:27:57 PDT
Comment on attachment 132917 [details]
address comments

Sorry, missed the update.  I'll get a patch that applies and put it in the commit queue.
Comment 10 Tony Chang 2012-04-04 16:30:42 PDT
Created attachment 135714 [details]
Patch for landing
Comment 11 Tony Chang 2012-04-04 16:32:43 PDT
Created attachment 135715 [details]
Patch for landing
Comment 12 WebKit Review Bot 2012-04-04 16:46:00 PDT
Please wait for approval from abarth@webkit.org, dglazkov@chromium.org, fishd@chromium.org, jamesr@chromium.org or tkent@chromium.org before submitting, as this patch contains changes to the Chromium public API. See also https://trac.webkit.org/wiki/ChromiumWebKitAPI.
Comment 13 WebKit Review Bot 2012-04-04 16:46:25 PDT
Attachment 135715 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WebKit/chromium/ChangeLog', u'Sourc..." exit_code: 1
Source/WebKit/chromium/public/WebInputEvent.h:155:  One space before end of line comments  [whitespace/comments] [5]
Source/WebKit/chromium/public/WebInputEvent.h:239:  Should have only a single space after a punctuation in a comment.  [whitespace/comments] [5]
Source/WebKit/chromium/public/WebInputEvent.h:407:  input_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/public/WebInputEvent.h:411:  keyboard_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/public/WebInputEvent.h:415:  mouse_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/public/WebInputEvent.h:419:  mousewheel_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/public/WebInputEvent.h:423:  gesture_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Source/WebKit/chromium/public/WebInputEvent.h:427:  touch_data is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]
Total errors found: 8 in 2 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 14 Tony Chang 2012-04-04 17:00:19 PDT
Created attachment 135724 [details]
Patch
Comment 15 WebKit Review Bot 2012-04-04 18:05:56 PDT
Comment on attachment 135724 [details]
Patch

Attachment 135724 [details] did not pass chromium-ews (chromium-xvfb):
Output: http://queues.webkit.org/results/12340043
Comment 16 James Robinson 2012-04-04 19:26:48 PDT
Alternate idea: can we make valgrind smarter?
Comment 17 Lei Zhang 2012-04-04 20:36:21 PDT
(In reply to comment #16)
> Alternate idea: can we make valgrind smarter?

base/third_party/valgrind/memcheck.h has macros like VALGRIND_MAKE_MEM_DEFINED to say "trust me, this block of data is defined." But I think the problem is we would need to use it in every place that accesses these data structures byte by byte.
Comment 18 Darin Fisher (:fishd, Google) 2012-04-04 23:18:45 PDT
Comment on attachment 135724 [details]
Patch

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

> Source/WebKit/chromium/public/WebInputEvent.h:406
> +class SameSizeAsWebInputEvent {

nit: consider putting this code into a nested namespace b/c these classes
are never meant to be used by consumers of the WebKit API.

WebKit::Internal:: perhaps?
Comment 19 Tony Chang 2012-04-05 10:14:47 PDT
Created attachment 135848 [details]
Patch
Comment 20 WebKit Review Bot 2012-04-05 11:48:05 PDT
Comment on attachment 135848 [details]
Patch

Clearing flags on attachment: 135848

Committed r113344: <http://trac.webkit.org/changeset/113344>
Comment 21 WebKit Review Bot 2012-04-05 11:48:25 PDT
All reviewed patches have been landed.  Closing bug.