You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cbmc/codedetect/simple_app.py

185 lines
5.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
简化的Flask应用用于快速启动工作空间界面
"""
import os
from flask import Flask, render_template, request, jsonify
app = Flask(__name__,
template_folder='/home/hzk/桌面/Codedetection/src/ui/templates',
static_folder='/home/hzk/桌面/Codedetection/src/ui/static')
app.secret_key = 'workspace-demo-key'
@app.route('/')
def index():
"""主页"""
return render_template('index.html')
@app.route('/upload')
def upload_page():
"""上传页面"""
return render_template('upload.html')
@app.route('/results')
def results_page():
"""结果页面"""
# 提供默认的uploaded_file数据以避免模板错误
default_file = {
'filename': '示例代码文件.c',
'size': 2048,
'path': '/tmp/example.c',
'file_id': 'demo_file',
'is_pasted': False
}
# 模拟验证结果数据
mock_results = {
'summary': {
'functions_parsed': 3,
'verification_status': '部分通过',
'verification_detail': '2/3 断言验证成功',
'mutation_score': '85%',
'processing_time': '12.5s'
},
'functions': [
{
'name': 'memcpy',
'signature': 'void* memcpy(void* dest, const void* src, size_t n)',
'params': ['dest', 'src', 'n'],
'return_type': 'void*',
'complexity': '中等',
'issues': ['可能存在缓冲区溢出风险', '需要验证指针有效性']
},
{
'name': 'strlen',
'signature': 'size_t strlen(const char* str)',
'params': ['str'],
'return_type': 'size_t',
'complexity': '',
'issues': ['需要验证输入指针非空']
},
{
'name': 'custom_function',
'signature': 'int custom_function(int input, char* buffer)',
'params': ['input', 'buffer'],
'return_type': 'int',
'complexity': '',
'issues': ['数组越界风险', '输入参数未验证', '内存泄漏可能']
}
],
'specification': {
'preconditions': [
'__requires(dest != NULL)',
'__requires(src != NULL)',
'__requires(n <= MAX_BUFFER_SIZE)',
'__requires(valid_read(src, n))',
'__requires(valid_write(dest, n))'
],
'postconditions': [
'__ensures(__result == dest)',
'__ensures(__mem_eq(dest, src, n))',
'__ensures(__pointer_ok(dest))',
'__ensures(__pointer_ok(src))'
],
'assertions': [
{
'line': 45,
'condition': 'dest != NULL',
'status': 'PASS',
'message': '目标指针非空检查通过'
},
{
'line': 46,
'condition': 'src != NULL',
'status': 'PASS',
'message': '源指针非空检查通过'
},
{
'line': 47,
'condition': 'n <= MAX_BUFFER_SIZE',
'status': 'FAIL',
'message': '缓冲区大小检查失败'
}
]
},
'mutation_results': {
'total_tests': 15,
'passed': 12,
'failed': 3,
'score': 80,
'mutations': [
{
'type': '边界值测试',
'description': '测试n=0和n=MAX_SIZE边界情况',
'status': 'PASS',
'killed': 2
},
{
'type': '空指针测试',
'description': '测试NULL指针输入',
'status': 'FAIL',
'killed': 0
},
{
'type': '内存重叠测试',
'description': '测试src和dest重叠情况',
'status': 'PARTIAL',
'killed': 1
}
]
},
'quality_metrics': {
'completeness': 85,
'correctness': 78,
'readability': 92,
'maintainability': 80,
'security': 75,
'overall_score': 82
}
}
return render_template('results.html',
uploaded_file=default_file,
results=mock_results)
@app.route('/workspace')
def workspace_page():
"""工作空间页面"""
# Calculate WebSocket and API URLs
ws_scheme = 'wss' if request.scheme == 'https' else 'ws'
ws_url = f"{ws_scheme}://{request.host}/ws/workspace"
api_base_url = f"{request.scheme}://{request.host}/api"
return render_template('workspace.html',
ws_url=ws_url,
api_base_url=api_base_url)
@app.route('/api/status')
def api_status():
"""API状态端点"""
return jsonify({
'status': 'running',
'version': '0.1.0',
'services': {
'web': 'online',
'parser': 'ready',
'llm': 'configured',
'cbmc': 'ready'
}
})
if __name__ == '__main__':
print("🚀 CodeDetect 工作空间服务器启动中...")
print("📍 访问地址:")
print(" - 主页: http://localhost:8080")
print(" - 工作空间: http://localhost:8080/workspace")
print(" - 上传页面: http://localhost:8080/upload")
print("🔧 按Ctrl+C停止服务器")
app.run(
host='0.0.0.0',
port=8080,
debug=True,
threaded=True
)