compiler: Serialize debug entries through checked slices

80edc7055b1d214542c2a903f6eb96bd7d9b019dc416faff0472f773d1de0496
Alexis Sellier committed ago 1 parent db636eec
compiler/radiance.rad +13 -9
851 851
}
852 852
853 853
/// Write a data section to a file at `basePath` + `ext`.
854 854
/// Empty data truncates any stale sidecar left by an earlier build.
855 855
fn writeDataWithExt(
856 -
    data: *[u8],
856 +
    data: &[u8],
857 857
    basePath: *[u8],
858 858
    ext: *[u8]
859 859
) throws (Error) {
860 860
    let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN];
861 861
    let mut pos: u32 = 0;
870 870
}
871 871
872 872
/// Serialize debug entries and write the `.debug` file.
873 873
/// Resolves module IDs to file paths via the module graph.
874 874
/// Format per entry is `{pc: u32,  offset: u32, filePath: [u8], NULL}`.
875 -
unsafe fn writeDebugInfo(
876 -
    entries: *[types::DebugEntry],
875 +
fn writeDebugInfo(
876 +
    entries: &[types::DebugEntry],
877 877
    graph: &module::ModuleGraph,
878 878
    basePath: *[u8],
879 -
    arena: &mut alloc::Arena
879 +
    buf: &mut [u8]
880 880
) throws (Error) {
881 881
    if entries.len == 0 {
882 882
        return;
883 883
    }
884 -
    // Use remaining arena space as serialization buffer.
885 -
    let buf = alloc::remainingBuf(&mut *arena);
884 +
    // The caller supplies the serialization buffer.
886 885
    let mut pos: u32 = 0;
887 886
888 887
    for i in 0..entries.len {
889 888
        let entry = &entries[i];
890 889
        let modEntry = module::get(graph, entry.moduleId) else {
891 890
            panic "writeDebugInfo: module not found for debug entry";
892 891
        };
893 -
        set pos += try! mem::copy(&mut buf[pos..], @sliceOf(&entry.pc as *u8, 4));
894 -
        set pos += try! mem::copy(&mut buf[pos..], @sliceOf(&entry.offset as *u8, 4));
892 +
        for value in [entry.pc, entry.offset] {
893 +
            for i in 0..@sizeOf(u32) {
894 +
                set buf[pos] = (value >> (i * 8)) as u8;
895 +
                set pos += 1;
896 +
            }
897 +
        }
895 898
        set pos += try! mem::copy(&mut buf[pos..], modEntry.filePath);
896 899
897 900
        set buf[pos] = 0;
898 901
        set pos += 1;
899 902
    }
1357 1360
        throw error(&["fatal:", "failed to write output file"]);
1358 1361
    }
1359 1362
1360 1363
    // Write debug info file if enabled.
1361 1364
    if ctx.debug {
1362 -
        try writeDebugInfo(result.debugEntries, &ctx.graph, outPath, res.arena);
1365 +
        let buf = alloc::remainingBuf(&mut *res.arena);
1366 +
        try writeDebugInfo(result.debugEntries, &ctx.graph, outPath, &mut buf[..]);
1363 1367
    }
1364 1368
    pkgLog(&entryPkg, &["ok", "(", outPath, ")"]);
1365 1369
}
1366 1370
1367 1371
@default unsafe fn main(env: *sys::Env) -> i32 {