Skip to main content

TEEC_FinalizeContext

Function TEEC_FinalizeContext 

Source
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_FinalizeContext(ctx: *mut TEEC_Context)
Expand description

TEEC_FinalizeContext() - 销毁保存连接信息的上下文。

该函数用于销毁已初始化的 TEE 上下文,关闭客户端应用与 TEE 之间的连接。 仅当与该上下文关联的所有会话已关闭且所有共享内存块已释放时才可调用。

@param ctx 要销毁的上下文。

Examples found in repository?
examples/cc-teec.rs (line 89)
66    fn new(uuid: &raw::TEEC_UUID) -> Result<Self> {
67        // SAFETY: `TEEC_Context` 和 `TEEC_Session` 是 POD 类型,没有无效的位模式。
68        // `mem::zeroed()` 在这里是安全的,因为这些结构体只包含整数和指针。
69        let mut ctx: Box<raw::TEEC_Context> = Box::new(unsafe { mem::zeroed() });
70        let mut session: raw::TEEC_Session = unsafe { mem::zeroed() };
71        let mut origin = 0_u32;
72
73        let res = TEEC_InitializeContext(ptr::null(), ctx.as_mut());
74        if res != raw::TEEC_SUCCESS {
75            return Err(Error::from_raw_os_error(res as i32));
76        }
77
78        let res = TEEC_OpenSession(
79            ctx.as_mut(),
80            &mut session,
81            uuid,
82            raw::TEEC_LOGIN_PUBLIC,
83            ptr::null(),
84            ptr::null_mut(),
85            &mut origin,
86        );
87
88        if res != raw::TEEC_SUCCESS {
89            TEEC_FinalizeContext(ctx.as_mut());
90            return Err(Error::from_raw_os_error(res as i32));
91        }
92
93        Ok(Self { ctx, session })
94    }
95
96    fn invoke_command(&self, cmd_id: u32, op: &mut raw::TEEC_Operation) -> Result<()> {
97        let mut origin = 0_u32;
98        let res = TEEC_InvokeCommand(&self.session as *const _ as *mut _, cmd_id, op, &mut origin);
99
100        if res != raw::TEEC_SUCCESS {
101            return Err(Error::from_raw_os_error(res as i32));
102        }
103
104        Ok(())
105    }
106
107    /// 获取 TEE Context 的可变引用
108    fn context_mut(&mut self) -> &mut raw::TEEC_Context {
109        self.ctx.as_mut()
110    }
111}
112
113impl Drop for Client_Session {
114    fn drop(&mut self) {
115        TEEC_CloseSession(&mut self.session);
116        TEEC_FinalizeContext(self.ctx.as_mut());
117    }