RESOLVED FIXED 188556
[JSC] Should not rotate constant with 64
https://bugs.webkit.org/show_bug.cgi?id=188556
Summary [JSC] Should not rotate constant with 64
Yusuke Suzuki
Reported 2018-08-14 09:11:08 PDT
[JSC] Should not rotate constant with 64
Attachments
Patch (2.78 KB, patch)
2018-08-14 09:13 PDT, Yusuke Suzuki
no flags
Patch (2.85 KB, patch)
2018-08-19 18:03 PDT, Yusuke Suzuki
saam: review+
ews-watchlist: commit-queue-
Archive of layout-test-results from ews206 for win-future (12.92 MB, application/zip)
2018-08-19 20:39 PDT, EWS Watchlist
no flags
Yusuke Suzuki
Comment 1 2018-08-14 09:13:13 PDT
Don Olmstead
Comment 2 2018-08-14 09:47:31 PDT
Comment on attachment 347080 [details] Patch Informal review is r+. This issue was found within a run of ARES6 using UBSan. This will prevent the undefined behavior.
Mark Lam
Comment 3 2018-08-14 10:12:26 PDT
Comment on attachment 347080 [details] Patch r=me
Yusuke Suzuki
Comment 4 2018-08-14 10:14:26 PDT
(In reply to Don Olmstead from comment #2) > Comment on attachment 347080 [details] > Patch > > Informal review is r+. This issue was found within a run of ARES6 using > UBSan. This will prevent the undefined behavior. Yeah, this is nice. (In reply to Mark Lam from comment #3) > Comment on attachment 347080 [details] > Patch > > r=me Thanks :)
Yusuke Suzuki
Comment 5 2018-08-14 10:16:38 PDT
Radar WebKit Bug Importer
Comment 6 2018-08-14 10:17:46 PDT
WebKit Commit Bot
Comment 7 2018-08-19 17:22:41 PDT
Re-opened since this is blocked by bug 188736
Darin Adler
Comment 8 2018-08-19 17:41:15 PDT
Comment on attachment 347080 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=347080&action=review > Source/JavaScriptCore/assembler/MacroAssembler.h:1298 > + // Generate the seed in [0, widthInBits). We should not generate widthInBits > + // since it leads to `<< widthInBits`, which is an undefined behavior. > + return random() % (widthInBits - 1); I don’t understand the original bug, nor do I understand how this change will fix it. The comment doesn’t agree with the code. Let’s consider the case where widthInBits is 64. This code then does (u % 63), where u is a 32-bit unsigned integer. That operation should result in an integer in the range [0, 63). The existing code that does (u % 64) already should yield a number in the range [0, 64). The theory of the bug doesn’t make sense. The result of (u % 64) should not ever be 64. Is there a bug here in the original code? How did we come to suspect there was?
Darin Adler
Comment 9 2018-08-19 17:41:40 PDT
(In reply to Don Olmstead from comment #2) > This issue was found within a run of ARES6 using > UBSan. This will prevent the undefined behavior. I’d like to know more about exactly what UBSan reported.
Saam Barati
Comment 10 2018-08-19 17:45:30 PDT
Comment on attachment 347080 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=347080&action=review >> Source/JavaScriptCore/assembler/MacroAssembler.h:1298 >> + return random() % (widthInBits - 1); > > I don’t understand the original bug, nor do I understand how this change will fix it. > > The comment doesn’t agree with the code. > > Let’s consider the case where widthInBits is 64. This code then does (u % 63), where u is a 32-bit unsigned integer. That operation should result in an integer in the range [0, 63). The existing code that does (u % 64) already should yield a number in the range [0, 64). The theory of the bug doesn’t make sense. The result of (u % 64) should not ever be 64. > > Is there a bug here in the original code? How did we come to suspect there was? Isn't the bug here just that a shift by a number >= 64 UB?
Saam Barati
Comment 11 2018-08-19 17:49:25 PDT
Comment on attachment 347080 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=347080&action=review >>> Source/JavaScriptCore/assembler/MacroAssembler.h:1298 >>> + return random() % (widthInBits - 1); >> >> I don’t understand the original bug, nor do I understand how this change will fix it. >> >> The comment doesn’t agree with the code. >> >> Let’s consider the case where widthInBits is 64. This code then does (u % 63), where u is a 32-bit unsigned integer. That operation should result in an integer in the range [0, 63). The existing code that does (u % 64) already should yield a number in the range [0, 64). The theory of the bug doesn’t make sense. The result of (u % 64) should not ever be 64. >> >> Is there a bug here in the original code? How did we come to suspect there was? > > Isn't the bug here just that a shift by a number >= 64 UB? I just read the original code. I also don't see how there is a bug. Since we already were doing % 64, we'd end up with a number in the range of [0, 63].
Darin Adler
Comment 12 2018-08-19 17:51:58 PDT
(In reply to Saam Barati from comment #11) > I just read the original code. I also don't see how there is a bug. Since we > already were doing % 64, we'd end up with a number in the range of [0, 63]. Yes, that matches my understanding. Since UBSan complained, I would like to know what it complained about. If it complained about a shift of 64, then I would like to know the details because the results of a % 64 should never be 64.
Yusuke Suzuki
Comment 13 2018-08-19 17:55:34 PDT
Comment on attachment 347080 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=347080&action=review >>>> Source/JavaScriptCore/assembler/MacroAssembler.h:1298 >>>> + return random() % (widthInBits - 1); >>> >>> I don’t understand the original bug, nor do I understand how this change will fix it. >>> >>> The comment doesn’t agree with the code. >>> >>> Let’s consider the case where widthInBits is 64. This code then does (u % 63), where u is a 32-bit unsigned integer. That operation should result in an integer in the range [0, 63). The existing code that does (u % 64) already should yield a number in the range [0, 64). The theory of the bug doesn’t make sense. The result of (u % 64) should not ever be 64. >>> >>> Is there a bug here in the original code? How did we come to suspect there was? >> >> Isn't the bug here just that a shift by a number >= 64 UB? > > I just read the original code. I also don't see how there is a bug. Since we already were doing % 64, we'd end up with a number in the range of [0, 63]. Yeah, the problem is, >> 64 AND << 64 are both undefined behavior. https://wiki.sei.cmu.edu/confluence/display/c/INT34-C.+Do+not+shift+an+expression+by+a+negative+number+of+bits+or+by+greater+than+or+equal+to+the+number+of+bits+that+exist+in+the+operand We should generate a seed [1, 63] to make `value = (value << rotation) | (value >> (sizeof(void*) * 8 - rotation));` safe.
Yusuke Suzuki
Comment 14 2018-08-19 17:57:02 PDT
Comment on attachment 347080 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=347080&action=review >>>>> Source/JavaScriptCore/assembler/MacroAssembler.h:1298 >>>>> + return random() % (widthInBits - 1); >>>> >>>> I don’t understand the original bug, nor do I understand how this change will fix it. >>>> >>>> The comment doesn’t agree with the code. >>>> >>>> Let’s consider the case where widthInBits is 64. This code then does (u % 63), where u is a 32-bit unsigned integer. That operation should result in an integer in the range [0, 63). The existing code that does (u % 64) already should yield a number in the range [0, 64). The theory of the bug doesn’t make sense. The result of (u % 64) should not ever be 64. >>>> >>>> Is there a bug here in the original code? How did we come to suspect there was? >>> >>> Isn't the bug here just that a shift by a number >= 64 UB? >> >> I just read the original code. I also don't see how there is a bug. Since we already were doing % 64, we'd end up with a number in the range of [0, 63]. > > Yeah, the problem is, >> 64 AND << 64 are both undefined behavior. > https://wiki.sei.cmu.edu/confluence/display/c/INT34-C.+Do+not+shift+an+expression+by+a+negative+number+of+bits+or+by+greater+than+or+equal+to+the+number+of+bits+that+exist+in+the+operand > We should generate a seed [1, 63] to make `value = (value << rotation) | (value >> (sizeof(void*) * 8 - rotation));` safe. The first fix was wrong, since it does not produce 64 without this patch. I thought the UB is caused by `value << 64`. But in fact, it is caused by `value >> (sizeof(void*) * 8 - rotation)` part if `rotation` is 0. I'll upload a patch, which generates a seed within [1, 63].
Yusuke Suzuki
Comment 15 2018-08-19 18:03:22 PDT
EWS Watchlist
Comment 16 2018-08-19 20:39:17 PDT
Comment on attachment 347474 [details] Patch Attachment 347474 [details] did not pass win-ews (win): Output: https://webkit-queues.webkit.org/results/8913070 New failing tests: http/tests/security/contentSecurityPolicy/video-with-https-url-allowed-by-csp-media-src-star.html
EWS Watchlist
Comment 17 2018-08-19 20:39:30 PDT
Created attachment 347478 [details] Archive of layout-test-results from ews206 for win-future The attached test failures were seen while running run-webkit-tests on the win-ews. Bot: ews206 Port: win-future Platform: CYGWIN_NT-6.1-2.9.0-0.318-5-3-x86_64-64bit
Yusuke Suzuki
Comment 18 2018-08-21 22:03:01 PDT
Note You need to log in before you can comment on or make changes to this bug.