Skip to content

Commit b81db57

Browse files
Implement StackOverflowError in ParparVM
Replaced the hard assertion failure in `nativeMethods.m` with a thrown `StackOverflowError` when the recursion limit is reached. Added a safety buffer of 50 stack frames to allow the exception constructor to execute without triggering infinite recursion. Added `java.lang.StackOverflowError` to the Java API.
1 parent 42acf97 commit b81db57

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

vm/ByteCodeTranslator/src/nativeMethods.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "java_lang_NullPointerException.h"
2828
#include "java_lang_Class.h"
2929
#include "java_lang_System.h"
30+
#include "java_lang_StackOverflowError.h"
3031

3132
#if defined(__APPLE__) && defined(__OBJC__)
3233
#import <Foundation/Foundation.h>
@@ -1552,7 +1553,11 @@ void initMethodStack(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT __cn1ThisObject, int
15521553
#endif
15531554
memset(&threadStateData->threadObjectStack[threadStateData->threadObjectStackOffset], 0, sizeof(struct elementStruct) * (localsStackSize + stackSize));
15541555
threadStateData->threadObjectStackOffset += localsStackSize + stackSize;
1555-
CODENAME_ONE_ASSERT(threadStateData->callStackOffset < CN1_MAX_STACK_CALL_DEPTH - 1);
1556+
if(threadStateData->callStackOffset >= CN1_MAX_STACK_CALL_DEPTH - 1) {
1557+
JAVA_OBJECT ex = __NEW_java_lang_StackOverflowError(CN1_THREAD_STATE_PASS_SINGLE_ARG);
1558+
java_lang_StackOverflowError___INIT__(CN1_THREAD_STATE_PASS_ARG ex);
1559+
throwException(threadStateData, ex);
1560+
}
15561561
threadStateData->callStackClass[threadStateData->callStackOffset] = classNameId;
15571562
threadStateData->callStackMethod[threadStateData->callStackOffset] = methodNameId;
15581563
threadStateData->callStackOffset++;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
* This code is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 only, as
6+
* published by the Free Software Foundation. Codename One designates this
7+
* particular file as subject to the "Classpath" exception as provided
8+
* by Oracle in the LICENSE file that accompanied this code.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Codename One through http://www.codenameone.com/ if you
21+
* need additional information or have any questions.
22+
*/
23+
package java.lang;
24+
25+
/**
26+
* Thrown when a stack overflow occurs because an application recurses too deeply.
27+
*/
28+
public class StackOverflowError extends VirtualMachineError {
29+
public StackOverflowError() {
30+
}
31+
32+
public StackOverflowError(String s) {
33+
super(s);
34+
}
35+
}

0 commit comments

Comments
 (0)