Skip to content

Commit

Permalink
dwarf_extractor.cpp: Enable limited support for C++ (bytecodealliance…
Browse files Browse the repository at this point in the history
…#3540)

While band-aid fixes like this is not plausible IMO,
some people prefer to have some debug info even if it's
partial/limited/broken. This commit partially (re)enables
C++ processing. On the other hand, do not bother to process
variables because it's known incompatible with C++.
  • Loading branch information
yamt authored and WenLY1 committed Jun 26, 2024
1 parent fa8a80a commit 4a64c4f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 50 deletions.
1 change: 1 addition & 0 deletions core/iwasm/aot/aot_reloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ typedef struct {
#define REG_COMMON_SYMBOLS \
REG_SYM(aot_set_exception_with_id), \
REG_SYM(aot_invoke_native), \
REG_SYM(aot_bounds_check), \
REG_SYM(aot_call_indirect), \
REG_SYM(aot_enlarge_memory), \
REG_SYM(aot_set_exception), \
Expand Down
15 changes: 15 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,21 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
return wasm_enlarge_memory(module_inst, inc_page_count);
}

uintptr_t
aot_bounds_check(AOTModuleInstance *module_inst, uint64 offset,
uint32 bytes)
{
WASMMemoryInstance *memory = aot_get_default_memory(module_inst);
uint64 linear_memory_size = memory->memory_data_size;

if (offset + bytes <= linear_memory_size)
{
return memory->memory_data + offset;
}
// execption
return 1;
}

bool
aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
uint32 *argv)
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ bool
aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
unsigned argc, uint32 argv[]);

uintptr_t
aot_bounds_check(AOTModuleInstance *module_inst, uint64 offset,
uint32 bytes);

/**
* Set AOT module instance exception with exception string
*
Expand Down
29 changes: 29 additions & 0 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
&& !(is_local_of_aot_value
&& aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value,
offset, bytes))) {
#if WASM_ENABLE_AOT_NATIVE_BOUNDS_CHECK != 0
LLVMValueRef param_values[3], value;
LLVMTypeRef param_types[3];
static LLVMTypeRef ret_type = 0, func_type = 0, func_ptr_type= 0;
static LLVMValueRef func;
uint32 argc = 3;

param_types[0] = INT8_PTR_TYPE;
param_types[1] = I64_TYPE;
param_types[2] = I32_TYPE;
ret_type = INT8_PTR_TYPE;

param_values[0] = func_ctx->aot_inst;
param_values[1] = offset1;
param_values[2] = I32_CONST(bytes);

if (!ret_type || !func_type)
{
printf("GET_AOT_FUNCTION\n");
GET_AOT_FUNCTION(aot_bounds_check, argc);
}

if (!(maddr = LLVMBuildCall2(comp_ctx->builder, func_type, func,
param_values, argc, "aot_bounds_check1"))) {
aot_set_last_error("llvm build call failed.");
goto fail;
}
#else
uint32 init_page_count =
comp_ctx->comp_data->memories[0].init_page_count;
if (init_page_count == 0) {
Expand Down Expand Up @@ -277,6 +305,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
offset, bytes))
goto fail;
}
#endif
}

if (!enable_segue) {
Expand Down
117 changes: 67 additions & 50 deletions core/iwasm/compilation/debug/dwarf_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,22 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
* https://github.com/bytecodealliance/wasm-micro-runtime/issues/3163
*/
LanguageType language_type = function.GetLanguage();
bool cplusplus = false;
switch (language_type) {
case eLanguageTypeC89:
case eLanguageTypeC:
case eLanguageTypeC99:
case eLanguageTypeC11:
case eLanguageTypeC17:
break;
case eLanguageTypeC_plus_plus:
case eLanguageTypeC_plus_plus_03:
case eLanguageTypeC_plus_plus_11:
case eLanguageTypeC_plus_plus_14:
case eLanguageTypeC_plus_plus_17:
case eLanguageTypeC_plus_plus_20:
cplusplus = true;
break;
default:
LOG_WARNING("func %s has unsupported language_type 0x%x",
function_name, (int)language_type);
Expand All @@ -325,28 +334,32 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
LLVMMetadataRef File = comp_ctx->debug_file; /* a fallback */

LLVMMetadataRef ParamTypes[num_function_args + 1];

ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);

