Skip to contentSkip to conversation

Eliminating grub_size_t

DISCUSSION Pavel Roskin 5 participants 15 messages Jul 2, 2008 – Jul 3, 2008

lore
Share & tools
lore
#0 lore raw

Hello!

I wonder if we would be better off without grub_size_t. I cannot think of any code that could use it legitimately.

The ordinary size_t is used to represent the result of sizeof, i.e. size of a structure. There is no need for grub to support data structures exceeding 4 gigabytes. If we want to support more memory, that's fine, but that would involve other types that can hold the pointer values, such as long.

size_t has different size on 32-bit and 64-bit systems, but we should strive to make the userspace utilities work like the bootloader, so that possible problems can be detected early and debugged easily.

Besides, we cannot even print size_t in grub_printf(), and I don't think we should.

grub_size_t should be replaced with int or grub_uint32_t. size_t can be used in pure userspace code to call functions that need it.

Signature · 2 lines
Regards,
Pavel Roskin
#1 lore raw
Quoted 10 lines · Pavel Roskin

Hello!

I wonder if we would be better off without grub_size_t. I cannot think of any code that could use it legitimately.

The ordinary size_t is used to represent the result of sizeof, i.e. size of a structure. There is no need for grub to support data structures exceeding 4 gigabytes. If we want to support more memory, that's fine, but that would involve other types that can hold the pointer values, such as long.

I'm not sure if I'm getting you right. Are you suggesting that we "undo" the someinteger->size_t conversion that caused so many headaches in many 32->64 bit ports? Machine-dependant types like size_t and ptrdiff_t are here to help us, not to haunt us. What is the exact problem with size_t in GRUB right now? I agree that grub_size_t is redundant, though.

Quoted 4 lines

size_t has different size on 32-bit and 64-bit systems, but we should strive to make the userspace utilities work like the bootloader, so that possible problems can be detected early and debugged easily.

I didn't understand this. What do you mean with "US working like the bootloader?"

Quoted 3 lines

Besides, we cannot even print size_t in grub_printf(), and I don't think we should.

1. that can be worked out
2. why not?

Quoted 4 lines

grub_size_t should be replaced with int or grub_uint32_t. size_t can be used in pure userspace code to call functions that need it.

#2 lore raw

Quoting Javier Martín <[email protected]>:

Quoted 11 lines · Pavel Roskin
Quoted 10 lines

Hello!

I wonder if we would be better off without grub_size_t. I cannot think of any code that could use it legitimately.

The ordinary size_t is used to represent the result of sizeof, i.e. size of a structure. There is no need for grub to support data structures exceeding 4 gigabytes. If we want to support more memory, that's fine, but that would involve other types that can hold the pointer values, such as long.

Quoted 5 lines

I'm not sure if I'm getting you right. Are you suggesting that we "undo" the someinteger->size_t conversion that caused so many headaches in many 32->64 bit ports? Machine-dependant types like size_t and ptrdiff_t are here to help us, not to haunt us. What is the exact problem with size_t in GRUB right now? I agree that grub_size_t is redundant, though.

No real problem. There are some warnings in fs/reiserfs.c about it. But I think removing or redefining grub_size_t would be the cleanest solution.

Quoted 5 lines
Quoted 3 lines

size_t has different size on 32-bit and 64-bit systems, but we should strive to make the userspace utilities work like the bootloader, so that possible problems can be detected early and debugged easily.

I didn't understand this. What do you mean with "US working like the bootloader?"

I mean that if, say, GRUB fails to read reiserfs, I'd like to be able to reproduce the problem in grub-fstest even if I'm compiling it on x86_64.

Quoted 4 lines
Quoted 2 lines

Besides, we cannot even print size_t in grub_printf(), and I don't think we should.

1. that can be worked out
2. why not?

We want to save space, not to add code just for a good feeling that out printf is a s good as the one in glibc.

Signature · 2 lines
Regards,
Pavel Roskin
#3 lore raw
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

size_t has different size on 32-bit and 64-bit systems, but we should strive to make the userspace utilities work like the bootloader, so that possible problems can be detected early and debugged easily.

I didn't understand this. What do you mean with "US working like the bootloader?"

I mean that if, say, GRUB fails to read reiserfs, I'd like to be able to reproduce the problem in grub-fstest even if I'm compiling it on x86_64.

