Repository navigation
sensor: sc2315e for hi3516ev200 from source, without the write past its state - #306
Conversation
OpenIPC/firmware ships libsns_sc2315e.so for hi3516ev200 as a prebuilt binary; its source is OpenIPC's own "SC2315E Universal driver" (2021-22), which never reached this repository. This is that source, at its last commit (fa7c697, "Use new driver versioning model"), ported to the T5 headers the other EV200 drivers build with: comm_sns.h / sns_ctrl.h / gk_api_*.h through hicompat.h, HI_ types and constants as GK_, HI_DBG_ERR as MODULE_DBG_ERR. The logic is unchanged, including the exported sensor_driver() entry point the shipped binary has, so the library is a drop-in for the blob.
cmos_fps_set() wrote the frame length it computed to pstSnsState[1].astRegsInfo[0].astI2cData[19].u32RegAddr and .bUpdate. Those two lines were carried over from a disassembly of the vendor's build, whose ISP_SNS_STATE_S is 0x200 bytes longer: at its offsets 0xa58 and 0xa50 sit u32FLStd and au32FL[0]. Here each pipe has one 2156-byte ISP_SNS_STATE_S, so pstSnsState[1] is past the allocation and every frame-rate set wrote two words ~490 bytes beyond it, into whatever the heap held next. musl's allocator found the damage when the context was freed, as SIGILL in free() on a pipeline restart. They now store to u32FLStd and au32FL[0], as the stock driver's version of the function does (kept under #if 0 below it). Tested on a hi3516ev200 + SC2315E camera, with an LD_PRELOAD shim that puts the 2156-byte context flush against a PROT_NONE page: - the shipped libsns_sc2315e.so faults on start, in cmos_fps_set (str r1, [r3, #2648], r3 = the context); - this build starts, survives eight pipeline rebuilds (frame rate changed between reloads; the context freed and allocated each time), and gives the same sensor timing as the shipped one (40 ms frame interval, the encoder at the configured 15/20/25 fps) and a normal picture.
f0ace56 to
5f559f2
Compare
PR Summary by QodoAdd source-built SC2315E driver and fix frame-rate heap overrun
AI Description
Diagram
High-Level Assessment
Files changed (6)
|
Code Review by Qodo
1.
|
With the previous commit u32FLStd and au32FL[0] are live: cmos_fps_set() keeps them in full lines, twice VMAX, as the stock template does. Two places still took them for VMAX itself, harmless while the fps path was writing elsewhere: - sensor_global_init() started them at VMAX (1125), so the AE defaults read before the first frame-rate set had half the frame; the stock template starts them at VMAX * 2. - leaving pixel-calibration mode wrote u32FLStd straight to the VMAX registers, which after a frame-rate set would be twice the frame. It now writes u32FLStd >> 1. Re-tested on the hi3516ev200 + SC2315E camera under the guard-page shim: eight pipeline rebuilds at 25/20/15 fps, no fault, 40 ms sensor frame interval, encoder at the configured rate, normal exposure and picture.
|
Qodo review, finding by finding:
Re-tested db44d78 on the hi3516ev200 + SC2315E camera under the guard-page shim: eight pipeline rebuilds at 25/20/15 fps, no fault, 40 ms sensor frame interval, encoder at the configured rate, normal exposure and picture. |
OpenIPC/firmware ships
libsns_sc2315e.sofor hi3516ev200 as a prebuilt binary (general/package/hisilicon-osdrv-hi3516ev200/files/sensor/). Its source -- OpenIPC's own "SC2315E Universal driver" from 2021-22 -- never reached this repository, and the binary has a heap overrun.Commits
comm_sns.h/sns_ctrl.h/gk_api_*.hthroughhicompat.h,HI_*types and constants asGK_*). Logic unchanged, including the exportedsensor_driver()entry point the shipped binary has, so the library is a drop-in for the blob. README sensor table gains the row.cmos_fps_set()stored the frame length topstSnsState[1].astRegsInfo[0].astI2cData[19].{u32RegAddr,bUpdate}-- lines carried over from a disassembly of the vendor's build, whoseISP_SNS_STATE_Sis 0x200 bytes longer, so itsu32FLStdandau32FL[0]sit at those offsets (0xa58, 0xa50). Here a pipe has one 2156-byte state, so every frame-rate set wrote two words ~490 bytes past the allocation. musl's allocator catches the damage when the context is freed on a pipeline restart (SIGILL infree(), reported from the field on hi3516ev200 + SC2315E cameras); whether it does depends only on heap layout, so it comes and goes with unrelated builds. The stores now go tou32FLStdandau32FL[0], as the stock driver's version of the function does (kept under#if 0beside it).Testing
Built for
hi3516ev200(SDK_CODE 0x3516E200),gk7205v200andgk7205v500(XMedia compat flags): no errors or warnings.On a hi3516ev200 + SC2315E camera, with an
LD_PRELOADshim that places the 2156-byte context flush against aPROT_NONEpage:pc = libsns_sc2315e.so+0x1050(str r1, [r3, #2648],r3= the context), the write landing in the guard pageSensor timing is the same as with the shipped binary (40 ms frame interval; encoder at the configured 15/20/25 fps), and the picture is normal.
Wiring it into the firmware (building it here and dropping the blob) follows as an OpenIPC/firmware PR once this is in.