简体   繁体   English

SAL(源注释语言)的目的是什么,SAL 1和2之间有什么区别?

[英]What is the purpose of SAL (Source Annotation Language) and what is the difference between SAL 1 and 2?

As asked in the title: 如标题中所述:

What is the purpose of SAL (Source Annotation Language) and what is the difference between SAL 1 and SAL 2? SAL(源注释语言)的目的是什么?SAL 1和SAL 2之间有什么区别?

I understand the basics of the usage, and that is serves to highlight the purpose of each of the variables passed to functions along with various other things for static code analysis, but how much difference does it actually make (ignoring increasing clarity of parameter requirements for other programmers on the project)? 我理解使用的基础知识,这有助于突出传递给函数的每个变量的目的以及静态代码分析的各种其他内容,但它实际上有多大差异(忽略了参数要求的增加清晰度)该项目的其他程序员)?

If I was to have the following prototype: 如果我有以下原型:

_Success_(return == 1) 
int TestFunction( _In_ int* pTest, _Inopt_ char* pOptional );

This is supposed to "tell" the static analyser that the function will return 1 upon it's successful operation, that pTest is a pointer which must not be nullptr and that pOptional is a pointer which may or may not be nullptr . 这应该“告诉”静态分析器该函数在成功操作时将返回1, pTest是一个指针,它必须不是nullptr ,而pOptional是一个指针,可能是也可能不是nullptr However, can't the static analyser get this information from the function definition itself? 但是,静态分析器不能从函数定义本身获取此信息吗? Moreover, what does it do with the information it gets such as the success criteria? 此外,它如何处理获得的信息,如成功标准?

Furthermore, why is there a difference between SAL 1 and SAL 2, why did microsoft decide to change the way they named their macros (ie from __out to _Out_ and __success to _Success_ ?) 此外,为什么SAL 1和SAL 2之间存在差异,为什么microsoft决定改变他们命名宏的方式(即从__out_Out___success_Success_ ?)

I'm sorry if this is described somewhere in detail on the MSDN but I was unable to find it or any other question on StackOverflow with a detailed answer so I thought I'd ask in the hopes of having my curiosity satisfied. 我很抱歉,如果在MSDN上详细描述了这一点,但我无法在StackOverflow上找到它或任何其他问题并提供详细的答案,所以我想我会问我希望能满足我的好奇心。

Thanks in advance for your time! 在此先感谢您的时间!

I read multiple questions in your question, hopefully I got them all: 我在你的问题中读到了多个问题,希望我得到了所有问题:

Why use SAL and not just deduce from source? 为什么要使用SAL而不仅仅是从源代码中推断出来?

There are multiple answers as to the point of explicitly telling the analyzer details about parameter behavior, etc., with SAL. 关于使用SAL明确告知分析器有关参数行为等的详细信息,有多个答案。

  1. Although an analyzer can deduce parameter behavior from the implementation, it often can't tell the difference between intent and accidents of implementation. 虽然分析器可以从实现中推断出参数行为,但它通常无法区分意图和实施事故。 As a developer, if you explicitly state the intended purpose of different parameters, the analyzer can both validate that you wrote an implementation that fulfilled your intent and also that callers are using it correctly. 作为开发人员,如果您明确说明了不同参数的预期用途,分析器可以验证您编写的实现是否满足您的意图,以及调用者是否正确使用它。

  2. This gives static analyzers information about function behavior when the source code isn't available to analyze, for instance the functions declared in various header files shipped as part of Visual Studio, driver kits, etc. 当源代码不可用于分析时,这为静态分析器提供有关函数行为的信息,例如,作为Visual Studio,驱动程序工具包等一部分提供的各种头文件中声明的函数。

  3. SAL allows expression of concepts that are difficult to impossible to deduce from source code only, such as lock usage and IRQL requirements in drivers. SAL允许表达难以从源代码中推断出的概念,例如锁定使用和驱动程序中的IRQL要求。

  4. This also assists in consistency with callback functions. 这也有助于与回调函数保持一致。 Some frameworks described by the Windows headers may declare a set of callback functions, so the Windows framework will call those callback functions defined elsewhere (applications, drivers, etc.). Windows标头描述的一些框架可能会声明一组回调函数,因此Windows框架将调用其他地方定义的回调函数(应用程序,驱动程序等)。 So Windows never sees the source for the called functions, and the callback function definitions never see the callers. 因此Windows永远不会看到被调用函数的源,并且回调函数定义永远不会看到调用者。

What information does the analyzer get from Success ? 分析仪从Success获得哪些信息?

This is not really relevant in the case you wrote. 这与您撰写的案例无关。 However in cases where there are output parameters (eg Out and family), it means that if the function does not succeed, the caller cannot rely on the output annotations. 但是,如果存在输出参数(例如Out和family),则意味着如果函数不成功,则调用者不能依赖输出注释。 So for instance: 例如:

_Success_(return) bool GetASmallInt(_Out_range_(0, 10) int& an_int);

If GetASmallInt returns true, an_int will be between 0 and 10, inclusive. 如果GetASmallInt返回true,则an_int将介于0和10之间(包括0和10)。 If it returns false, no such guarantee exists, and the variable may not even have been initialized by the function. 如果它返回false,则不存在这样的保证,并且该函数甚至可能没有初始化该变量。

What is the difference between SAL 1 and SAL 2 and why were the annotations renamed from __in to _In_ ? SAL 1和SAL 2之间有什么区别,为什么注释从__in重命名为_In_

Some corner cases in the original definition of SAL (eg __in) didn't mesh with C++ well. SAL的原始定义中的一些极端情况(例如__in)没有很好地与C ++相匹配。 The new syntax started off with some newer implementation that made sure it would be consistent with the requirements of both the C and C++ grammars. 新语法从一些较新的实现开始,确保它符合C和C ++语法的要求。

The main difference between SAL 1 and SAL 2 has to do with SAL 2 being able to express a lot of concepts that SAL 1 cannot, and SAL 2 is better defined, especially with respect to C++, as noted above. SAL 1和SAL 2之间的主要区别在于SAL 2能够表达SAL 1无法表达的许多概念,而SAL 2更好地定义,特别是对于C ++,如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM