Skip to content

Commit 4c4aa85

Browse files
committed
Fully interpret __attribute__((always_inline))
The Linux kernel uses tests on __builtin_constant_p that are required to evalute to true for compilationt to succeed. These can only evaluate to true when inlining is actually done (and expressions are simplified).
1 parent 9226175 commit 4c4aa85

File tree

12 files changed

+167
-3
lines changed

12 files changed

+167
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// this is a GCC extension
2+
3+
#ifdef __GNUC__
4+
static inline __attribute__((always_inline)) _Bool
5+
__is_kfree_rcu_offset(unsigned long offset)
6+
{
7+
return offset < 4096;
8+
}
9+
10+
static inline __attribute__((always_inline)) void
11+
kfree_rcu(unsigned long offset)
12+
{
13+
// immediate use of a constant as argument to __builtin_constant_p
14+
((void)sizeof(char[1 - 2 * !!(!__builtin_constant_p(offset))]));
15+
// inlining required to turn the array size into a compile-time constant
16+
((void)sizeof(char[1 - 2 * !!(!__is_kfree_rcu_offset(offset))]));
17+
}
18+
19+
static inline __attribute__((always_inline)) void unused(unsigned long offset)
20+
{
21+
// this would not be constant as it's never used, the front-end needs to
22+
// discard it
23+
((void)sizeof(char[1 - 2 * !!(!__builtin_constant_p(offset))]));
24+
}
25+
#endif
26+
27+
int main()
28+
{
29+
#ifdef __GNUC__
30+
kfree_rcu(42);
31+
#endif
32+
33+
return 0;
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$
9+
--
10+
The static asserts (arrays that may have a negative size if the assertion fails)
11+
can only be evaluated if always_inline is correctly applied.

src/ansi-c/ansi_c_convert_type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ void ansi_c_convert_typet::read_rec(const typet &type)
171171
c_storage_spec.is_weak=true;
172172
else if(type.id() == ID_used)
173173
c_storage_spec.is_used = true;
174+
else if(type.id() == ID_always_inline)
175+
c_storage_spec.is_always_inline = true;
174176
else if(type.id()==ID_auto)
175177
{
176178
// ignore

src/ansi-c/ansi_c_declaration.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void ansi_c_declarationt::output(std::ostream &out) const
8181
out << " is_extern";
8282
if(get_is_static_assert())
8383
out << " is_static_assert";
84+
if(get_is_always_inline())
85+
out << " is_always_inline";
8486
out << "\n";
8587

8688
out << "Type: " << type().pretty() << "\n";
@@ -164,6 +166,9 @@ void ansi_c_declarationt::to_symbol(
164166
symbol.is_extern=false;
165167
else if(get_is_extern()) // traditional GCC
166168
symbol.is_file_local=true;
169+
170+
if(get_is_always_inline())
171+
symbol.is_macro = true;
167172
}
168173

169174
// GCC __attribute__((__used__)) - do not treat those as file-local

src/ansi-c/ansi_c_declaration.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ class ansi_c_declarationt:public exprt
205205
set(ID_is_used, is_used);
206206
}
207207

208+
bool get_is_always_inline() const
209+
{
210+
return get_bool(ID_is_always_inline);
211+
}
212+
213+
void set_is_always_inline(bool is_always_inline)
214+
{
215+
set(ID_is_always_inline, is_always_inline);
216+
}
217+
208218
void to_symbol(
209219
const ansi_c_declaratort &,
210220
symbolt &symbol) const;

src/ansi-c/c_storage_spec.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void c_storage_spect::read(const typet &type)
3434
is_weak=true;
3535
else if(type.id() == ID_used)
3636
is_used = true;
37+
else if(type.id() == ID_always_inline)
38+
is_always_inline = true;
3739
else if(type.id()==ID_auto)
3840
{
3941
// ignore

src/ansi-c/c_storage_spec.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ class c_storage_spect
3636
is_inline=false;
3737
is_weak=false;
3838
is_used = false;
39+
is_always_inline = false;
3940
alias.clear();
4041
asm_label.clear();
4142
section.clear();
4243
}
4344

44-
bool is_typedef, is_extern, is_static, is_register,
45-
is_inline, is_thread_local, is_weak, is_used;
45+
bool is_typedef, is_extern, is_static, is_register, is_inline,
46+
is_thread_local, is_weak, is_used, is_always_inline;
4647

4748
// __attribute__((alias("foo")))
4849
irep_idt alias;
@@ -53,6 +54,7 @@ class c_storage_spect
5354

5455
bool operator==(const c_storage_spect &other) const
5556
{
57+
// clang-format off
5658
return is_typedef==other.is_typedef &&
5759
is_extern==other.is_extern &&
5860
is_static==other.is_static &&
@@ -61,9 +63,11 @@ class c_storage_spect
6163
is_inline==other.is_inline &&
6264
is_weak==other.is_weak &&
6365
is_used == other.is_used &&
66+
is_always_inline == other.is_always_inline &&
6467
alias==other.alias &&
6568
asm_label==other.asm_label &&
6669
section==other.section;
70+
// clang-format on
6771
}
6872

6973
bool operator!=(const c_storage_spect &other) const
@@ -81,6 +85,7 @@ class c_storage_spect
8185
is_thread_local |=other.is_thread_local;
8286
is_weak |=other.is_weak;
8387
is_used |=other.is_used;
88+
is_always_inline |= other.is_always_inline;
8489
if(alias.empty())
8590
alias=other.alias;
8691
if(asm_label.empty())

src/ansi-c/c_typecheck_base.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ void c_typecheck_baset::typecheck_declaration(
689689
declaration.set_is_typedef(full_spec.is_typedef);
690690
declaration.set_is_weak(full_spec.is_weak);
691691
declaration.set_is_used(full_spec.is_used);
692+
declaration.set_is_always_inline(full_spec.is_always_inline);
692693

693694
symbolt symbol;
694695
declaration.to_symbol(*d_it, symbol);

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/base_type.h>
1818
#include <util/c_types.h>
1919
#include <util/cprover_prefix.h>
20+
#include <util/expr_util.h>
2021
#include <util/ieee_float.h>
2122
#include <util/pointer_offset_size.h>
2223
#include <util/pointer_predicates.h>
24+
#include <util/replace_symbol.h>
2325
#include <util/simplify_expr.h>
2426
#include <util/string_constant.h>
2527

@@ -1915,7 +1917,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
19151917
if(entry!=asm_label_map.end())
19161918
identifier=entry->second;
19171919

1918-
if(symbol_table.symbols.find(identifier)==symbol_table.symbols.end())
1920+
symbol_tablet::symbolst::const_iterator sym_entry =
1921+
symbol_table.symbols.find(identifier);
1922+
1923+
if(sym_entry == symbol_table.symbols.end())
19191924
{
19201925
// This is an undeclared function.
19211926
// Is this a builtin?
@@ -1957,6 +1962,87 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
19571962
warning() << "function `" << identifier << "' is not declared" << eom;
19581963
}
19591964
}
1965+
else if(
1966+
sym_entry->second.type.get_bool(ID_C_inlined) &&
1967+
sym_entry->second.is_macro && sym_entry->second.value.is_not_nil())
1968+
{
1969+
// calling a function marked as always_inline
1970+
const symbolt &func_sym = sym_entry->second;
1971+
const code_typet &func_type = to_code_type(func_sym.type);
1972+
1973+
replace_symbolt replace;
1974+
1975+
const code_typet::parameterst &parameters = func_type.parameters();
1976+
auto p_it = parameters.begin();
1977+
for(const auto &arg : expr.arguments())
1978+
{
1979+
if(p_it == parameters.end())
1980+
{
1981+
// we don't support varargs with always_inline
1982+
err_location(f_op);
1983+
error() << "function call has additional arguments, "
1984+
<< "cannot apply always_inline" << eom;
1985+
throw 0;
1986+
}
1987+
1988+
irep_idt p_id = p_it->get_identifier();
1989+
if(p_id.empty())
1990+
{
1991+
p_id = id2string(func_sym.base_name) + "::" +
1992+
id2string(p_it->get_base_name());
1993+
}
1994+
replace.insert(p_id, arg);
1995+
1996+
++p_it;
1997+
}
1998+
1999+
if(p_it != parameters.end())
2000+
{
2001+
err_location(f_op);
2002+
error() << "function call has missing arguments, "
2003+
<< "cannot apply always_inline" << eom;
2004+
throw 0;
2005+
}
2006+
2007+
codet body = to_code(func_sym.value);
2008+
replace(body);
2009+
2010+
side_effect_exprt side_effect_expr(
2011+
ID_statement_expression, func_type.return_type());
2012+
body.make_block();
2013+
2014+
// simulates parts of typecheck_function_body
2015+
typet cur_return_type = return_type;
2016+
return_type = func_type.return_type();
2017+
typecheck_code(body);
2018+
return_type.swap(cur_return_type);
2019+
2020+
// replace final return by an ID_expression
2021+
codet &last = to_code_block(body).find_last_statement();
2022+
2023+
if(last.get_statement() == ID_return)
2024+
last.set_statement(ID_expression);
2025+
2026+
// NOLINTNEXTLINE(whitespace/braces)
2027+
const bool has_returns = has_subexpr(body, [&](const exprt &e) {
2028+
return e.id() == ID_code && to_code(e).get_statement() == ID_return;
2029+
});
2030+
if(has_returns)
2031+
{
2032+
// we don't support multiple return statements with always_inline
2033+
err_location(last);
2034+
error() << "function has multiple return statements, "
2035+
<< "cannot apply always_inline" << eom;
2036+
throw 0;
2037+
}
2038+
2039+
side_effect_expr.copy_to_operands(body);
2040+
typecheck_side_effect_statement_expression(side_effect_expr);
2041+
2042+
expr.swap(side_effect_expr);
2043+
2044+
return;
2045+
}
19602046
}
19612047

19622048
// typecheck it now

src/ansi-c/parser.y

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ extern char *yyansi_ctext;
150150
%token TOK_GCC_ATTRIBUTE_DESTRUCTOR "destructor"
151151
%token TOK_GCC_ATTRIBUTE_FALLTHROUGH "fallthrough"
152152
%token TOK_GCC_ATTRIBUTE_USED "used"
153+
%token TOK_GCC_ATTRIBUTE_ALWAYS_INLINE "always_inline"
153154
%token TOK_GCC_LABEL "__label__"
154155
%token TOK_MSC_ASM "__asm"
155156
%token TOK_MSC_BASED "__based"
@@ -1547,6 +1548,8 @@ gcc_type_attribute:
15471548
{ $$=$1; set($$, ID_destructor); }
15481549
| TOK_GCC_ATTRIBUTE_USED
15491550
{ $$=$1; set($$, ID_used); }
1551+
| TOK_GCC_ATTRIBUTE_ALWAYS_INLINE
1552+
{ $$=$1; set($$, ID_always_inline); }
15501553
;
15511554

15521555
gcc_attribute:

0 commit comments

Comments
 (0)