Skip to content

Commit ab184fb

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 f7700c6 commit ab184fb

File tree

12 files changed

+166
-3
lines changed

12 files changed

+166
-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
@@ -155,6 +155,8 @@ void ansi_c_convert_typet::read_rec(const typet &type)
155155
c_storage_spec.is_register=true;
156156
else if(type.id()==ID_weak)
157157
c_storage_spec.is_weak=true;
158+
else if(type.id() == ID_always_inline)
159+
c_storage_spec.is_always_inline = true;
158160
else if(type.id()==ID_auto)
159161
{
160162
// 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
}

src/ansi-c/ansi_c_declaration.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ class ansi_c_declarationt:public exprt
195195
set(ID_is_weak, is_weak);
196196
}
197197

198+
bool get_is_always_inline() const
199+
{
200+
return get_bool(ID_is_always_inline);
201+
}
202+
203+
void set_is_always_inline(bool is_always_inline)
204+
{
205+
set(ID_is_always_inline, is_always_inline);
206+
}
207+
198208
void to_symbol(
199209
const ansi_c_declaratort &,
200210
symbolt &symbol) const;

src/ansi-c/c_storage_spec.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void c_storage_spect::read(const typet &type)
3232
is_register=true;
3333
else if(type.id()==ID_weak)
3434
is_weak=true;
35+
else if(type.id() == ID_always_inline)
36+
is_always_inline = true;
3537
else if(type.id()==ID_auto)
3638
{
3739
// ignore

src/ansi-c/c_storage_spec.h

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

43-
bool is_typedef, is_extern, is_static, is_register,
44-
is_inline, is_thread_local, is_weak;
44+
bool is_typedef, is_extern, is_static, is_register, is_inline,
45+
is_thread_local, is_weak, is_always_inline;
4546

4647
// __attribute__((alias("foo")))
4748
irep_idt alias;
@@ -52,16 +53,19 @@ class c_storage_spect
5253

5354
bool operator==(const c_storage_spect &other) const
5455
{
56+
// clang-format off
5557
return is_typedef==other.is_typedef &&
5658
is_extern==other.is_extern &&
5759
is_static==other.is_static &&
5860
is_register==other.is_register &&
5961
is_thread_local==other.is_thread_local &&
6062
is_inline==other.is_inline &&
6163
is_weak==other.is_weak &&
64+
is_always_inline == other.is_always_inline &&
6265
alias==other.alias &&
6366
asm_label==other.asm_label &&
6467
section==other.section;
68+
// clang-format on
6569
}
6670

6771
bool operator!=(const c_storage_spect &other) const
@@ -78,6 +82,7 @@ class c_storage_spect
7882
is_inline |=other.is_inline;
7983
is_thread_local |=other.is_thread_local;
8084
is_weak |=other.is_weak;
85+
is_always_inline |= other.is_always_inline;
8186
if(alias.empty())
8287
alias=other.alias;
8388
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
@@ -688,6 +688,7 @@ void c_typecheck_baset::typecheck_declaration(
688688
declaration.set_is_register(full_spec.is_register);
689689
declaration.set_is_typedef(full_spec.is_typedef);
690690
declaration.set_is_weak(full_spec.is_weak);
691+
declaration.set_is_always_inline(full_spec.is_always_inline);
691692

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

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Author: Daniel Kroening, [email protected]
2525
#include <util/string_constant.h>
2626
#include <util/pointer_offset_size.h>
2727
#include <util/pointer_predicates.h>
28+
#include <util/replace_symbol.h>
2829

2930
#include "builtin_factory.h"
3031
#include "c_typecast.h"
@@ -1912,7 +1913,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
19121913
if(entry!=asm_label_map.end())
19131914
identifier=entry->second;
19141915

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

19592044
// typecheck it now

src/ansi-c/parser.y

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ extern char *yyansi_ctext;
142142
%token TOK_GCC_ATTRIBUTE_NORETURN "noreturn"
143143
%token TOK_GCC_ATTRIBUTE_CONSTRUCTOR "constructor"
144144
%token TOK_GCC_ATTRIBUTE_DESTRUCTOR "destructor"
145+
%token TOK_GCC_ATTRIBUTE_ALWAYS_INLINE "always_inline"
145146
%token TOK_GCC_LABEL "__label__"
146147
%token TOK_MSC_ASM "__asm"
147148
%token TOK_MSC_BASED "__based"
@@ -1531,6 +1532,8 @@ gcc_type_attribute:
15311532
{ $$=$1; set($$, ID_constructor); }
15321533
| TOK_GCC_ATTRIBUTE_DESTRUCTOR
15331534
{ $$=$1; set($$, ID_destructor); }
1535+
| TOK_GCC_ATTRIBUTE_ALWAYS_INLINE
1536+
{ $$=$1; set($$, ID_always_inline); }
15341537
;
15351538

15361539
gcc_attribute:

0 commit comments

Comments
 (0)