[FIXED] lookupswitch bug in kaffe.def

Robert Zawiasa bozo at bibl.u-szeged.hu
Mon Feb 22 04:12:56 PST 1999


here is a more serious bug fixed,

take this switch construct below (notice: no cases but the default):

switch (x) {
	default:
		doSomething();
}

if compiled with pizza v0.39g (that coming with kaffe):

  fine optimization works
  no bytecode emitted for the switch itself -> OK for Kaffe

if compiled with javac 1.1:

  lookupswitch generated
  with the default case
  plus a dummy fall-through case 0:	   -> still OK for Kaffe

if compiled with javac 1.2:

  lookupswitch generated but
  only with the default case -> Kaffe throws NullPointerException
				that is a SIGSEGV (dangerous runaway JIT code)

the error is in kaffevm/kaffe.def:

  the lookupswitch handling assumes that there is always at least one
  case-key/fork-address, though the vmspec allows a lookupswitch only
  with the lonely default case.

see the attachment..
-------------- next part --------------
diff -urN kaffe/kaffe/kaffevm/kaffe.def kaffe-patched/kaffe/kaffevm/kaffe.def
--- kaffe/kaffe/kaffevm/kaffe.def	Mon Jan  4 03:41:24 1999
+++ kaffe-patched/kaffe/kaffevm/kaffe.def	Mon Feb 22 11:57:17 1999
@@ -1769,29 +1769,30 @@
 
 	slot_alloctmp(mtable);
 	slot_alloctmp(tmp);
-	slot_alloctmp(tmp2);
-
-	move_label_const(tmp2, reference_table_label(7));
-	move_ref(tmp, tmp2);
-	add_ref_const(tmp, tmp, idx * switchpair_size);
-
+	move_label_const(tmp, reference_table_label(7));
+	if (idx != 0) {
+		slot_alloctmp(tmp2);
+
+		move_ref(tmp2, tmp);
+		add_ref_const(tmp, tmp, idx * switchpair_size);
+	}
 	end_sub_block();
-	set_label(LOOKUPSWITCH, 5);
-	start_sub_block();
-	load_key(mtable, tmp);
-	end_sub_block();
-	cbranch_int_eq(mtable, stack(0), reference_label(LOOKUPSWITCH, 6));
 
-	start_sub_block();
-	add_ref_const(tmp, tmp, -switchpair_size);
-	end_sub_block();
-	cbranch_ref_ne(tmp, tmp2, reference_label(LOOKUPSWITCH, 5));
+	if (idx != 0) {
+		set_label(LOOKUPSWITCH, 5);
+		start_sub_block();
+		load_key(mtable, tmp);
+		end_sub_block();
+		cbranch_int_eq(mtable, stack(0), reference_label(LOOKUPSWITCH, 6));
+
+		start_sub_block();
+		add_ref_const(tmp, tmp, -switchpair_size);
+		end_sub_block();
+		cbranch_ref_ne(tmp, tmp2, reference_label(LOOKUPSWITCH, 5));
 
-	start_sub_block();
-	add_ref_const(tmp, tmp, -switchpair_addr);
-	end_sub_block();
+		set_label(LOOKUPSWITCH, 6);
+	}
 
-	set_label(LOOKUPSWITCH, 6);
 	start_sub_block();
 	add_ref_const(tmp, tmp, switchpair_addr);
 	load_code_ref(tmp, tmp);
@@ -1802,8 +1803,8 @@
 #if defined(TRANSLATOR)
 	{
 		set_label(LOOKUPSWITCH, 7);
-		build_code_ref(&getcode(npc), pc);
 		build_key(&getcode(npc)); /* Dummy key */
+		build_code_ref(&getcode(npc), pc);
 		for (low = 1; low <= idx; low++) {
 			build_key(&getcode(npc + (low * switchpair_size)));
 			build_code_ref(&getcode(npc + (low * switchpair_size) + switchpair_addr), pc);


More information about the kaffe mailing list