for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
++function_arg_idx) {
SBType function_arg_type =
function_args.GetTypeAtIndex(function_arg_idx);

if (function_arg_type.IsValid()) {
ParamTypes[function_arg_idx + 1] =
lldb_type_to_type_dbi(comp_ctx, function_arg_type);
if (ParamTypes[function_arg_idx + 1] == NULL) {
LOG_WARNING(
"func %s arg %" PRIu32
" has a type not implemented by lldb_type_to_type_dbi",
function_name, function_arg_idx);
}
}
else {
LOG_WARNING("func %s arg %" PRIu32 ": GetTypeAtIndex failed",
size_t num_param_types = 0;

if (!cplusplus) {
num_param_types = num_function_args + 1;
ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);

for (uint32_t function_arg_idx = 0;
function_arg_idx < num_function_args; ++function_arg_idx) {
SBType function_arg_type =
function_args.GetTypeAtIndex(function_arg_idx);

if (function_arg_type.IsValid()) {
ParamTypes[function_arg_idx + 1] =
lldb_type_to_type_dbi(comp_ctx, function_arg_type);
if (ParamTypes[function_arg_idx + 1] == NULL) {
LOG_WARNING(
"func %s arg %" PRIu32
" has a type not implemented by lldb_type_to_type_dbi",
function_name, function_arg_idx);
ParamTypes[function_arg_idx + 1] = NULL;
}
}
else {
LOG_WARNING("func %s arg %" PRIu32 ": GetTypeAtIndex failed",
function_name, function_arg_idx);
ParamTypes[function_arg_idx + 1] = NULL;
}
}
}

Expand All @@ -366,7 +379,7 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
}

LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero);
DIB, File, ParamTypes, num_param_types, LLVMDIFlagZero);

auto line_entry = sc.GetLineEntry();
LLVMMetadataRef ReplaceableFunctionMetadata =
Expand All @@ -386,13 +399,6 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,

LLVMMetadataRef ParamExpression =
LLVMDIBuilderCreateExpression(DIB, NULL, 0);
auto variable_list =
function.GetBlock().GetVariables(extractor->target, true, false, false);
if (num_function_args != variable_list.GetSize()) {
LOG_ERROR(
"function args number mismatch!:value number=%d, function args=%d",
variable_list.GetSize(), num_function_args);
}

LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
Expand All @@ -412,27 +418,38 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
ParamLocation, block_curr);

for (uint32_t function_arg_idx = 0;
function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
if (variable.IsValid() && ParamTypes[function_arg_idx + 1] != NULL) {
SBDeclaration dec(variable.GetDeclaration());
auto valtype = variable.GetType();
LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
comp_ctx->context, dec.GetLine(), dec.GetColumn(),
FunctionMetadata, NULL);
const char *varname = variable.GetName();
LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
DIB, FunctionMetadata, varname, varname ? strlen(varname) : 0,
function_arg_idx + 1 + 1,
File, // starts form 1, and 1 is exenv,
dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
LLVMDIFlagZero);
LLVMValueRef Param =
LLVMGetParam(func_ctx->func, function_arg_idx + 1);
LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
ParamExpression, ParamLocation,
block_curr);
if (!cplusplus) {
auto variable_list = function.GetBlock().GetVariables(
extractor->target, true, false, false);
if (num_function_args != variable_list.GetSize()) {
LOG_ERROR("function args number mismatch!:value number=%d, "
"function args=%d",
variable_list.GetSize(), num_function_args);
}
for (uint32_t function_arg_idx = 0;
function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
if (variable.IsValid()
&& ParamTypes[function_arg_idx + 1] != NULL) {
SBDeclaration dec(variable.GetDeclaration());
auto valtype = variable.GetType();
LLVMMetadataRef ParamLocation =
LLVMDIBuilderCreateDebugLocation(
comp_ctx->context, dec.GetLine(), dec.GetColumn(),
FunctionMetadata, NULL);
const char *varname = variable.GetName();
LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
DIB, FunctionMetadata, varname,
varname ? strlen(varname) : 0, function_arg_idx + 1 + 1,
File, // starts form 1, and 1 is exenv,
dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
LLVMDIFlagZero);
LLVMValueRef Param =
LLVMGetParam(func_ctx->func, function_arg_idx + 1);
LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
ParamExpression, ParamLocation,
block_curr);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions wamr-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ if (NOT DEFINED WAMR_BUILD_PLATFORM)
set (WAMR_BUILD_PLATFORM "linux")
endif()

if (NOT WAMR_BUILD_AOT_NATIVE_BOUNDS_CHECK STREQUAL 1)
add_definitions(-DWASM_ENABLE_AOT_NATIVE_BOUNDS_CHECK=0)
else()
add_definitions(-DWASM_ENABLE_AOT_NATIVE_BOUNDS_CHECK=1)
endif()

# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
Expand Down

0 comments on commit 4a64c4f

Please sign in to comment.