In this case, so we're producing a 32-bit, pc grub image. To have a similar effect in grub-fstest, we'd need to define grub_size_t to be a 32-bit quantity when compiling that too, am I right? Is there any reason not to just have grub-fstest try to imitate whatever the bootloader image decides it needs? So if some platform requires a 64-bit bootloader and we're running on 32-bits, we may need a 64-bit grub_size_t in both places (well, this is maybe not likely to work entirely, but GCC can generate the operations -- or we could just use 32 bit for grub-fstest then if we think it's the least-nonsensical thing to do in that hypothetical situation).

-Isaac

#4 lore raw

Quoting Isaac Dupree <[email protected]>:

Quoted 12 lines
Quoted 1 line

I mean that if, say, GRUB fails to read reiserfs, I'd like to be able to reproduce the problem in grub-fstest even if I'm compiling it on x86_64.

In this case, so we're producing a 32-bit, pc grub image. To have a similar effect in grub-fstest, we'd need to define grub_size_t to be a 32-bit quantity when compiling that too, am I right? Is there any reason not to just have grub-fstest try to imitate whatever the bootloader image decides it needs? So if some platform requires a 64-bit bootloader and we're running on 32-bits, we may need a 64-bit grub_size_t in both places (well, this is maybe not likely to work entirely, but GCC can generate the operations -- or we could just use 32 bit for grub-fstest then if we think it's the least-nonsensical thing to do in that hypothetical situation).

I don't think we need 64-bit grub_size_t anywhere, even in 64-bit bootloaders.

Signature · 2 lines
Regards,
Pavel Roskin
#5 lore raw
In reply to Pavel Roskin
Quoted 13 lines · Pavel Roskin

Quoting Javier Martín <[email protected]>:

Quoted 11 lines · Pavel Roskin
Quoted 10 lines

Hello!

I wonder if we would be better off without grub_size_t. I cannot think of any code that could use it legitimately.

The ordinary size_t is used to represent the result of sizeof, i.e. size of a structure. There is no need for grub to support data structures exceeding 4 gigabytes. If we want to support more memory, that's fine, but that would involve other types that can hold the pointer values, such as long.

Quoted 7 lines
Quoted 5 lines

I'm not sure if I'm getting you right. Are you suggesting that we "undo" the someinteger->size_t conversion that caused so many headaches in many 32->64 bit ports? Machine-dependant types like size_t and ptrdiff_t are here to help us, not to haunt us. What is the exact problem with size_t in GRUB right now? I agree that grub_size_t is redundant, though.

No real problem. There are some warnings in fs/reiserfs.c about it. But I think removing or redefining grub_size_t would be the cleanest solution.

If reiserfs is using it in wrong place, fix the reiserfs. If you are reading some file system variable, then you should use grub_uintN_t to specify storage size in bits.

size_t is usually used as common index or offset (or size) to some buffer. size_t is returned by sizeof(). It is meant to be optimal size for platform. Eg. on 64bit memory bus it is 64bit and on 32bit memory bus it is 32bit. What grub is doing here is just defining yet another type for the same thing.

Google for size_t if you want to find out more about it.

#6 lore raw
Quoted 3 lines · Vesa Jääskeläinen

If reiserfs is using it in wrong place, fix the reiserfs. If you are reading some file system variable, then you should use grub_uintN_t to specify storage size in bits.

OK, I'll have another look at the code.

Quoted 7 lines

size_t is usually used as common index or offset (or size) to some buffer. size_t is returned by sizeof(). It is meant to be optimal size for platform. Eg. on 64bit memory bus it is 64bit and on 32bit memory bus it is 32bit. What grub is doing here is just defining yet another type for the same thing.

Google for size_t if you want to find out more about it.

I know what it is. I believe int should be as good as size_t for most purposes is we are not working with very large structures or read gigabytes of data from files at once.

Signature · 2 lines
Regards,
Pavel Roskin
#7 lore raw
Quoted 14 lines · Pavel Roskin
Quoted 3 lines · Vesa Jääskeläinen

If reiserfs is using it in wrong place, fix the reiserfs. If you are reading some file system variable, then you should use grub_uintN_t to specify storage size in bits.

OK, I'll have another look at the code.

Quoted 6 lines

size_t is usually used as common index or offset (or size) to some buffer. size_t is returned by sizeof(). It is meant to be optimal size for platform. Eg. on 64bit memory bus it is 64bit and on 32bit memory bus it is 32bit. What grub is doing here is just defining yet another type for the same thing.

Quoted 5 lines
Quoted 1 line

