Skip to content

Commit 65f994e

Browse files
agattidpgeorge
authored andcommitted
py/compile: Allow NULL emitter table entries.
This commit fixes a regression introduced in 1b92bda, where a new architecture was added to mpy-cross but it had no matching native emitter exists. The result was that the architecture emitter entry point would be correctly calculated according to the native architecture index, but if the emitters entry points table was not updated to match the new number of architectures an out of bound access may be performed. Unfortunately adding RV64IMC shifted the debug emitter index further down the table, and that table wasn't updated to reflect the lack of an emitter for RV64. Adding a NULL entry there would cause a NULL pointer access as there was no need to perform any check about the emitter entry point function's validity until now. Signed-off-by: Alessandro Gatti <[email protected]>
1 parent 3d9a3e8 commit 65f994e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

py/compile.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static const emit_method_table_t *emit_native_table[] = {
103103
&emit_native_xtensa_method_table,
104104
&emit_native_xtensawin_method_table,
105105
&emit_native_rv32_method_table,
106+
NULL,
106107
&emit_native_debug_method_table,
107108
};
108109

@@ -3571,6 +3572,13 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool
35713572
case MP_EMIT_OPT_NATIVE_PYTHON:
35723573
case MP_EMIT_OPT_VIPER:
35733574
if (emit_native == NULL) {
3575+
// The check looks like this to work around a false
3576+
// warning in GCC 13 (and possibly later), where it
3577+
// assumes that the check will always fail.
3578+
if ((uintptr_t)NATIVE_EMITTER_TABLE == (uintptr_t)NULL) {
3579+
comp->compile_error = mp_obj_new_exception_msg(&mp_type_NotImplementedError, MP_ERROR_TEXT("cannot emit native code for this architecture"));
3580+
goto emit_finished;
3581+
}
35743582
emit_native = NATIVE_EMITTER(new)(&comp->emit_common, &comp->compile_error, &comp->next_label, max_num_labels);
35753583
}
35763584
comp->emit_method_table = NATIVE_EMITTER_TABLE;
@@ -3603,6 +3611,10 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool
36033611
}
36043612
}
36053613

3614+
#if MICROPY_EMIT_NATIVE
3615+
emit_finished:
3616+
#endif
3617+
36063618
if (comp->compile_error != MP_OBJ_NULL) {
36073619
// if there is no line number for the error then use the line
36083620
// number for the start of this scope

0 commit comments

Comments
 (0)