Skip to content

Commit 19a8be2

Browse files
authored
Merge pull request #60: date-format: Add result hashing to prevent DCE
Dead code elimination could be used to bypass the date formatting in extreme cases. Create a simple hash from the result strings to prevent this.
2 parents 5023d37 + 3051ba4 commit 19a8be2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

SunSpider/date-format-tofte.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,34 @@ Date.prototype.formatDate = function (input,time) {
305305

306306
function run() {
307307
var date = new Date("1/1/2007 1:11:11");
308+
var resultHash = 0x1a2b3c4d;
308309

309310
for (i = 0; i < 500; ++i) {
310311
var shortFormat = date.formatDate("Y-m-d");
311312
var longFormat = date.formatDate("l, F d, Y g:i:s A");
312313
date.setTime(date.getTime() + 84266956);
314+
// Assuming only ascii output.
315+
resultHash ^= shortFormat.charCodeAt(6) | shortFormat.charCodeAt(8) << 8;
316+
resultHash ^= longFormat.charCodeAt(10) << 16 | longFormat.charCodeAt(11) << 24;
313317
}
314318

315319
// FIXME: Find a way to validate this test.
316320
// https://bugs.webkit.org/show_bug.cgi?id=114849
321+
return resultHash;
317322
}
318323

319324

320325
class Benchmark {
326+
EXPECTED_RESULT_HASH = 439041101;
327+
321328
runIteration() {
329+
this.resultHash = 0x1a2b3c4d;
322330
for (let i = 0; i < 8; ++i)
323-
run();
331+
this.resultHash ^= run();
332+
}
333+
334+
validate() {
335+
if (this.resultHash != this.EXPECTED_RESULT_HASH)
336+
throw new Error(`Got unexpected result hash ${this.resultHash} instead of ${this.EXPECTED_RESULT_HASH}`)
324337
}
325338
}

SunSpider/date-format-xparb.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,34 @@ Date.patterns = {
410410

411411
function run() {
412412
var date = new Date("1/1/2007 1:11:11");
413+
var resultHash = 0x1a2b3c4d;
413414

414415
for (i = 0; i < 4000; ++i) {
415416
var shortFormat = date.dateFormat("Y-m-d");
416417
var longFormat = date.dateFormat("l, F d, Y g:i:s A");
417418
date.setTime(date.getTime() + 84266956);
419+
// Assuming only ascii output.
420+
resultHash ^= shortFormat.charCodeAt(6) | shortFormat.charCodeAt(8) << 8;
421+
resultHash ^= longFormat.charCodeAt(10) << 16 | longFormat.charCodeAt(11) << 24;
418422
}
419423

420424
// FIXME: Find a way to validate this test.
421425
// https://bugs.webkit.org/show_bug.cgi?id=114849
426+
return resultHash;
422427
}
423428

424429

425430
class Benchmark {
431+
EXPECTED_RESULT_HASH = 439041101;
432+
426433
runIteration() {
434+
this.resultHash = 0x1a2b3c4d;
427435
for (let i = 0; i < 8; ++i)
428-
run();
436+
this.resultHash ^= run();
437+
}
438+
439+
validate() {
440+
if (this.resultHash != this.EXPECTED_RESULT_HASH)
441+
throw new Error(`Got unexpected result hash ${this.resultHash} instead of ${this.EXPECTED_RESULT_HASH}`)
429442
}
430443
}

0 commit comments

Comments
 (0)