Google for size_t if you want to find out more about it.

I know what it is. I believe int should be as good as size_t for most purposes is we are not working with very large structures or read gigabytes of data from files at once.

Perhaps, but it doesn't hurt either. I think it is a good thing to have a type such that it is clear what kind of variable is used.

--
Marco

#8 lore raw
Quoted 6 lines · Marco Gerards
Quoted 3 lines

I know what it is. I believe int should be as good as size_t for most purposes is we are not working with very large structures or read gigabytes of data from files at once.

Perhaps, but it doesn't hurt either. I think it is a good thing to have a type such that it is clear what kind of variable is used.

I mean, we can have the type, but make it 32-bit on all systems. Anyway, the warnings have been fixed. In some cases, grub_size_t was used for offsets, which is wrong because we want to support large files on 32-bit systems.

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Signature · 2 lines
Regards,
Pavel Roskin
#9 lore raw
Quoted 9 lines · Pavel Roskin
Quoted 5 lines · Marco Gerards
Quoted 3 lines

I know what it is. I believe int should be as good as size_t for most purposes is we are not working with very large structures or read gigabytes of data from files at once.

Perhaps, but it doesn't hurt either. I think it is a good thing to have a type such that it is clear what kind of variable is used.

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Why? I would let it be optimal type for current memory bus width.

#13 lore raw
In reply to Pavel Roskin
Quoted 8 lines · Pavel Roskin
Quoted 6 lines · Marco Gerards
Quoted 3 lines

I know what it is. I believe int should be as good as size_t for most purposes is we are not working with very large structures or read gigabytes of data from files at once.

Perhaps, but it doesn't hurt either. I think it is a good thing to have a type such that it is clear what kind of variable is used.

Quoted 7 lines

I mean, we can have the type, but make it 32-bit on all systems. Anyway, the warnings have been fixed. In some cases, grub_size_t was used for offsets, which is wrong because we want to support large files on 32-bit systems.

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Please don't. I'd rather stick to integers, such change will only slow down GRUB with no gain.

--
Marco

#10 lore raw
In reply to Vesa Jääskeläinen
Quoted 4 lines · Vesa Jääskeläinen
Quoted 2 lines

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Why? I would let it be optimal type for current memory bus width.

int is supposed to be the optimal type, and int is 32-bit on 64-bit platforms we support. As far as I know, the x86_64 machine code actually defaults to 32-bit for all arguments except memory addresses.

Signature · 2 lines
Regards,
Pavel Roskin
#14 lore raw
In reply to Marco Gerards
Quoted 7 lines · Marco Gerards
Quoted 2 lines · Pavel Roskin

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Please don't. I'd rather stick to integers, such change will only slow down GRUB with no gain.

Integers are 32-bit on all platforms we support. Moreover, long is 32-bit on all platforms we support except SPARC, which is basically on life support (I didn't look at the x86_64 EFI changes yet).

Anyway, I'd like to stop this thread until I have specific code. Otherwise, I have to explain what I mean instead of showing it.

Signature · 2 lines
Regards,
Pavel Roskin
#11 lore raw
In reply to Pavel Roskin
Quoted 9 lines · Pavel Roskin
Quoted 3 lines · Vesa Jääskeläinen
Quoted 2 lines

I think I'll try to make grub_size_t 32-bit everywhere and see if it's going to make any difference or help discover some issues.

Why? I would let it be optimal type for current memory bus width.

int is supposed to be the optimal type, and int is 32-bit on 64-bit platforms we support. As far as I know, the x86_64 machine code actually defaults to 32-bit for all arguments except memory addresses.

And size_t is kinda connected to memory addresses. Do you agree :) ?

#12 lore raw
Quoted 1 line · Vesa Jääskeläinen

And size_t is kinda connected to memory addresses. Do you agree :) ?

Yes. However, size_t should hold the maximal structure size, and we can limit it to 4 (or even 2) gigabytes. You can think of it as of the size of a contiguous chunk of memory. Difference between pointers to different chunks doesn't have to fit size_t or ptrdiff_t.

Let's see: size_t is used:

in sizeof - OK to limit
in malloc - OK to limit
in strlen - OK to limit
in memcpy - OK to limit
in file I/O - OK to limit

We should not be limiting file and partition sizes and memory addresses, but it's OK to limit the size of memory that is used at once, including reading from files. That's the whole reason why off_t may be longer than size_t.

Signature · 2 lines
Regards,
Pavel Roskin