Page 1 of 2
Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 8:27 am
by Virtlink
Hello again,
I have this strangest problem. After hours and hours of debugging, I finally traced it down to this: when I have my test function test() in the same code file as my printf(const char *format, ...) function, then the pointers, arguments and values get garbled. I encountered this problem while writing the strchr() function, and reduced it to the following test function.
In one code file, I have the following:
Code: Select all
#include <stdarg.h> // va_start(), va_end(), va_arg()
#include <scrn.h> // puts()
#include <string.h> // vsprintf()
void test()
{
void * i = (void *)0xDEADBEEF;
printf("i = 0x%p\n", i);
}
long printf(const char *format, ...)
{
va_list args;
int i;
char buffer[1024];
va_start(args, format);
i = vsprintf(buffer, format, args);
va_end(args);
puts(buffer); scrn.c
return i;
}
When I call test(), I it prints:
When I put the test() function in ANY other file which doesn't contain the printf() function, it prints:
(As it should.)
I have used this printf() function without problems for the past month, so it should work. I removed almost all code from my OS except for the GDT, IDT, IRQ, ISRS, screen and printf() code and associated functions (meaning that paging is disabled, no malloc or heap, no thread or processes or scheduling or timer or keyboard interrupts), and the problem persists. I used an older working build and put the test() function in the file containing the printf() function, and the problem persists. I move the test function OR the printf function to any other file, and the problem is solved. The problem also occurs with arguments to the test function, when the test function has any other name, when the test function returns any value. I have increased the stack to enormous sizes and the problem is still there. I really don't get it.
Here's the code from the stdarg.h file:
Code: Select all
#ifndef __STDARG_H
#define __STDARG_H
// Variable argument list.
typedef char * va_list;
#define __va_rounded_size(TYPE) \
(((sizeof(TYPE) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))
#define va_start(AP, LASTARG) \
(AP = ((char *)&(LASTARG) + __va_rounded_size(LASTARG)))
void va_end(va_list);
#define va_end(AP)
#define va_arg(AP, TYPE) \
(AP += __va_rounded_size(TYPE), \
*((TYPE *)(AP - __va_rounded_size(TYPE))))
#endif // __STDARG_H
Any help is really really appreciated
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 8:40 am
by albeva
I ran into similar wierd problem. It has something todo with var_arg's getting messed up. Here's what I had to do:
Code: Select all
void printAddress (void * p)
{
//printf("Address=%x\n", p);
// THIS IS HACKING. For some reason printf directly won't work.
// screws up the arguments somehow. Wierd
vsprintf(printbuf, "Address=0x%p\n", (va_list)&p);
}
I suggest you call vsprintf directly. Although I wonder what the problem is myswlf as well.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 8:45 am
by PHPnerd
These are my defines, try them.
Code: Select all
typedef unsigned char *va_list;
#define STACKITEM int
#define VA_SIZE(TYPE) \
((sizeof(TYPE) + sizeof(STACKITEM) - 1) \
& ~(sizeof(STACKITEM) - 1))
#define va_start(AP, LASTARG) \
(AP=((va_list)&(LASTARG) + VA_SIZE(LASTARG)))
#define va_end(AP)
#define va_arg(AP, TYPE) \
(AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE))))
#ifndef NULL
#define NULL 0
#endif
// PHP
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 9:10 am
by albeva
PHPnerd Your VAR_ARG won't help.
However the problem is if your function is in the *same* file the compiler will try to inline printf call. It seems that this inlining couses the trouble. define your printf as follows
Code: Select all
extern int printf(const char *fmt, ...)
__attribute__((format(printf, 1, 2)))
__attribute__((noinline));
Fixed this issue for me
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 9:29 am
by Virtlink
albeva wrote:However the problem is if your function is in the *same* file the compiler will try to inline printf call. It seems that this inlining couses the trouble. define your printf as follows
Code: Select all
extern int printf(const char *fmt, ...)
__attribute__((format(printf, 1, 2)))
__attribute__((noinline));
Fixed this issue for me
Well, this fixed the issue for me too (and thanks to 'format' I get loads of formatting warnings for all my printf calls
)! Thank you very much! I couldn't have found it myself.
Should every function using va_list and using a variable number of arguments use __attribute((noinline))?
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 9:41 am
by albeva
Using gcc standard stdarg.h header fixed this issue.
Code: Select all
/* Copyright (C) 1989, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, if you include this header file into source
files compiled by GCC, this header file does not by itself cause
the resulting executable to be covered by the GNU General Public
License. This exception does not however invalidate any other
reasons why the executable file might be covered by the GNU General
Public License. */
/*
* ISO C Standard: 7.15 Variable arguments <stdarg.h>
*/
#ifndef _STDARG_H
#ifndef _ANSI_STDARG_H_
#ifndef __need___va_list
#define _STDARG_H
#define _ANSI_STDARG_H_
#endif /* not __need___va_list */
#undef __need___va_list
/* Define __gnuc_va_list. */
#ifndef __GNUC_VA_LIST
#define __GNUC_VA_LIST
typedef __builtin_va_list __gnuc_va_list;
#endif
/* Define the standard macros for the user,
if this invocation was from the user program. */
#ifdef _STDARG_H
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L
#define va_copy(d,s) __builtin_va_copy(d,s)
#endif
#define __va_copy(d,s) __builtin_va_copy(d,s)
/* Define va_list, if desired, from __gnuc_va_list. */
/* We deliberately do not define va_list when called from
stdio.h, because ANSI C says that stdio.h is not supposed to define
va_list. stdio.h needs to have access to that data type,
but must not use that name. It should use the name __gnuc_va_list,
which is safe because it is reserved for the implementation. */
#ifdef _HIDDEN_VA_LIST /* On OSF1, this means varargs.h is "half-loaded". */
#undef _VA_LIST
#endif
#ifdef _BSD_VA_LIST
#undef _BSD_VA_LIST
#endif
#if defined(__svr4__) || (defined(_SCO_DS) && !defined(__VA_LIST))
/* SVR4.2 uses _VA_LIST for an internal alias for va_list,
so we must avoid testing it and setting it here.
SVR4 uses _VA_LIST as a flag in stdarg.h, but we should
have no conflict with that. */
#ifndef _VA_LIST_
#define _VA_LIST_
#ifdef __i860__
#ifndef _VA_LIST
#define _VA_LIST va_list
#endif
#endif /* __i860__ */
typedef __gnuc_va_list va_list;
#ifdef _SCO_DS
#define __VA_LIST
#endif
#endif /* _VA_LIST_ */
#else /* not __svr4__ || _SCO_DS */
/* The macro _VA_LIST_ is the same thing used by this file in Ultrix.
But on BSD NET2 we must not test or define or undef it.
(Note that the comments in NET 2's ansi.h
are incorrect for _VA_LIST_--see stdio.h!) */
#if !defined (_VA_LIST_) || defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__) || defined(WINNT)
/* The macro _VA_LIST_DEFINED is used in Windows NT 3.5 */
#ifndef _VA_LIST_DEFINED
/* The macro _VA_LIST is used in SCO Unix 3.2. */
#ifndef _VA_LIST
/* The macro _VA_LIST_T_H is used in the Bull dpx2 */
#ifndef _VA_LIST_T_H
/* The macro __va_list__ is used by BeOS. */
#ifndef __va_list__
typedef __gnuc_va_list va_list;
#endif /* not __va_list__ */
#endif /* not _VA_LIST_T_H */
#endif /* not _VA_LIST */
#endif /* not _VA_LIST_DEFINED */
#if !(defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__))
#define _VA_LIST_
#endif
#ifndef _VA_LIST
#define _VA_LIST
#endif
#ifndef _VA_LIST_DEFINED
#define _VA_LIST_DEFINED
#endif
#ifndef _VA_LIST_T_H
#define _VA_LIST_T_H
#endif
#ifndef __va_list__
#define __va_list__
#endif
#endif /* not _VA_LIST_, except on certain systems */
#endif /* not __svr4__ */
#endif /* _STDARG_H */
#endif /* not _ANSI_STDARG_H_ */
#endif /* not _STDARG_H */
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 10:51 am
by cr2
albeva wrote:Using gcc standard stdarg.h header fixed this issue.
Albeva, your OS is now officially under the GNU GPL.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 10:57 am
by albeva
I am not sure how GPL applies to heade files that are part of the compiler and distributed with it. Because otherwise ANYTHING compiled with gcc would automatically be under gpl.
I guess you shouln't distribute this header file and use it only from compile's default location.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 11:12 am
by cr2
I guess any work compiled under GCC is GNU GPL (if you use the included library).
But, I might be wrong here
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 11:20 am
by Combuster
If you actually read the license it says something llike the following.
"This file is GPLed. However, you may use a compiled form of this file without GPLing the resulting binary"
In legalese:
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
(snip)
As a special exception, if you include this header file into source
files compiled by GCC, this header file does not by itself cause
the resulting executable to be covered by the GNU General Public
License. This exception does not however invalidate any other
reasons why the executable file might be covered by the GNU General
Public License.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 11:32 am
by albeva
lol. it just proves how few of us *actually* read licences
there was an experiment some time ago with a licence. where buried in a legal text was a note that anyone reading this should contact to a certain address and receive $$$ (no idea how much it was) Anyway the point was that it took a long long time (like half a year) before someone finally contacted to claim the cash.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 11:39 am
by cr2
Ok, I was wrong there. But still, I have doubts about including this file in your OS.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 11:49 am
by 01000101
I included this question in an email that I sent over to the guys at both the GCC website and the FSF website. I had to write them a letter anyways asking about some other (un-related) questions. Hopefully they will clear it up.
I read through the GPL again (as I've had to go through it plenty of times), and it states that software that is not 'dependant' on the GPL'd code should not have to be redistributed under the GPL, but once it is deemed necessary or reliant on that GPL'd code, it must take on the licence.
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 12:53 pm
by Virtlink
I still wonder. When I don't want to use the GPL'ed GCC header files, should every function using va_list and using a variable number of arguments use __attribute((noinline))? Or is some construction with the GCC specific va-list implementation like '__gnuc_va_list' better (as suggested in the above header file's comment)?
Re: Problem with test() and printf(const char *format, ...)
Posted: Sun Aug 24, 2008 12:57 pm
by Combuster
and it states that software that is not 'dependant' on the GPL'd code should not have to be redistributed under the GPL, but once it is deemed necessary or reliant on that GPL'd code, it must take on the licence.
Which, in this case, you could arguably substitute the varargs header with a non-GPLed version. Hence its not dependent on *that* version.
But if they really wanted a loophole to make all GCC-code copyrighted it would've been widely known by now...
I still wonder. When I don't want to use the GPL'ed GCC header files, should every function using va_list and using a variable number of arguments use __attribute((noinline))? Or is some construction with the GCC specific va-list implementation like '__gnuc_va_list' better (as suggested in the above header file's comment)?
Try it out
I would expect that you don't need those constructs by using those macros, since they are written for the compiler and hence should comply with the C standard and work as expected (independent of being inlined or not)