Skip to content

Commit

Permalink
Fix MIN_INT values when checking integer ranges (#22664)
Browse files Browse the repository at this point in the history
Fixes: #19251
  • Loading branch information
sbc100 authored Oct 1, 2024
1 parent b36624a commit 4cb578c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/runtime_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ var MAX_UINT32 = (2 ** 32) - 1;
var MAX_UINT53 = (2 ** 53) - 1;
var MAX_UINT64 = (2 ** 64) - 1;

var MIN_INT8 = - (2 ** ( 8 - 1)) + 1;
var MIN_INT16 = - (2 ** (16 - 1)) + 1;
var MIN_INT32 = - (2 ** (32 - 1)) + 1;
var MIN_INT53 = - (2 ** (53 - 1)) + 1;
var MIN_INT64 = - (2 ** (64 - 1)) + 1;
var MIN_INT8 = - (2 ** ( 8 - 1));
var MIN_INT16 = - (2 ** (16 - 1));
var MIN_INT32 = - (2 ** (32 - 1));
var MIN_INT53 = - (2 ** (53 - 1));
var MIN_INT64 = - (2 ** (64 - 1));

function checkInt(value, bits, min, max) {
assert(Number.isInteger(Number(value)), `attempt to write non-integer (${value}) into integer heap`);
Expand Down
25 changes: 24 additions & 1 deletion test/core/test_getValue_setValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// found in the LICENSE file.

#include <emscripten.h>
#include <stdint.h>

int main() {
char buffer_char[8] = { 'x' };
Expand All @@ -30,6 +31,28 @@ int main() {
ptr = Module['getValue']($1, '*');
out('ptr: 0x' + ptr.toString(16) + ' ' + typeof(ptr));
#endif
}, buffer_char, buffer_ptr);

/* In ASSERTIONS=2 mode we check the limits of input values */
var min = $2;
var max = $3;
setValue($1, max, 'i8');
setValue($1, min, 'i8');
#ifdef ASSERTIONS_2
function checkAsserts(f, expected) {
try {
f();
} catch(e) {
assert(e.message.includes(expected), "expected assertion to include:" + expected);
return;
}
assert(false, "expected assertion");
}
checkAsserts(() => setValue($1, max + 1, 'i8'), "value (256) too large to write as 8-bit value");
checkAsserts(() => setValue($1, min - 1, 'i8'), "value (-129) too small to write as 8-bit value");
#else
setValue($1, max + 1, 'i8');
setValue($1, min - 1, 'i8');
#endif
}, buffer_char, buffer_ptr, INT8_MIN, UINT8_MAX);
}

6 changes: 6 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7068,11 +7068,17 @@ def test(args=None, asserts=False):
# see that direct usage (not on module) works. we don't export, but the use
# keeps it alive through JSDCE
test(args=['-DDIRECT'])

# Test with ASSERTIONS=2 where we check the limits of value passed to setValue.
self.set_setting('ASSERTIONS', 2)
test(args=['-DDIRECT', '-DASSERTIONS_2'])

# see that with assertions, we get a nice error message
self.set_setting('EXPORTED_RUNTIME_METHODS', [])
self.set_setting('ASSERTIONS')
test(asserts=True)
self.set_setting('ASSERTIONS', 0)

# see that when we export them, things work on the module
self.set_setting('EXPORTED_RUNTIME_METHODS', ['getValue', 'setValue'])
test()
Expand Down

0 comments on commit 4cb578c

Please sign in to comment.