* [PATCH 1/2 sparse] Support the force attribute for function parameters
@ 2013-01-30 19:32 Thierry Reding
2013-01-30 19:32 ` [PATCH 2/2] Ignore address space for IS_ERR() and friends Thierry Reding
0 siblings, 1 reply; 2+ messages in thread
From: Thierry Reding @ 2013-01-30 19:32 UTC (permalink / raw)
To: linux-sparse; +Cc: Christopher Li, Greg Kroah-Hartman, linux-kernel
Functions such as IS_ERR() in the Linux kernel extract an error value
encoded in a pointer. sparse warns when a pointer with an address space
other than the default (i.e. annotated with __user, __iomem, __percpu or
__rcu) is passed to these functions. However, checking whether the
pointer represents an encoded error and extracting the value are purely
arithmetic operations on the pointer and therefore not concerned with
the pointer's address space at all.
This patch allows function parameters to be annotated with the force
attribute which will cause any differences in the address space and the
noderef attribute to be ignored.
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
---
evaluate.c | 22 +++++++++++++---------
expand.c | 4 ++--
parse.c | 2 ++
symbol.h | 5 ++++-
4 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/evaluate.c b/evaluate.c
index bebe968..d5a00e1 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -609,8 +609,9 @@ static void examine_fn_arguments(struct symbol *fn);
#define MOD_IGN (MOD_VOLATILE | MOD_CONST)
const char *type_difference(struct ctype *c1, struct ctype *c2,
- unsigned long mod1, unsigned long mod2)
+ unsigned long mod1, unsigned long mod2, unsigned long flags)
{
+ unsigned long mask = MOD_IGNORE | MOD_SIGNEDNESS;
unsigned long as1 = c1->as, as2 = c2->as;
struct symbol *t1 = c1->base_type;
struct symbol *t2 = c2->base_type;
@@ -727,7 +728,7 @@ const char *type_difference(struct ctype *c1, struct ctype *c2,
return "different argument counts";
diffstr = type_difference(&arg1->ctype,
&arg2->ctype,
- MOD_IGN, MOD_IGN);
+ MOD_IGN, MOD_IGN, 0);
if (diffstr) {
static char argdiff[80];
sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
@@ -759,9 +760,11 @@ const char *type_difference(struct ctype *c1, struct ctype *c2,
t1 = base1;
t2 = base2;
}
- if (as1 != as2)
+ if (as1 != as2 && (flags & FLAG_FORCE) == 0)
return "different address spaces";
- if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
+ if (flags & FLAG_FORCE)
+ mask |= MOD_NODEREF;
+ if ((mod1 ^ mod2) & ~mask)
return "different modifiers";
return NULL;
}
@@ -795,7 +798,7 @@ static struct symbol *evaluate_ptr_sub(struct expression *expr)
examine_pointer_target(rtype);
typediff = type_difference(<ype->ctype, &rtype->ctype,
target_qualifiers(rtype),
- target_qualifiers(ltype));
+ target_qualifiers(ltype), 0);
if (typediff)
expression_error(expr, "subtraction of different types can't work (%s)", typediff);
@@ -1056,7 +1059,7 @@ static struct symbol *evaluate_compare(struct expression *expr)
typediff = type_difference(<ype->ctype, &rtype->ctype,
target_qualifiers(rtype),
- target_qualifiers(ltype));
+ target_qualifiers(ltype), 0);
if (!typediff)
goto OK;
@@ -1170,7 +1173,7 @@ static struct symbol *evaluate_conditional_expression(struct expression *expr)
/* XXX: that should be pointer to composite */
ctype = ltype;
typediff = type_difference(<ype->ctype, &rtype->ctype,
- qual, qual);
+ qual, qual, 0);
if (!typediff)
goto Qual;
goto Err;
@@ -1347,7 +1350,8 @@ static int compatible_assignment_types(struct expression *expr, struct symbol *t
goto Cast;
}
/* It's OK if the target is more volatile or const than the source */
- typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
+ typediff = type_difference(&t->ctype, &s->ctype, 0, mod1,
+ target->ctype.flags);
if (typediff)
goto Err;
return 1;
@@ -3016,7 +3020,7 @@ static void check_duplicates(struct symbol *sym)
const char *typediff;
evaluate_symbol(next);
declared++;
- typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
+ typediff = type_difference(&sym->ctype, &next->ctype, 0, 0, 0);
if (typediff) {
sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
show_ident(sym->ident),
diff --git a/expand.c b/expand.c
index 63a9075..b76d951 100644
--- a/expand.c
+++ b/expand.c
@@ -465,9 +465,9 @@ static int compare_types(int op, struct symbol *left, struct symbol *right)
struct ctype c2 = {.base_type = right};
switch (op) {
case SPECIAL_EQUAL:
- return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN);
+ return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN, 0);
case SPECIAL_NOTEQUAL:
- return type_difference(&c1, &c2, MOD_IGN, MOD_IGN) != NULL;
+ return type_difference(&c1, &c2, MOD_IGN, MOD_IGN, 0) != NULL;
case '<':
return left->bit_size < right->bit_size;
case '>':
diff --git a/parse.c b/parse.c
index bd42180..a553570 100644
--- a/parse.c
+++ b/parse.c
@@ -1832,6 +1832,8 @@ static struct token *parameter_declaration(struct token *token, struct symbol *s
apply_modifiers(token->pos, &ctx);
sym->ctype = ctx.ctype;
sym->ctype.modifiers |= storage_modifiers(&ctx);
+ if (ctx.storage_class == SForced)
+ sym->ctype.flags |= FLAG_FORCE;
sym->endpos = token->pos;
return token;
}
diff --git a/symbol.h b/symbol.h
index 1e74579..2417496 100644
--- a/symbol.h
+++ b/symbol.h
@@ -81,12 +81,15 @@ extern struct context *alloc_context(void);
DECLARE_PTR_LIST(context_list, struct context);
+#define FLAG_FORCE 1
+
struct ctype {
unsigned long modifiers;
unsigned long alignment;
struct context_list *contexts;
unsigned int as;
struct symbol *base_type;
+ unsigned long flags;
};
struct decl_state {
@@ -266,7 +269,7 @@ extern struct symbol_list *translation_unit_used_list;
extern void access_symbol(struct symbol *);
extern const char * type_difference(struct ctype *c1, struct ctype *c2,
- unsigned long mod1, unsigned long mod2);
+ unsigned long mod1, unsigned long mod2, unsigned long flags);
extern struct symbol *lookup_symbol(struct ident *, enum namespace);
extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
--
1.8.1.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/2] Ignore address space for IS_ERR() and friends
2013-01-30 19:32 [PATCH 1/2 sparse] Support the force attribute for function parameters Thierry Reding
@ 2013-01-30 19:32 ` Thierry Reding
0 siblings, 0 replies; 2+ messages in thread
From: Thierry Reding @ 2013-01-30 19:32 UTC (permalink / raw)
To: linux-sparse; +Cc: Christopher Li, Greg Kroah-Hartman, linux-kernel
This patch ignores sparse' address_space and noderef attributes for
pointers passed to the IS_ERR() and related functions. Since they only
extract an error code or compare the pointer to an integer value, they
can safely be used on any type of pointer.
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
---
Note that in order for this to work a patched version of sparse is
required.
include/linux/err.h | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/include/linux/err.h b/include/linux/err.h
index f2edce2..92103bf 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -24,17 +24,17 @@ static inline void * __must_check ERR_PTR(long error)
return (void *) error;
}
-static inline long __must_check PTR_ERR(const void *ptr)
+static inline long __must_check PTR_ERR(const void __force *ptr)
{
return (long) ptr;
}
-static inline long __must_check IS_ERR(const void *ptr)
+static inline long __must_check IS_ERR(const void __force *ptr)
{
return IS_ERR_VALUE((unsigned long)ptr);
}
-static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
+static inline long __must_check IS_ERR_OR_NULL(const void __force *ptr)
{
return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}
@@ -46,13 +46,13 @@ static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
* Explicitly cast an error-valued pointer to another pointer type in such a
* way as to make it clear that's what's going on.
*/
-static inline void * __must_check ERR_CAST(const void *ptr)
+static inline void * __must_check ERR_CAST(const void __force *ptr)
{
/* cast away the const */
return (void *) ptr;
}
-static inline int __must_check PTR_RET(const void *ptr)
+static inline int __must_check PTR_RET(const void __force *ptr)
{
if (IS_ERR(ptr))
return PTR_ERR(ptr);
--
1.8.1.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-01-30 19:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-30 19:32 [PATCH 1/2 sparse] Support the force attribute for function parameters Thierry Reding
2013-01-30 19:32 ` [PATCH 2/2] Ignore address space for IS_ERR() and friends Thierry Reding
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).