-
Notifications
You must be signed in to change notification settings - Fork 399
Open
Description
I recently added support for string concatenation in #546 , however there are some edge cases that still need to be handled. Below is a high-level overview of my implementation.
Overview: String Concatenation in JPF
When JPF encounters string concatenation (like "Hello " + name + "!"), here's the simplified execution flow:
1. Java Compilation (Background)
// Java source: "Hello " + name + "!"
// Compiles to: invokedynamic #4 makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
// With recipe: "Hello \u0001!" (\u0001 = placeholder for variable)the work flow when we encounter a string concat call site will start from here in the invokedyanmic
2. INVOKEDYNAMIC Instruction Execution
@Override
public Instruction execute(ThreadInfo ti) {
// 1. Get bootstrap method info from class
BootstrapMethodInfo bmi = enclosingClass.getBootstrapMethodInfo(bootstrapMethodIndex);
// 2. Create or retrieve cached CallSite
CallSite callSite = getOrCreateCallSite(callSiteKey, ti, enclosingClass, bmi);
// 3. Execute the CallSite
return executeCallSite(ti, callSite, bmi);
}
private CallSite createStringConcatCallSite(MethodHandles.Lookup lookup, String name, MethodType methodType) {
// 1. Extract recipe: "Hello \u0001!"
// 2. Extract constants: []
// 3. Get actual parameter types: [String.class]
// 4. Create MethodHandle that calls JPFStringConcatHelper.concatenate()
// 5. Return ConstantCallSite with the handle
}
3. String Concatenation Execution
// In INVOKEDYNAMIC.java
private Instruction executeStringConcatenation(ThreadInfo ti, MethodHandle target, MethodType type, BootstrapMethodInfo bmi) {
// 1. Pop arguments from JPF operand stack: ["World"]
// 2. Convert JPF ElementInfo objects to Java objects
// 3. Call MethodHandle: target.invokeWithArguments(args)
// 4. Get result: "Hello World!"
// 5. Push result back to JPF stack as new String ElementInfo
}
4. Actual Concatenation Logic
// In JPFStringConcatHelper.java
public static String concatenate(String recipe, Class<?>[] argTypes, Object[] constants, Object... args) {
// 1. Parse recipe: "Hello \u0001!"
// 2. For each character:
// - Regular char: append to result
// - \u0001: append next argument from args[]
// - \u0002: append constant from constants[]
// 3. Return: "Hello World!"
}
This implementation allows JPF to correctly model Java 9+ string concatenation without breaking compatibility with JPF's internal object representation system.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels