drccrd opened a new issue, #7306:
URL: https://github.com/apache/incubator-kie/issues/7306

   A `declare enum` with 128 or more literals builds without errors, but the 
generated class cannot be initialized: the first use of the enum throws 
`ExceptionInInitializerError` caused by `NegativeArraySizeException`. 127 
literals work.
   
   Reproducer
   
   ```java
   for (int n : new int[] {127, 128, 200}) {
       StringBuilder drl = new StringBuilder("package p;\ndeclare enum Big\n");
       for (int i = 0; i < n; i++) {
           drl.append(i == 0 ? "  " : ", ").append("V").append(i);
       }
       drl.append(";\nend\n");
       KieBase kbase = new KieHelper().addContent(drl.toString(), 
ResourceType.DRL).build();
       try {
           Class<?> big = Class.forName("p.Big", true, ((InternalRuleBase) 
kbase).getRootClassLoader());
           System.out.println(n + " literals: OK, values().length = " + 
big.getEnumConstants().length);
       } catch (Throwable t) {
           System.out.println(n + " literals: " + t + ", caused by " + 
t.getCause());
       }
   }
   ```
   
   ```
   127 literals: OK, values().length = 127
   128 literals: java.lang.ExceptionInInitializerError, caused by 
java.lang.NegativeArraySizeException: -128
   200 literals: java.lang.ExceptionInInitializerError, caused by 
java.lang.NegativeArraySizeException: -56
   ```
   
   Drools 10.2.0, Java 17.0.2. The code below is unchanged on `main` (421155b).
   
   Root cause
   
   `AsmUtil.pushInt` emits `ICONST_0` to `ICONST_5` and falls back to `BIPUSH` 
for every other value 
([AsmUtil.java#L675-L691](https://github.com/apache/incubator-kie/blob/421155b01389a283db94be6dc00d7e64f7ac68c7/drools-mvel/src/main/java/org/drools/mvel/asm/AsmUtil.java#L675-L691)):
   
   ```java
   default : mv.visitIntInsn( Opcodes.BIPUSH, j );
   ```
   
   `BIPUSH` takes a signed byte, so 128 is encoded as `0x80` and read back as 
-128. `DefaultEnumClassBuilder` uses `pushInt` in the enum's static initializer 
for each literal's ordinal 
([#L204](https://github.com/apache/incubator-kie/blob/421155b01389a283db94be6dc00d7e64f7ac68c7/drools-mvel/src/main/java/org/drools/mvel/asm/DefaultEnumClassBuilder.java#L204)),
 for the size of the `$VALUES` array 
([#L246](https://github.com/apache/incubator-kie/blob/421155b01389a283db94be6dc00d7e64f7ac68c7/drools-mvel/src/main/java/org/drools/mvel/asm/DefaultEnumClassBuilder.java#L246))
 and for the index each literal is stored at 
([#L252](https://github.com/apache/incubator-kie/blob/421155b01389a283db94be6dc00d7e64f7ac68c7/drools-mvel/src/main/java/org/drools/mvel/asm/DefaultEnumClassBuilder.java#L252)).
 With 128 or more literals the array size wraps, and ordinals from 128 on are 
wrong as well. These three are the only callers of `pushInt`.
   
   Fix
   
   Push values in the short range with `SIPUSH` and anything larger with `LDC` 
(`mv.visitLdcInsn(j)`), as javac does. PR with a test to follow.
   
   Scope
   
   Only the classic DRL build (the ASM class builder in drools-mvel) is 
affected. The executable model generates the enum as Java source 
(`EnumGenerator`) and works. Until fixed, the workaround is to keep a declared 
enum at 127 literals or fewer.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to