Skip to content

execfile: segmentation fault with incorrect Python vfs implementation #17841

@jepler

Description

@jepler

Port, board and/or hardware

unix port, coverage build, x86_64

MicroPython version

MicroPython v1.26.0-preview.524.g255d74b5a8.dirty on 2025-08-05; linux [GCC 12.2.0] version

Reproduction

Use the following script.

import os
import vfs

class Filesystem:
    def mount(self, readonly, mkfs):
        pass

    def open(self, file, mode):
        return None # violates vfs contract

fs = Filesystem()
vfs.mount(fs, "/test_mnt")
execfile("/test_mnt/test.py")

Expected behaviour

Prints a Python exception because the return type from open() is incorrect (not a VFS file)

Observed behaviour

Crashes with a segmentation fault

Additional Information

gdb says:

Program received signal SIGSEGV, Segmentation fault.
mp_reader_new_file (reader=reader@entry=0x7fffffffd820, 
    filename=filename@entry=876) at ../../extmod/vfs_reader.c:86
86	    const mp_stream_p_t *stream_p = mp_get_stream(file);
(gdb) where
#0  mp_reader_new_file (reader=reader@entry=0x7fffffffd820, 
    filename=filename@entry=876) at ../../extmod/vfs_reader.c:86
#1  0x00005555555c4ca3 in mp_lexer_new_from_file (filename=876)
    at ../../py/lexer.c:897
#2  0x000055555561b5bf in eval_exec_helper (n_args=1, args=0x7fffffffda50, 
    parse_input_kind=parse_input_kind@entry=MP_PARSE_SINGLE_INPUT)
    at ../../py/builtinevex.c:167
#3  0x000055555561b782 in mp_builtin_execfile (n_args=<optimized out>, 
    args=<optimized out>) at ../../py/builtinevex.c:195

possibly mp_get_stream_raise should be used instead?

Code of Conduct

Yes, I agree

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions