Skip to content

with migrate: a function that reads or writes an extern C global, or indexes an extern incomplete array, is emitted without unsafe and the output does not check #3344

Closed

Description

@ehartford

Found 2026-11-14 while fixing the nightly's stc-drift-report (#3231). On the installed compiler (main 6e71323):

/* file.c */
#include <stdint.h>
extern const uint8_t tab[];
extern int counter;
uint32_t look(uint32_t i) { counter = counter + 1; return tab[i]; }

with migrate $PWD/file.c -o $PWD/out/file.w writes

pub fn look(__param_i: u32) -> u32:
    (counter = (((counter + 1) as i32)))
    return ((tab[__param_i] as u32))
extern let tab: *u8
extern var counter: i32

and then reports file.w does not check (3 error(s) above):

  • E0921: mutation of C global counter may race and E0921: read of C global counter may race (§31.4: a C global that is not const or thread-local is raw to touch),
  • raw pointer indexing requires unsafe context for tab[__param_i] (extern const uint8_t tab[] is rendered *u8).

Expected: the migrator emits what it cannot prove safe inside unsafe (§16.11, D159), as it does for a raw pointer parameter; the output checks. The translation unit shows neither global is thread-local and that counter is not const, so both accesses are raw by §31.4 and the function body, or the two expressions, belong in unsafe.

Where to look: the body lowering's unsafe accounting (the N unsafe count the migrator prints) does not count a DeclRef to a file-scope variable with no definition in the project, nor an index through an extern incomplete array.

Activity

  1. ehartford commented on Nov 15, 2026

    @ehartford
    ContributorAuthor

    Narrowed (installed compiler, main 8d6e104), directory mode:

    int get(int *p) { return p[1]; }                               /* -> pub unsafe fn get: correct */
    extern int counter;
    int bump(void) { counter = counter + 1; return counter; }      /* -> pub fn bump: does not check (E0921 twice) */
    extern const unsigned char tab[];
    int look(int i) { return tab[i]; }                             /* -> pub fn look: does not check (raw pointer indexing) */
    static int own = 3;
    int mine(void) { own = own + 1; return own; }                  /* -> pub fn mine over a With `var`: fine */

    Directory mode does not run the check that single-file mode runs, so with migrate dir -o out reports "4 functions translated" over output that does not compile. That silence is part of the defect.

    What decides it. A raw pointer parameter makes the function unsafe fn, and a call of an unsafe fn from a safe body is wrapped (ci_migrate_call_requires_unsafe_wrapper, exprs.unsafe_expr). Nothing does the same for a reference to a file-scope variable that this migration renders as extern var / extern let (no definition in the project: ci_migrate_project_var_owner_path is empty and the declaration is CI_VAR_DECL_ONLY), nor for an index through one rendered *T.

    Fix. In the body lowering, a DeclRef to such a variable is wrapped as the unsafe call is (exprs.unsafe_expr, skipped inside an unsafe fn body): a read or write of a non-const, non-thread-local extern, and an index or dereference of an extern rendered as a pointer. The body lowering (CImport.w) needs the project's answer to "does a unit here define it", which today is only asked when the declarations are rendered (CiMigrate.w). And directory mode runs the same check single-file mode runs, or says it did not.

  2. ehartford commented on Nov 15, 2026

    @ehartford
    ContributorAuthor

    The unsafe half is fixed in #3353: a raw extern global's read and write print inside unsafe, and an extern array of unknown size is indexed as a pointer; the migrated file checks. Open here: with migrate <dir> -o <out> does not run the check the single-file form runs, so a directory migration can still report success over output that does not compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions