Solaris - BUS error with optimize mode


 
Thread Tools Search this Thread
Top Forums Programming Solaris - BUS error with optimize mode
# 8  
Old 05-20-2015
That forces alignment with a #pragma, otherwise, that'd be extremely difficult.

He didn't mention that about his code, but you're right, it could be something to look for.
# 9  
Old 05-20-2015
The code is badly written:

Code:
struct _a
{
	int _a1;
	uint64_t _a2;
}


int f2(uint64_t *obj)
{
        // obj is of type uint64_t. Not an unsigned long long!
        // this could be any value, including NULL! 
        // no wonder a BUS ERROR in certain circumstances  
	printf("%llu", *obj); <--- BUS ERROR
	
	return 0;
}

void f1()
{
	...
        // how is malloc() expected to know what size space to allocate
        // no test for NULL!
	_a* obj_a = malloc();
	
        // a random value will be returned.  Should zeroize structure
        // why the use of %llu. _a2 is a uint64_t  
	printf("%llu", obj_a->_a2); <--- OK
        // you should use an appropriate cast here
	f2(&obj_a->_a2);
}

# 10  
Old 05-21-2015
Quote:
Originally Posted by Corona688
That forces alignment with a #pragma, otherwise, that'd be extremely difficult.

He didn't mention that about his code, but you're right, it could be something to look for.
The point was that even Sun messed up alignment in operating system basic tools such as mkfs on SPARC. SPARC has some very strict alignment restrictions, and the OPs code is one of the archetypes of ways to violate those restrictions: a malloc'd block that's used by a structure that has a small member with no alignment restrictions declared before a larger member that can have much stricter alignment restrictions.

As far as I can tell, GCC on SPARC has no equivalent of the "-xmemalign" argument the Solaris Studio compilers have:

Man Page cc.1

Quote:
-xmemalign=ab
(SPARC) Use the -xmemalign option to control the
assumptions the compiler makes about the alignment of
data. By controlling the code generated for potentially
misaligned memory accesses and by controlling program
behavior in the event of a misaligned access, you can
more easily port your code to SPARC.

Specify the maximum assumed memory alignment and
behavior of misaligned data accesses. There must be a
value for both a (alignment) and b (behavior). a speci-
fies the maximum assumed memory alignment and b speci-
fies the behavior for misaligned memory accesses.
For memory accesses where the alignment is determinable
at compile time, the compiler generates the appropriate
load/store instruction sequence for that alignment of
data.

For memory accesses where the alignment cannot be
determined at compile time, the compiler must assume an
alignment to generate the needed load/store sequence.

For memory accesses where the alignment is determinable
at compile time, the compiler generates the appropriate
load/store instruction sequence for that alignment of
data.

For memory accesses where the alignment cannot be
determined at compile time, the compiler must assume an
alignment to generate the needed load/store sequence.

If actual data alignment at runtime is less than the
specified alignment, the misaligned access attempt (a
memory read or write) generates a trap. The two possi-
ble responses to the trap are as follows:

o The OS converts the trap to a SIGBUS signal. If the
program does not catch the signal, the program aborts.
Even if the program catches the signal, the misaligned
access attempt will not have succeeded.

o The OS handles the trap by interpreting the
misaligned access and returning control to the program
as if the access had succeeded normally.

Values

Accepted values for a are:

1 Assume at most 1 byte alignment.

2 Assume at most 2 byte alignment.

4 Assume at most 4 byte alignment.

8 Assume at most 8 byte alignment.

16 Assume at most 16 byte alignment.

Accepted values for b are:

i Interpret access and continue execution.

s Raise signal SIGBUS.

f For variants of -xarch=v9 only. Raise signal
SIGBUS for alignments less than or equal to
4, otherwise interpret access and continue
execution. For all other -xarch values, the f
flag is equivalent to i.

You must also specify -xmemalign whenever you want to
link to an object file that was compiled with the value
of b set to either i or f. For a complete list of com-
piler options that must be specified at both compile
time and at link time, see the C User's Guide.

Defaults

The default for all SPARC v9 architectures is
-xmemalign=8s. Specifying -xmemalign is equivalent to
specifying -xmemalign=1i.
This User Gave Thanks to achenle For This Post:
# 11  
Old 05-21-2015
Solaris supports memalign() - a malloc variant that allows specification of alignments on a per object basis. We have had to take that approach with some code.
# 12  
Old 05-21-2015
Quote:
Originally Posted by jim mcnamara
Solaris supports memalign() - a malloc variant that allows specification of alignments on a per object basis. We have had to take that approach with some code.
memalign() isn't going to help in this case - malloc() itself has to return "memory suitably aligned for any use". The problem comes from using a structure in such a way that a member that has a strict alignment is offset from the beginning of the structure by an amount that causes the alignment to be wrong. Per the "-xmemalign" documentation from the Solaris Studio "cc" man page:

Quote:
For memory accesses where the alignment cannot be
determined at compile time, the compiler must assume an
alignment to generate the needed load/store sequence.
And that's why the OPs code that accesses the structure via malloc()'d memory causes a bus error.

It's pretty much the archetype of how to get a SIGBUS error on SPARC.
# 13  
Old 05-22-2015
Quote:
Originally Posted by achenle
The problem comes from using a structure in such a way that a member that has a strict alignment is offset from the beginning of the structure by an amount that causes the alignment to be wrong.

It's pretty much the archetype of how to get a SIGBUS error on SPARC.
Code:
struct _a
{
	int _a1;
	uint64_t _a2;
}

Please explain how you come to the conclusion that the 2nd member of the structure require strict alignment. I do not see that requirement.
# 14  
Old 05-22-2015
Quote:
Originally Posted by fpmurphy
Code:
struct _a
{
    int _a1;
    uint64_t _a2;
}

Please explain how you come to the conclusion that the 2nd member of the structure require strict alignment. I do not see that requirement.
There are three ways to get SIGBUS on SPARC:

1. Writing to memory that was created with mmap using the MAP_NORESERVE flag and an initial write to a virtual page requires that page to actually be created, but no swap space is available for a backing store.

2. Hardware error.

3. Misaligned memory access.

This isn't 1 or 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Howto solve this disk error in Solaris in single user mode

Hi all, OS is Solaros 10 Sparc While doing Netbackup upgradation to 7.5 , the server was asked to reboot. But then it came up in single user mode, and after I typed format command it showed some disk error. bash-3.00# format Searching for disks...WARNING:... (2 Replies)
Discussion started by: manalisharmabe
2 Replies

2. Programming

Bus Error: 10...Help please!

Hi all, I am writing a phonebook program to store names and number using a list. Here is the code for the function which allows the user to enter the name and number (where the error occurs). //THIS FUNCTION ADDS A NEW ENTRY TO THE phonebook_list void insert(void){ //variables int... (5 Replies)
Discussion started by: kdejan
5 Replies

3. Programming

Bus error

Hi everyone, I have a GUI project and when I run it and left in idle state for a long time(there is nothing done, just opened GUI, no more actions),I get bus error after trying to do anything with it. I've tried to build it in debug mode and use gdb, but I don't get any error in debug mode.It... (3 Replies)
Discussion started by: sisi
3 Replies

4. HP-UX

Bus Error

I am getting bus error when i include "#!/bin/ksh". If i remove interpreter then script is working. Can anyone explain this and how can i avoid this error? Operating System is HP-UX B.11.23 U 9000/800 1091834454 (2 Replies)
Discussion started by: anbu23
2 Replies

5. UNIX for Dummies Questions & Answers

bus error (coredump)

Hi all, I am getting bus error problem in SunOS. Can you please help me out in this regard. Actually, my entire code till the last line has been executed. But after tht i am getting a bus error. Please help me. Thanks in advance. Charu. (4 Replies)
Discussion started by: charu
4 Replies

6. Solaris

split bus mode

Hi there, I have two SunBlade 2000s that I want to connect to a single D1000. I am told that I need to do a split bus mode. I don't really understand what that means. Does that mean that half of the storage disks will be assigned to one host and the others to the other host? How do I get... (18 Replies)
Discussion started by: Arkayev
18 Replies

7. Programming

BUS error

Hi! I've got a program which runs fine under Linux, but I have compiled it to run under SunOS 5.8 in a Sparc computer, and now it sometimes fails with "bus error". Ussing gdb I surfed to the error line, which is *pointer = some_vector; where some_vector is a 16 byte struct (4 integers)... (1 Reply)
Discussion started by: shesatmine
1 Replies

8. UNIX for Dummies Questions & Answers

bus error on solaris

Hi there I am running soalris 9 on a sun fire 480r and all of a sudden (today) whenever the users run the command `top` we get the following message `bus error` does anybody have any information on what this is all about and whether there is a routine i can perform to gather more... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

9. UNIX for Dummies Questions & Answers

Bus Error

This may belong in the C Programming forum, but here goes anyway... What would cause a bus error? I searched google for a cause, but came up with some conflicting reports... Could it be caused by disk space? A lot of the pages I found mentioned linking with the incorrect versions of the... (4 Replies)
Discussion started by: LivinFree
4 Replies
Login or Register to Ask a Question