Skip to content

Commit bf07641

Browse files
committed
Merge pull request #1195 from inolen/longjmp_signed
trigger setjmp on negative values
2 parents 7102046 + 91a245c commit bf07641

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/jsifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ function JSify(data, functionsOnly, givenFunctions) {
14891489
if (ASM_JS && funcData.setjmpTable) {
14901490
// check if a longjmp was done. If a setjmp happened, check if ours. If ours, go to -111 to handle it.
14911491
// otherwise, just return - the call to us must also have been an invoke, so the setjmp propagates that way
1492-
ret += '; if (((__THREW__|0) != 0) & ((threwValue|0) > 0)) { setjmpLabel = ' + asmCoercion('_testSetjmp(' + makeGetValue('__THREW__', 0, 'i32') + ', setjmpTable)', 'i32') + '; if ((setjmpLabel|0) > 0) { label = -1111; break } else return ' + (funcData.returnType != 'void' ? asmCoercion('0', funcData.returnType) : '') + ' } __THREW__ = threwValue = 0;\n';
1492+
ret += '; if (((__THREW__|0) != 0) & ((threwValue|0) !== 0)) { setjmpLabel = ' + asmCoercion('_testSetjmp(' + makeGetValue('__THREW__', 0, 'i32') + ', setjmpTable)', 'i32') + '; if ((setjmpLabel|0) > 0) { label = -1111; break } else return ' + (funcData.returnType != 'void' ? asmCoercion('0', funcData.returnType) : '') + ' } __THREW__ = threwValue = 0;\n';
14931493
}
14941494

14951495
return ret;

tests/runner.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,28 +2452,34 @@ def test_longjmp(self):
24522452
static jmp_buf buf;
24532453

24542454
void second(void) {
2455-
printf("second\n"); // prints
2456-
longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1
2455+
printf("second\n");
2456+
longjmp(buf,-1);
24572457
}
24582458

24592459
void first(void) {
2460-
second();
2461-
printf("first\n"); // does not print
2460+
printf("first\n"); // prints
2461+
longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1
24622462
}
24632463

24642464
int main() {
24652465
volatile int x = 0;
2466-
if ( ! setjmp(buf) ) {
2466+
int jmpval = setjmp(buf);
2467+
if (!jmpval) {
2468+
x++; // should be properly restored once longjmp jumps back
2469+
first(); // when executed, setjmp returns 1
2470+
printf("skipped\n"); // does not print
2471+
} else if (jmpval == 1) { // when first() jumps back, setjmp returns 1
2472+
printf("result: %d %d\n", x, jmpval); // prints
24672473
x++;
2468-
first(); // when executed, setjmp returns 0
2469-
} else { // when longjmp jumps back, setjmp returns 1
2470-
printf("main: %d\n", x); // prints
2474+
second(); // when executed, setjmp returns -1
2475+
} else if (jmpval == -1) { // when second() jumps back, setjmp returns -1
2476+
printf("result: %d %d\n", x, jmpval); // prints
24712477
}
24722478

24732479
return 0;
24742480
}
24752481
'''
2476-
self.do_run(src, 'second\nmain: 1\n')
2482+
self.do_run(src, 'first\nresult: 1 1\nsecond\nresult: 2 -1')
24772483

24782484
def test_longjmp2(self):
24792485
src = r'''

0 commit comments

Comments
 (0)