-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.sh
More file actions
executable file
·1551 lines (1378 loc) · 65.5 KB
/
verify.sh
File metadata and controls
executable file
·1551 lines (1378 loc) · 65.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Default values
USE_DOCKER=false
USE_LLM=false
VALIDATE_TRANSLATION=false
VALIDATION_MODE="partial"
EXPLAIN_VIOLATION=false
FAST_MODE=false
TEST_FUNCTION=false
TEMP_DIR=""
ESBMC_EXTRA_OPTS=""
ESBMC_EXECUTABLE="esbmc"
#LLM_MODEL="openrouter/anthropic/claude-3.7-sonnet"
#LLM_MODEL="openrouter/qwen/qwen3-coder-plus"
#LLM_MODEL="openai/mlx-community/GLM-4.5-Air-4bit"
LLM_MODEL="openrouter/z-ai/glm-4.6"
TEST_FUNCTION_NAME=""
TRANSLATION_MODE=""
USE_ANALYSIS=false
LIST_TEST_FUNCTIONS=""
ANALYZED_FUNCTIONS=""
DIRECT_TRANSLATION=false # Flag for direct translation mode
MULTI_FILE_MODE=false # Flag for multi-file mode
INPUT_FILES=() # Array to store multiple input files
MAIN_FILE="" # Main file to be verified
FORCE_CONVERT=false # Flag to force conversion of all lines/functions
USE_LOCAL_LLM=false # Flag for using local LLM via aider.sh
C_FILE_MODE=false # Flag for processing .c files directly
# Prompt file paths
SOURCE_INSTRUCTION_FILE="prompts/python_prompt.txt"
VALIDATION_INSTRUCTION_FILE="prompts/validation_prompt.txt"
EXPLANATION_INSTRUCTION_FILE="prompts/explanation_prompt.txt"
MULTI_FILE_INSTRUCTION_FILE="prompts/multi_file_prompt.txt"
# Run aider from venv
run_aider() {
if [ "$USE_LOCAL_LLM" = true ]; then
# Set environment variables for local LLM
export OPENAI_API_KEY=dummy
export OPENAI_API_BASE=http://localhost:8080/v1
echo "Using local LLM with OPENAI_API_BASE=$OPENAI_API_BASE"
fi
if [ -d "$OLD_PWD/venv" ]; then
PYTHON_BIN="$OLD_PWD/venv/bin/python"
AIDER_BIN="$OLD_PWD/venv/bin/aider"
if [ -f "$AIDER_BIN" ]; then
"$AIDER_BIN" "$@"
else
"$PYTHON_BIN" -m aider "$@"
fi
else
echo "Warning: Virtual environment not found, using system aider"
aider "$@"
fi
}
show_usage() {
echo "Usage: ./verify.sh [--docker] [--llm] [--image IMAGE_NAME | --container CONTAINER_ID] [--esbmc-opts \"ESBMC_OPTIONS\"] [--esbmc-exec EXECUTABLE] [--model MODEL_NAME] [--translate MODE] [--function FUNCTION_NAME] [--explain] [--fast] [--validate-translation MODE] [--analyze] [--direct] [--multi-file MAIN_FILE] [--force-convert] [--local-llm] [--c-file] <filename> [<filename2> <filename3> ...]"
echo "Options:"
echo " --docker Run ESBMC in Docker container"
echo " --image IMAGE_NAME Specify Docker image (default: esbmc)"
echo " --container ID Specify existing container ID"
echo " --esbmc-opts OPTS Additional ESBMC options (in quotes)"
echo " --esbmc-exec EXECUTABLE Specify custom ESBMC executable path (default: esbmc)"
echo " --function FUNCTION_NAME Test function mode (adds --function)"
echo " --model MODEL_NAME Specify LLM model (default: openrouter/anthropic/claude-3.7-sonnet)"
echo " --translate MODE Set translation mode (fast|reasoning)"
echo " fast: Use Gemini for quick translations"
echo " reasoning: Use DeepSeek for complex translations"
echo " --validate-translation MODE Validate and fix translated code (partial|complete)"
echo " partial: Basic validation of syntax and structure"
echo " complete: Ensure full functional equivalence"
echo " --explain Explain ESBMC violations in terms of source code"
echo " --fast Enable fast mode (adds --unwind 10 --no-unwinding-assertions)"
echo " --analyze Analyze and test functions that may have errors"
echo " --direct Use direct LLM translation (Python to C) without shedskin"
echo " --multi-file MAIN_FILE Verify multiple files with MAIN_FILE as entry point"
echo " (Can be used with or without --llm)"
echo " Can also accept a glob pattern like '*.py' or 'src/*.py'"
echo " --force-convert Force conversion of all functions with reasonable implementations"
echo " --model MODEL_NAME Specify LLM model for both cloud and local LLMs"
echo " Examples: openrouter/anthropic/claude-3-sonnet"
echo " openrouter/google/gemini-2.0-flash-001"
echo " openai/gpt-4"
echo " local-model-name (use with --local-llm)"
echo " --local-llm Use local LLM via aider.sh (sets OPENAI_API_KEY=dummy and OPENAI_API_BASE=http://localhost:8080/v1)"
echo " Use --model to specify which local model to use"
echo " --c-file Process .c files directly without conversion (for debugging)"
exit 1
}
analyze_code_for_errors() {
local input_file=$1
local temp_file=$(mktemp)
local functions_to_test=""
exec 1>&1
echo "Analyzing code for potential errors..." >&2
{
echo "Analyze the following code and identify functions that might contain errors."
echo ""
echo "Return ONLY a comma-separated list of function names that should be tested."
echo "Return the exact name of the function "
echo "Do NOT include any other text, explanations, or formatting."
echo ""
echo "=== SOURCE CODE ==="
cat "$input_file"
} > "$temp_file"
# Get raw output and create a list of potential function names
OUTPUT=$(run_aider --no-git --no-show-model-warnings --no-pretty \
--model "$LLM_MODEL" --yes --message-file "$temp_file" | tee /dev/tty)
sync
rm "$temp_file"
echo "$OUTPUT"
}
print_esbmc_cmd() {
local cmd=$1
echo "----------------------------------------"
echo "ESBMC command to be executed:"
echo "$cmd"
echo "----------------------------------------"
}
check_threading() {
local file=$1
if grep -qE "pthread|Thread|threading|goroutine|java.lang.Thread" "$file"; then
return 0
else
return 1
fi
}
validate_translation() {
local original_file=$1
local converted_file=$2
local validation_mode=$3
local attempt=1
local success=false
echo "Validating translation in $validation_mode mode..."
local VALIDATION_LOG=$(mktemp)
local COMBINED_FILE=$(mktemp)
while [ "$success" = false ]; do
echo "Translation attempt $attempt..."
if [ "$USE_ANALYSIS" = true ]; then
local analysis_message="3. Pay special attention to these potentially problematic functions:\n"
ANALYZED_FUNCTIONS=$(analyze_code_for_errors "$input_file" | tr -d '[:space:]')
if [ ! -z "$ANALYZED_FUNCTIONS" ]; then
for func in $(echo "$ANALYZED_FUNCTIONS" | tr ',' ' '); do
if [[ $func =~ ^[a-zA-Z0-9_]+$ ]]; then
analysis_message+=" - Ensure function '$func' is correctly converted:\n"
analysis_message+=" * Same function name preserved in C\n"
analysis_message+=" * Equivalent parameter types and return type\n"
analysis_message+=" * All function logic maintained exactly\n"
analysis_message+=" * The function converts to c must be the same even the content is very important \n"
fi
done
fi
fi
{
echo "=== TRANSLATION STATUS REQUEST ==="
echo "Please review the current translation state and:"
echo "1. Implement any missing functions if needed"
echo "2. Fix any compilation errors in the current code"
echo "$analysis_message"
echo ""
echo "=== ORIGINAL CODE ==="
cat "$original_file"
echo -e "\n=== CURRENT TRANSLATION ==="
cat "$converted_file"
} > "$COMBINED_FILE"
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$VALIDATION_INSTRUCTION_FILE" \
--read "$COMBINED_FILE" "$converted_file"
echo "Checking if code compiles..."
if [ "$USE_DOCKER" = true ]; then
CMDRUN="docker run --rm -v $(pwd):/workspace -w /workspace $DOCKER_IMAGE esbmc"
# Run esbmc in the container with current directory mounted
CMRUN='docker exec -w "/workspace" "${CONTAINER_NAME}" $ESBMC_EXECUTABLE "$@"'
else
CMDRUN="esbmc"
fi
if [ "$USE_DOCKER" = true ]; then
# For Docker, we need to capture the exit code differently
$CMDRUN --parse-tree-only "$converted_file" 2>/dev/null
docker_exit_code=$?
if [ $docker_exit_code -eq 0 ]; then
echo "Compilation successful"
success=true
else
echo "Compilation failing on attempt $attempt (exit code: $docker_exit_code) - will retry with fixes..."
echo "Requesting LLM to fix compilation errors and try again..."
sleep 1
fi
else
# For local execution, use the original method
if $CMDRUN --parse-tree-only "$converted_file" 2>/dev/null; then
echo "Compilation successful"
success=true
else
echo "Compilation failing on attempt $attempt - will retry with fixes..."
echo "Requesting LLM to fix compilation errors and try again..."
sleep 1
fi
fi
((attempt++))
done
rm -f "$VALIDATION_LOG" "$COMBINED_FILE"
return 0
}
attempt_llm_conversion() {
local input_file=$1
local output_file=$2
local max_attempts=10
local attempt=1
local success=false
local file_extension="${input_file##*.}"
local TEMP_PROMPT="$TEMP_DIR/aider_prompt.txt"
if [ "$USE_ANALYSIS" = true ]; then
local analysis_message="6. Pay special attention to these potentially problematic functions:\n"
ANALYZED_FUNCTIONS=$(analyze_code_for_errors "$input_file" | tr -d '[:space:]')
if [ ! -z "$ANALYZED_FUNCTIONS" ]; then
for func in $(echo "$ANALYZED_FUNCTIONS" | tr ',' ' '); do
if [[ $func =~ ^[a-zA-Z0-9_]+$ ]]; then
analysis_message+=" - Ensure function '$func' is correctly converted:\n"
analysis_message+=" * Same function name preserved in C\n"
analysis_message+=" * Equivalent parameter types and return type\n"
analysis_message+=" * All function logic maintained exactly\n"
analysis_message+=" * The function converts to c must be the same even the content is very important \n"
fi
done
fi
fi
{
if [ "$DIRECT_TRANSLATION" = true ] && [ "$file_extension" = "py" ]; then
echo "Convert the following Python code directly to C code without using any intermediate steps."
else
echo "Convert the following ${file_extension} code to C code that can be verified by ESBMC."
fi
echo "Ensure the core functionality and behavior is preserved."
echo "The converted code should:"
echo "1. Maintain all essential logic and algorithms"
echo "2. Handle memory management appropriately"
echo "3. Use equivalent C data structures and types"
echo "4. Preserve any concurrent/parallel behavior"
echo "5. Include necessary headers and dependencies"
echo "6. CRITICAL: NEVER define ESBMC-specific functions like __ESBMC_assume, __ESBMC_assert, etc."
echo " ESBMC provides its own headers and definitions for these functions"
echo "7. Do NOT include any ESBMC internal headers or definitions"
if [ "$TEST_FUNCTION" = true ]; then
echo "6. Ensure the function '$TEST_FUNCTION_NAME' is correctly converted with:"
echo " - Same function name preserved in C"
echo " - Equivalent parameter types and return type"
echo " - All function logic maintained exactly"
fi
if [ ! -z "$analysis_message" ]; then
echo -e "$analysis_message"
fi
if [ "$FORCE_CONVERT" = true ]; then
echo "IMPORTANT: Implement ALL functions with complete, reasonable implementations."
echo "Do NOT leave any function bodies empty or with just comments."
echo "For functions with missing implementations in the source:"
echo " - Infer the intended behavior from function names, parameters, and context"
echo " - Implement reasonable default behavior based on the function signature"
echo " - Add appropriate error handling and return values"
echo " - Document your implementation choices with comments"
echo " - EVERY function must have a complete implementation with actual code"
echo " - Replace ALL placeholder comments like '// Implementation of X' with actual code"
echo " - If a function modifies state, ensure the state changes are implemented"
echo " - For monitor functions, implement actual monitoring logic"
echo " - For command functions, implement the command's actual behavior"
echo " - ENSURE there is a main() function in the code that demonstrates the functionality"
echo " - Remove any comments like 'This is a basic structure' or 'You will need to fill in'"
fi
cat "$SOURCE_INSTRUCTION_FILE" 2>/dev/null
} > "$TEMP_PROMPT"
# # Add these lines after creating the temp_prompt
# echo "=== TEMP_PROMPT CONTENTS START ==="
# cat "$TEMP_PROMPT"
# echo "=== TEMP_PROMPT CONTENTS END ==="
while [ $attempt -le $max_attempts ] && [ "$success" = false ]; do
echo "Attempt $attempt of $max_attempts to generate valid C code from ${file_extension}..."
echo "Filename: $input_file"
# echo "Prompt: $TEMP_PROMPT"
# cat "$TEMP_PROMPT" # Add this line to show the actual contents
if [ $attempt -eq 1 ]; then
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$TEMP_PROMPT" --read "$input_file" "$output_file"
else
# Include error information in subsequent attempts
if [ -f "$TEMP_DIR/esbmc_error_$((attempt-1)).txt" ]; then
{
echo ""
echo "=== PREVIOUS ATTEMPT ERROR ==="
echo "The previous compilation failed with these errors:"
cat "$TEMP_DIR/esbmc_error_$((attempt-1)).txt"
echo ""
echo "=== FIX INSTRUCTIONS ==="
echo "Please fix these specific errors:"
echo "1. If you see 'conflicting types for __ESBMC_assume' or similar ESBMC function errors:"
echo " - REMOVE any definitions of __ESBMC_assume, __ESBMC_assert, or other ESBMC functions"
echo " - These are provided by ESBMC itself and should NOT be defined"
echo " - Only include standard C headers like <assert.h>, <stdlib.h>, etc."
echo "2. If you see syntax errors, missing semicolons, or other C compilation errors:"
echo " - Fix the syntax issues in the code"
echo " - Ensure all statements end with semicolons"
echo " - Check for unmatched braces or parentheses"
echo "3. If you see 'undefined reference' errors:"
echo " - Add missing function implementations"
echo " - Ensure all declared functions are defined"
echo "4. Fix any other compilation errors shown above"
echo "5. Ensure the code compiles cleanly without ESBMC-specific definitions"
echo ""
} >> "$TEMP_PROMPT"
fi
if [ "$USE_DOCKER" = true ]; then
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --test --auto-test \
--test-cmd "docker run --rm -v $(pwd):/workspace -w /workspace $DOCKER_IMAGE esbmc --parse-tree-only $output_file" \
--yes --message-file "$TEMP_PROMPT" --read "$input_file" "$output_file"
else
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --test --auto-test \
--test-cmd "esbmc --parse-tree-only $output_file" \
--yes --message-file "$TEMP_PROMPT" --read "$input_file" "$output_file"
fi
fi
if [ "$USE_DOCKER" = true ]; then
CMDRUN="docker run --rm -v $(pwd):/workspace -w /workspace $DOCKER_IMAGE esbmc"
else
CMDRUN="$ESBMC_EXECUTABLE"
fi
# Check if the ESBMC executable is a Docker wrapper
if [[ "$ESBMC_EXECUTABLE" == *"docker"* || "$ESBMC_EXECUTABLE" == *"esbmc-mac"* ]]; then
# For Docker-based execution, we need to capture the exit code differently
echo "=== VERIFY.SH DEBUG INFO ===" >&2
echo "Detected Docker-based ESBMC executable: $ESBMC_EXECUTABLE" >&2
echo "Current working directory: $(pwd)" >&2
echo "Original PWD: $OLD_PWD" >&2
# Convert relative path to absolute if needed
if [[ "$ESBMC_EXECUTABLE" == ./* ]]; then
CMDRUN="$OLD_PWD/$ESBMC_EXECUTABLE"
echo "Converted relative path to absolute: $CMDRUN" >&2
else
CMDRUN="$ESBMC_EXECUTABLE"
fi
echo "Output file: $output_file" >&2
echo "Command to be executed: $CMDRUN --parse-tree-only \"$output_file\"" >&2
echo "File exists check: $(test -f "$output_file" && echo "EXISTS" || echo "MISSING")" >&2
echo "File size: $(test -f "$output_file" && wc -c < "$output_file" || echo "N/A") bytes" >&2
echo "ESBMC executable exists: $(test -f "$CMDRUN" && echo "YES" || echo "NO")" >&2
echo "ESBMC executable is executable: $(test -x "$CMDRUN" && echo "YES" || echo "NO")" >&2
# Show first few lines of the file for debugging
if [ -f "$output_file" ]; then
echo "First 10 lines of output file:" >&2
head -10 "$output_file" >&2
echo "--- End of file preview ---" >&2
fi
# Run with explicit error output for debugging
echo "Running command with stderr visible:" >&2
ESBMC_ERROR_OUTPUT=$("$CMDRUN" --parse-tree-only "$output_file" 2>&1)
docker_exit_code=$?
echo "ESBMC command exit code: $docker_exit_code" >&2
echo "Exit code meaning: "
case $docker_exit_code in
0) echo "SUCCESS" >&2 ;;
1) echo "GENERAL ERROR" >&2 ;;
126) echo "COMMAND NOT EXECUTABLE" >&2 ;;
127) echo "COMMAND NOT FOUND" >&2 ;;
*) echo "UNKNOWN ERROR CODE" >&2 ;;
esac
# Save the error output for the next attempt
if [ $docker_exit_code -ne 0 ]; then
echo "$ESBMC_ERROR_OUTPUT" > "$TEMP_DIR/esbmc_error_$attempt.txt"
echo "ESBMC error output saved to: $TEMP_DIR/esbmc_error_$attempt.txt"
echo "Full error output:" >&2
echo "$ESBMC_ERROR_OUTPUT" >&2
fi
if [ $docker_exit_code -eq 0 ]; then
echo "Successfully generated valid C code on attempt $attempt"
success=true
else
echo "ESBMC parse tree check failed on attempt $attempt (exit code: $docker_exit_code)"
[ $attempt -lt $max_attempts ] && echo "Retrying..." && sleep 1
fi
else
# For local execution, use the original method
echo "=== VERIFY.SH DEBUG INFO ===" >&2
echo "Using local ESBMC executable: $ESBMC_EXECUTABLE" >&2
echo "Current working directory: $(pwd)" >&2
echo "Output file: $output_file" >&2
echo "Command to be executed: $CMDRUN --parse-tree-only \"$output_file\"" >&2
# Capture error output for local execution too
ESBMC_ERROR_OUTPUT=$("$CMDRUN" --parse-tree-only "$output_file" 2>&1)
local_exit_code=$?
if [ $local_exit_code -eq 0 ]; then
echo "Successfully generated valid C code on attempt $attempt"
success=true
else
echo "ESBMC parse tree check failed on attempt $attempt (exit code: $local_exit_code)"
# Save the error output for the next attempt
echo "$ESBMC_ERROR_OUTPUT" > "$TEMP_DIR/esbmc_error_$attempt.txt"
echo "ESBMC error output saved to: $TEMP_DIR/esbmc_error_$attempt.txt"
echo "Full error output:" >&2
echo "$ESBMC_ERROR_OUTPUT" >&2
[ $attempt -lt $max_attempts ] && echo "Retrying..." && sleep 1
fi
fi
((attempt++))
done
rm -f "$TEMP_PROMPT"
return $([ "$success" = true ] && echo 0 || echo 1)
}
explain_violation() {
local source_file=$1
local c_file=$2
local violation_output=$3
local temp_file=$(mktemp)
echo "Analyzing ESBMC violation..."
{
echo "=== ORIGINAL SOURCE CODE ===" > "$temp_file"
cat "$source_file" >> "$temp_file"
echo -e "\n=== TRANSLATED C CODE ===" >> "$temp_file"
cat "$c_file" >> "$temp_file"
echo -e "\n=== ESBMC VIOLATION (LAST 30 LINES) ===" >> "$temp_file"
echo "$violation_output" | tail -n 30 >> "$temp_file"
}
echo "Requesting explanation from LLM..."
echo "----------------------------------------"
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$EXPLANATION_INSTRUCTION_FILE" \
--read "$temp_file"
rm "$temp_file"
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--docker) USE_DOCKER=true; shift ;;
--llm) USE_LLM=true; shift ;;
--analyze) USE_ANALYSIS=true; shift ;;
--direct) DIRECT_TRANSLATION=true; USE_LLM=true; shift ;;
--local-llm)
USE_LOCAL_LLM=true
USE_LLM=true
# Use default local model if no custom model was specified via --model
if [ -z "$MODEL_NAME_OVERRIDE" ]; then
LLM_MODEL="openai/mlx-community/GLM-4.5-Air-4bit"
echo "Using local LLM with default model: $LLM_MODEL"
else
echo "Using local LLM with custom model: $LLM_MODEL"
fi
shift
;;
--c-file)
C_FILE_MODE=true
echo "Processing .c file directly (no conversion)"
shift
;;
--multi-file)
[ -z "$2" ] && { echo "Error: --multi-file requires a main file name or pattern"; show_usage; }
MULTI_FILE_MODE=true
# Check if the argument is a glob pattern (contains * or ?)
if [[ "$2" == *\** || "$2" == *\?* ]]; then
GLOB_PATTERN="$2"
# Will set MAIN_FILE later after expanding the pattern
MAIN_FILE=""
else
MAIN_FILE=$(basename "$2") # Extract just the filename, not the full path
GLOB_PATTERN=""
fi
shift 2
;;
--force-convert)
FORCE_CONVERT=true
shift
;;
--validate-translation)
case "$2" in
partial|complete)
VALIDATE_TRANSLATION=true
VALIDATION_MODE="$2"
shift 2
;;
*)
echo "Error: --validate-translation requires mode (partial|complete)"
show_usage
;;
esac
;;
--explain) EXPLAIN_VIOLATION=true; shift ;;
--fast) FAST_MODE=true; shift ;;
--translate)
[ -z "$2" ] && { echo "Error: --translate requires mode (fast|reasoning)"; show_usage; }
case "$2" in
fast)
LLM_MODEL="openrouter/google/gemini-2.0-flash-001"
TRANSLATION_MODE="fast"
;;
reasoning)
LLM_MODEL="openrouter/deepseek/deepseek-r1"
TRANSLATION_MODE="reasoning"
;;
*)
echo "Error: Invalid translation mode. Use 'fast' or 'reasoning'"
show_usage
;;
esac
shift 2
;;
--esbmc-opts)
[ -z "$2" ] && { echo "Error: --esbmc-opts requires options string"; show_usage; }
ESBMC_EXTRA_OPTS="$2"
shift 2
;;
--esbmc-exec)
[ -z "$2" ] && { echo "Error: --esbmc-exec requires executable path"; show_usage; }
ESBMC_EXECUTABLE="$2"
echo "Using custom ESBMC executable: $ESBMC_EXECUTABLE"
shift 2
;;
--model)
[ -z "$2" ] && { echo "Error: --model requires a model name"; show_usage; }
LLM_MODEL="$2"
MODEL_NAME_OVERRIDE="$2" # Track that a custom model was specified
echo "Using LLM model: $LLM_MODEL"
shift 2
;;
--function)
[ -z "$2" ] && { echo "Error: --function requires a function name"; show_usage; }
TEST_FUNCTION_NAME="$2"
TEST_FUNCTION=true
shift 2
;;
--image)
[ -z "$2" ] && { echo "Error: --image requires a Docker image name"; show_usage; }
[ ! -z "$CONTAINER_ID" ] && { echo "Error: Cannot use both --image and --container"; show_usage; }
DOCKER_IMAGE="$2"
shift 2
;;
--container)
[ -z "$2" ] && { echo "Error: --container requires a container ID"; show_usage; }
[ ! -z "$DOCKER_IMAGE" ] && [ "$DOCKER_IMAGE" != "esbmc" ] && { echo "Error: Cannot use both --image and --container"; show_usage; }
CONTAINER_ID="$2"
shift 2
;;
-h|--help) show_usage ;;
*)
if [ "$MULTI_FILE_MODE" = true ]; then
INPUT_FILES+=("$1")
shift
else
[ -z "$FULLPATH" ] && FULLPATH="$1" || show_usage
shift
fi
;;
esac
done
# Validate input files
if [ "$MULTI_FILE_MODE" = true ]; then
# If a glob pattern was provided, expand it now
if [ ! -z "$GLOB_PATTERN" ]; then
echo "Expanding glob pattern: $GLOB_PATTERN"
# Save the original INPUT_FILES array
ORIGINAL_INPUT_FILES=("${INPUT_FILES[@]}")
# Clear the INPUT_FILES array
INPUT_FILES=()
# Find files matching the pattern
for file in $GLOB_PATTERN; do
if [ -f "$file" ]; then
INPUT_FILES+=("$file")
echo "Added file from pattern: $file"
fi
done
# Add any explicitly specified files
for file in "${ORIGINAL_INPUT_FILES[@]}"; do
# Check if the file is already in the INPUT_FILES array
FILE_ALREADY_ADDED=false
for added_file in "${INPUT_FILES[@]}"; do
if [ "$file" = "$added_file" ]; then
FILE_ALREADY_ADDED=true
break
fi
done
if [ "$FILE_ALREADY_ADDED" = false ]; then
INPUT_FILES+=("$file")
echo "Added explicitly specified file: $file"
fi
done
# If no files were found, show an error
[ ${#INPUT_FILES[@]} -eq 0 ] && { echo "Error: No files match the pattern $GLOB_PATTERN"; show_usage; }
# Set the main file to the first file if not specified
if [ -z "$MAIN_FILE" ]; then
MAIN_FILE=$(basename "${INPUT_FILES[0]}")
echo "Using first matched file as main: $MAIN_FILE"
fi
else
[ ${#INPUT_FILES[@]} -eq 0 ] && { echo "Error: No input files provided"; show_usage; }
fi
# Check if main file is in the input files
MAIN_FILE_FOUND=false
for file in "${INPUT_FILES[@]}"; do
if [ "$(basename "$file")" = "$MAIN_FILE" ]; then
MAIN_FILE_FOUND=true
break
fi
done
if [ "$MAIN_FILE_FOUND" = false ]; then
echo "Warning: Main file $MAIN_FILE not found by exact name in input files"
echo "Looking for main file with path..."
# Try to find the file with the full path
for file in "${INPUT_FILES[@]}"; do
if [[ "$file" == *"/$MAIN_FILE" || "$file" == *"$MAIN_FILE" ]]; then
MAIN_FILE=$(basename "$file")
MAIN_FILE_FOUND=true
echo "Found main file as: $file"
break
fi
done
# If still not found, use the first file as main
if [ "$MAIN_FILE_FOUND" = false ]; then
MAIN_FILE=$(basename "${INPUT_FILES[0]}")
echo "Using first input file as main: $MAIN_FILE"
fi
fi
# Check if all files exist
for file in "${INPUT_FILES[@]}"; do
[ ! -f "$file" ] && { echo "Error: Source file $file does not exist"; exit 1; }
done
FULLPATH="${INPUT_FILES[0]}" # Use first file for directory info
FILENAME=$(basename "$MAIN_FILE")
BASENAME="${FILENAME%.*}"
EXTENSION="${FILENAME##*.}"
DIRNAME=$(dirname "$FULLPATH")
else
[ -z "$FULLPATH" ] && show_usage
[ ! -f "$FULLPATH" ] && { echo "Error: Source file $FULLPATH does not exist"; exit 1; }
FILENAME=$(basename "$FULLPATH")
BASENAME="${FILENAME%.*}"
EXTENSION="${FILENAME##*.}"
DIRNAME=$(dirname "$FULLPATH")
fi
TEMP_DIR=$(mktemp -d)
echo "Working directory: $TEMP_DIR"
OLD_PWD=$(pwd)
# Check if prompts directory exists
[ ! -d "prompts" ] && { echo "Error: prompts directory not found"; exit 1; }
[ ! -f "prompts/explanation_prompt.txt" ] && { echo "Error: explanation_prompt.txt not found in prompts directory"; exit 1; }
# Copy prompt files to local directory first
mkdir -p prompts
cp "$SOURCE_INSTRUCTION_FILE" prompts/aider_prompt.txt 2>/dev/null
# Copy files to temp directory
cp -r module_import/* "$TEMP_DIR/" 2>/dev/null
if [ "$MULTI_FILE_MODE" = true ]; then
for file in "${INPUT_FILES[@]}"; do
if [ -f "$file" ]; then
echo "Copying $file to $TEMP_DIR/$(basename "$file")"
cp "$file" "$TEMP_DIR/$(basename "$file")"
# Verify file was copied
if [ ! -f "$TEMP_DIR/$(basename "$file")" ]; then
echo "Warning: Failed to copy $file to $TEMP_DIR/$(basename "$file")"
# Try again with absolute path
cp "$(realpath "$file")" "$TEMP_DIR/$(basename "$file")"
if [ ! -f "$TEMP_DIR/$(basename "$file")" ]; then
echo "Error: Second attempt to copy file failed"
else
echo "Successfully copied $(basename "$file") to temp directory on second attempt"
fi
else
echo "Successfully copied $(basename "$file") to temp directory"
fi
else
echo "Warning: Source file $file does not exist"
fi
done
else
cp "$FULLPATH" "$TEMP_DIR/$FILENAME"
# Verify file was copied
if [ ! -f "$TEMP_DIR/$FILENAME" ]; then
echo "Warning: Failed to copy $FULLPATH to $TEMP_DIR/$FILENAME"
# Try again with absolute path
cp "$(realpath "$FULLPATH")" "$TEMP_DIR/$FILENAME"
fi
fi
[ -f "esbmc.py" ] && cp "esbmc.py" "$TEMP_DIR/"
[ -d "prompts" ] && cp -r prompts "$TEMP_DIR/"
for file in *.hpp; do
[ -f "$file" ] && cp "$file" "$TEMP_DIR/${file}"
done
# Create multi-file prompt if it doesn't exist
if [ "$MULTI_FILE_MODE" = true ] && [ ! -f "$TEMP_DIR/prompts/multi_file_prompt.txt" ]; then
mkdir -p "$TEMP_DIR/prompts"
cat > "$TEMP_DIR/prompts/multi_file_prompt.txt" << 'EOF'
Convert the following multiple Python files into a single coherent C program.
Important guidelines:
1. Preserve the dependency structure between files
2. Maintain all function signatures and behaviors
3. Ensure proper header includes and forward declarations
4. Organize the code logically with clear separation between modules
5. Handle imports and module references correctly
6. Preserve global variables and their initialization order
7. Ensure the main entry point works correctly
The main file is specified, and all other files are dependencies.
EOF
fi
cd "$TEMP_DIR"
[ -d "$OLD_PWD/venv" ] && source "$OLD_PWD/venv/bin/activate"
[ ! -z "$TRANSLATION_MODE" ] && echo "Using translation mode: $TRANSLATION_MODE with model: $LLM_MODEL"
# Function to check for incomplete implementations and fix them
verify_complete_implementations() {
local c_file=$1
local is_main_file=${2:-true} # Default to true if not specified
local temp_file=$(mktemp)
local incomplete_found=false
echo "Checking for incomplete implementations in $c_file..."
# Check for common patterns of incomplete implementations
if grep -E "\/\/ Implementation of|\/\/ TODO|\/\/ Not implemented|{[ \t]*\/\*[ \t]*\*\/[ \t]*}|{[ \t]*\/\/.*[ \t]*}|{[ \t]*}|\/\*[ \t]*Empty implementation[ \t]*\*\/|This is a basic|You will need to fill in|You need to fill in" "$c_file" > /dev/null; then
incomplete_found=true
echo "Found incomplete implementations in $c_file"
# Create a prompt to fix incomplete implementations
{
echo "The C code has incomplete function implementations that need to be completed."
echo "Please implement ALL functions with reasonable behavior based on their names, parameters, and context."
echo ""
echo "IMPORTANT REQUIREMENTS:"
echo "1. Replace ALL placeholder comments with actual code"
echo "2. No empty function bodies or bodies with just comments"
echo "3. Implement reasonable default behavior for all functions"
echo "4. Add appropriate error handling and return values"
echo "5. If a function is supposed to modify state, implement the state changes"
echo "6. For monitor functions, implement actual monitoring logic"
echo "7. For command functions, implement the command's actual behavior"
echo "8. Document your implementation choices with comments"
if [ "$is_main_file" = true ]; then
echo "9. ENSURE there is a main() function in the code"
fi
echo "10. Remove any comments like 'This is a basic structure' or 'You will need to fill in'"
echo ""
echo "=== CURRENT CODE WITH INCOMPLETE IMPLEMENTATIONS ==="
cat "$c_file"
} > "$temp_file"
echo "Requesting LLM to complete implementations..."
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$temp_file" "$c_file"
# Verify if there are still incomplete implementations
if grep -E "\/\/ Implementation of|\/\/ TODO|\/\/ Not implemented|{[ \t]*\/\*[ \t]*\*\/[ \t]*}|{[ \t]*\/\/.*[ \t]*}|{[ \t]*}|\/\*[ \t]*Empty implementation[ \t]*\*\/|This is a basic|You will need to fill in|You need to fill in" "$c_file" > /dev/null; then
echo "WARNING: Some implementations may still be incomplete. Running a second pass..."
# Create a more forceful prompt for the second pass
{
echo "CRITICAL: The C code STILL has incomplete function implementations that MUST be completed."
echo "Every single function MUST have a complete implementation with actual code."
echo ""
echo "STRICT REQUIREMENTS:"
echo "1. Replace ALL placeholder comments with actual code"
echo "2. No empty function bodies or bodies with just comments"
echo "3. Implement reasonable default behavior for all functions"
echo "4. Add appropriate error handling and return values"
echo "5. If a function is supposed to modify state, implement the state changes"
echo "6. For monitor functions, implement actual monitoring logic"
echo "7. For command functions, implement the command's actual behavior"
echo "8. Document your implementation choices with comments"
if [ "$is_main_file" = true ]; then
echo "9. ENSURE there is a main() function in the code"
fi
echo "10. Remove any comments like 'This is a basic structure' or 'You will need to fill in'"
echo ""
echo "=== CURRENT CODE WITH INCOMPLETE IMPLEMENTATIONS ==="
cat "$c_file"
} > "$temp_file"
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$temp_file" "$c_file"
fi
else
echo "No incomplete implementations found in $c_file"
fi
# Check if main function exists (only for main files)
if [ "$is_main_file" = true ] && ! grep -q "int main" "$c_file"; then
echo "WARNING: No main function found in $c_file. Skipping main function addition to preserve original output."
fi
rm -f "$temp_file"
return 0
}
# Function to combine multiple files for LLM processing
combine_files() {
local output_file=$1
local main_file=$2
shift 2
local files=("$@")
# Check if files exist before trying to combine them
if [ ! -f "$main_file" ]; then
echo "Error: Main file $main_file not found"
return 1
fi
# Create the output file
echo "=== MAIN FILE: $(basename "$main_file") ===" > "$output_file"
echo '```python' >> "$output_file"
if [ -f "$main_file" ]; then
cat "$main_file" >> "$output_file"
else
echo "# ERROR: File $main_file not found" >> "$output_file"
fi
echo '```' >> "$output_file"
echo "" >> "$output_file"
for file in "${files[@]}"; do
if [ "$(basename "$file")" != "$(basename "$main_file")" ] && [ -f "$file" ]; then
echo "=== DEPENDENCY FILE: $(basename "$file") ===" >> "$output_file"
echo '```python' >> "$output_file"
cat "$file" >> "$output_file"
echo '```' >> "$output_file"
echo "" >> "$output_file"
elif [ ! -f "$file" ]; then
basename_file=$(basename "$file")
echo "=== DEPENDENCY FILE: $basename_file (NOT FOUND) ===" >> "$output_file"
echo '```python' >> "$output_file"
echo "# ERROR: File $basename_file not found" >> "$output_file"
echo '```' >> "$output_file"
echo "" >> "$output_file"
fi
done
}
# Function for multi-file LLM conversion
attempt_multi_file_conversion() {
local combined_file=$1
local output_file=$2
local main_file=$3
local max_attempts=10
local attempt=1
local success=false
# Always use combined.c as the output file name for consistency
local original_output_file="$output_file"
output_file="combined.c"
# Create a copy of the combined file for reference
local combined_file_copy=$(mktemp)
cp "$combined_file" "$combined_file_copy"
local TEMP_PROMPT="$TEMP_DIR/aider_prompt.txt"
# Create the prompt file with proper permissions
touch "$TEMP_PROMPT"
chmod 644 "$TEMP_PROMPT"
{
echo "Convert the following multiple Python files into a single coherent C program."
echo ""
echo "The main file is: $main_file"
echo ""
echo "Important guidelines:"
echo "1. Preserve the dependency structure between files"
echo "2. Maintain all function signatures and behaviors"
echo "3. Ensure proper header includes and forward declarations"
echo "4. Organize the code logically with clear separation between modules"
echo "5. Handle imports and module references correctly"
echo "6. Preserve global variables and their initialization order"
echo "7. Ensure the main entry point works correctly"
echo "8. COMBINE ALL FILES into a single C file - do not create multiple output files"
echo "9. Include ALL functionality from ALL input files in the output"
echo "10. CRITICAL: NEVER define ESBMC-specific functions like __ESBMC_assume, __ESBMC_assert, etc."
echo " ESBMC provides its own headers and definitions for these functions"
echo "11. Do NOT include any ESBMC internal headers or definitions"
if [ "$FORCE_CONVERT" = true ]; then
echo "10. IMPORTANT: Implement ALL functions with complete, reasonable implementations"
echo " Do NOT leave any function bodies empty or with just comments"
echo " For functions with missing implementations in the source:"
echo " - Infer the intended behavior from function names, parameters, and context"
echo " - Implement reasonable default behavior based on the function signature"
echo " - Add appropriate error handling and return values"
echo " - Document your implementation choices with comments"
echo " - EVERY function must have a complete implementation with actual code"
echo " - Replace ALL placeholder comments like '// Implementation of X' with actual code"
echo " - If a function modifies state, ensure the state changes are implemented"
echo " - For monitor functions, implement actual monitoring logic"
echo " - For command functions, implement the command's actual behavior"
echo "11. ENSURE there is a main() function in the code that demonstrates the functionality"
echo "12. Remove any comments like 'This is a basic structure' or 'You will need to fill in'"
fi
cat "$MULTI_FILE_INSTRUCTION_FILE" 2>/dev/null
} > "$TEMP_PROMPT"
while [ $attempt -le $max_attempts ] && [ "$success" = false ]; do
echo "Attempt $attempt of $max_attempts to generate valid C code from multiple Python files..."
# Don't create an empty file before running aider
if [ $attempt -eq 1 ]; then
echo "Running first attempt with combined file: $combined_file"
echo "Output will be written to: $output_file"
# Capture aider output for debugging
AIDER_OUTPUT=$(mktemp)
# Verify the prompt file exists and has content
if [ ! -s "$TEMP_PROMPT" ]; then
echo "ERROR: Prompt file is empty or does not exist"
echo "Prompt file path: $TEMP_PROMPT"
echo "Prompt file contents:"
cat "$TEMP_PROMPT"
exit 1
fi
# Verify the combined file exists and has content
if [ ! -s "$combined_file" ]; then
echo "ERROR: Combined file is empty or does not exist"
echo "Combined file path: $combined_file"
exit 1
fi
echo "Running aider with:"
echo " Prompt file: $TEMP_PROMPT"
echo " Input file: $combined_file"
echo " Output file: $(pwd)/$output_file"
# Check if we're using GLM-4.5-Air-4bit model which has known shutdown issues
if [[ "$LLM_MODEL" == *"GLM-4.5-Air-4bit"* ]]; then
echo "Using GLM-4.5-Air-4bit model with special error handling..."
# Use a temporary file to capture stderr separately
STDERR_FILE=$(mktemp)
# Run aider with stderr redirected to a separate file
run_aider --no-git --no-show-model-warnings --model "$LLM_MODEL" --yes \
--message-file "$TEMP_PROMPT" --read "$combined_file" "$(pwd)/$output_file" 2> "$STDERR_FILE" | tee "$AIDER_OUTPUT"
# Check if the specific error occurred