From: Julian Seward Date: Sat, 10 Nov 2007 22:50:13 +0000 (+0000) Subject: Followup to r7105: stop gcc complaining about violations of ANSI C X-Git-Tag: svn/VALGRIND_3_3_0~149 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5af7d1d73d160d9e8afd32b11e75ca7e37298ac1;p=thirdparty%2Fvalgrind.git Followup to r7105: stop gcc complaining about violations of ANSI C aliasing rules by casting through (char*) as an intermediate type, rather than through (void*). This seems at least moderately justifiable since the ANSI C standard explicitly says that any object may be accessed through a char pointer. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7141 --- diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c index 0a97571247..18f71e098a 100644 --- a/helgrind/hg_main.c +++ b/helgrind/hg_main.c @@ -60,11 +60,22 @@ /*----------------------------------------------------------------*/ /* Note there are a whole bunch of ugly double casts of the form - (Word*)(void*)&p. These placate gcc at -O2. The obvious form + (Word*)(HChar*)&p. These placate gcc at -O2. The obvious form (Word*)&p causes gcc to complain that 'dereferencing a type-punned pointer ill break strict-aliasing rules'. It stops complaining - when the intermediate void* type is inserted. Is this a reasonable - "fix"? I don't know. */ + when the intermediate HChar* type is inserted. + + HChar is the same as plain 'char' (see + VEX/pub/libvex_basictypes.h). The ANSI C standard says "An object + shall have its stored value accessed only by an lvalue that has one + of the following types: [..] A character type." + + (http://gcc.gnu.org/ml/gcc/1999-09n/msg00419.html) + + Hence it would appear that casting via an intermediate char* type + is a standards-compliant (== future-proof) way to circumvent the + aliasing rules in places where it is convenient to do so. +*/ // FIXME what is supposed to happen to locks in memory which // is relocated as a result of client realloc? @@ -630,7 +641,7 @@ static Bool is_sane_Bag_of_Threads ( WordBag* bag ) Thread* thr; Word count; HG_(initIterBag)( bag ); - while (HG_(nextIterBag)( bag, (Word*)(void*)&thr, &count )) { + while (HG_(nextIterBag)( bag, (Word*)(HChar*)&thr, &count )) { if (count < 1) return False; if (!is_sane_Thread(thr)) return False; } @@ -825,7 +836,7 @@ static void remove_Lock_from_locksets_of_all_owning_Threads( Lock* lk ) } /* for each thread that holds this lock do ... */ HG_(initIterBag)( lk->heldBy ); - while (HG_(nextIterBag)( lk->heldBy, (Word*)(void*)&thr, NULL )) { + while (HG_(nextIterBag)( lk->heldBy, (Word*)(HChar*)&thr, NULL )) { tl_assert(is_sane_Thread(thr)); tl_assert(HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lk )); @@ -1107,7 +1118,7 @@ static void pp_Lock ( Int d, Lock* lk ) Word count; VG_(printf)(" { "); HG_(initIterBag)( lk->heldBy ); - while (HG_(nextIterBag)( lk->heldBy, (Word*)(void*)&thr, &count )) + while (HG_(nextIterBag)( lk->heldBy, (Word*)(HChar*)&thr, &count )) VG_(printf)("%lu:%p ", count, thr); HG_(doneIterBag)( lk->heldBy ); VG_(printf)("}"); @@ -1141,8 +1152,8 @@ static void pp_map_locks ( Int d ) space(d); VG_(printf)("map_locks (%d entries) {\n", (Int)HG_(sizeFM)( map_locks )); HG_(initIterFM)( map_locks ); - while (HG_(nextIterFM)( map_locks, (Word*)(void*)&gla, - (Word*)(void*)&lk )) { + while (HG_(nextIterFM)( map_locks, (Word*)(HChar*)&gla, + (Word*)(HChar*)&lk )) { space(d+3); VG_(printf)("guest %p -> Lock %p\n", gla, lk); } @@ -1189,8 +1200,8 @@ static void pp_map_segments ( Int d ) space(d); VG_(printf)("map_segments (%d entries) {\n", (Int)HG_(sizeFM)( map_segments )); HG_(initIterFM)( map_segments ); - while (HG_(nextIterFM)( map_segments, (Word*)(void*)&segid, - (Word*)(void*)&seg )) { + while (HG_(nextIterFM)( map_segments, (Word*)(HChar*)&segid, + (Word*)(HChar*)&seg )) { space(d+3); VG_(printf)("segid 0x%x -> Segment %p\n", (UInt)segid, seg); } @@ -1309,8 +1320,8 @@ static void pp_map_shmem_shared ( Int d ) SecMap* sm; space(d); VG_(printf)("map_shmem_ShR_and_ShM_only {\n"); HG_(initIterFM)( map_shmem ); - while (HG_(nextIterFM)( map_shmem, (Word*)(void*)&ga, - (Word*)(void*)&sm )) { + while (HG_(nextIterFM)( map_shmem, (Word*)(HChar*)&ga, + (Word*)(HChar*)&sm )) { pp_SecMap_shared( d+3, sm, ga ); } HG_(doneIterFM) ( map_shmem ); @@ -1522,7 +1533,7 @@ Lock* map_locks_lookup_or_create ( LockKind lkk, Addr ga, ThreadId tid ) Lock* oldlock = NULL; tl_assert(is_sane_ThreadId(tid)); found = HG_(lookupFM)( map_locks, - NULL, (Word*)(void*)&oldlock, (Word)ga ); + NULL, (Word*)(HChar*)&oldlock, (Word)ga ); if (!found) { Lock* lock = mk_LockN(lkk, ga); lock->appeared_at = VG_(record_ExeContext)( tid, 0 ); @@ -1546,7 +1557,7 @@ static Lock* map_locks_maybe_lookup ( Addr ga ) { Bool found; Lock* lk = NULL; - found = HG_(lookupFM)( map_locks, NULL, (Word*)(void*)&lk, (Word)ga ); + found = HG_(lookupFM)( map_locks, NULL, (Word*)(HChar*)&lk, (Word)ga ); tl_assert(found ? lk != NULL : lk == NULL); if (found) { // check the relevant secondary map has .mbHasLocks? @@ -1560,7 +1571,7 @@ static void map_locks_delete ( Addr ga ) Addr ga2 = 0; Lock* lk = NULL; HG_(delFromFM)( map_locks, - (Word*)(void*)&ga2, (Word*)(void*)&lk, (Word)ga ); + (Word*)(HChar*)&ga2, (Word*)(HChar*)&lk, (Word)ga ); /* delFromFM produces the val which is being deleted, if it is found. So assert it is non-null; that in effect asserts that we are deleting a (ga, Lock) pair which actually exists. */ @@ -1584,7 +1595,7 @@ static Segment* map_segments_lookup ( SegmentID segid ) Segment* seg = NULL; tl_assert( is_sane_SegmentID(segid) ); found = HG_(lookupFM)( map_segments, - NULL, (Word*)(void*)&seg, (Word)segid ); + NULL, (Word*)(HChar*)&seg, (Word)segid ); tl_assert(found); tl_assert(seg != NULL); return seg; @@ -1596,7 +1607,7 @@ static Segment* map_segments_maybe_lookup ( SegmentID segid ) Segment* seg = NULL; tl_assert( is_sane_SegmentID(segid) ); found = HG_(lookupFM)( map_segments, - NULL, (Word*)(void*)&seg, (Word)segid ); + NULL, (Word*)(HChar*)&seg, (Word)segid ); if (!found) tl_assert(seg == NULL); return seg; } @@ -2346,7 +2357,7 @@ static SecMap* shmem__find_or_alloc_SecMap ( Addr ga ) SecMap* sm = NULL; Addr gaKey = shmem__round_to_SecMap_base(ga); if (HG_(lookupFM)( map_shmem, - NULL/*keyP*/, (Word*)(void*)&sm, (Word)gaKey )) { + NULL/*keyP*/, (Word*)(HChar*)&sm, (Word)gaKey )) { /* Found; address of SecMap is in sm */ tl_assert(sm); } else { @@ -2368,7 +2379,7 @@ static Bool shmem__get_mbHasLocks ( Addr a ) SecMap* sm; Addr aKey = shmem__round_to_SecMap_base(a); if (HG_(lookupFM)( map_shmem, - NULL/*keyP*/, (Word*)(void*)&sm, (Word)aKey )) { + NULL/*keyP*/, (Word*)(HChar*)&sm, (Word)aKey )) { /* Found */ return sm->mbHasLocks; } else { @@ -2382,7 +2393,7 @@ static void shmem__set_mbHasLocks ( Addr a, Bool b ) Addr aKey = shmem__round_to_SecMap_base(a); tl_assert(b == False || b == True); if (HG_(lookupFM)( map_shmem, - NULL/*keyP*/, (Word*)(void*)&sm, (Word)aKey )) { + NULL/*keyP*/, (Word*)(HChar*)&sm, (Word)aKey )) { /* Found; address of SecMap is in sm */ } else { /* create a new one */ @@ -2399,7 +2410,7 @@ static void shmem__set_mbHasShared ( Addr a, Bool b ) Addr aKey = shmem__round_to_SecMap_base(a); tl_assert(b == False || b == True); if (HG_(lookupFM)( map_shmem, - NULL/*keyP*/, (Word*)(void*)&sm, (Word)aKey )) { + NULL/*keyP*/, (Word*)(HChar*)&sm, (Word)aKey )) { /* Found; address of SecMap is in sm */ } else { /* create a new one */ @@ -2559,7 +2570,7 @@ static void locks__sanity_check ( Char* who ) // gla == lk->guest_addr HG_(initIterFM)( map_locks ); while (HG_(nextIterFM)( map_locks, - (Word*)(void*)&gla, (Word*)(void*)&lk )) { + (Word*)(HChar*)&gla, (Word*)(HChar*)&lk )) { if (lk->guestaddr != gla) BAD("2"); } HG_(doneIterFM)( map_locks ); @@ -2582,7 +2593,7 @@ static void locks__sanity_check ( Char* who ) Word count; HG_(initIterBag)( lk->heldBy ); while (HG_(nextIterBag)( lk->heldBy, - (Word*)(void*)&thr, &count )) { + (Word*)(HChar*)&thr, &count )) { // is_sane_LockN above ensures these tl_assert(count >= 1); tl_assert(is_sane_Thread(thr)); @@ -2673,7 +2684,7 @@ static void shmem__sanity_check ( Char* who ) HG_(initIterFM)( map_shmem ); // for sm in SecMaps { while (HG_(nextIterFM)( map_shmem, - (Word*)(void*)&smga, (Word*)(void*)&sm )) { + (Word*)(HChar*)&smga, (Word*)(HChar*)&sm )) { SecMapIter itr; UInt* w32p; Bool mbHasShared = False; @@ -2944,7 +2955,7 @@ static ExeContext* maybe_get_lastlock_initpoint ( Addr ga ) ExeContext* ec_hint = NULL; if (ga_to_lastlock != NULL && HG_(lookupFM)(ga_to_lastlock, - NULL, (Word*)(void*)&ec_hint, ga)) { + NULL, (Word*)(HChar*)&ec_hint, ga)) { tl_assert(ec_hint != NULL); return ec_hint; } else { @@ -4942,7 +4953,7 @@ static void shadow_mem_make_NoAccess ( Thread* thr, Addr aIN, SizeT len ) /* FIXME: don't iterate over the complete lock set */ HG_(initIterFM)( map_locks ); while (HG_(nextIterFM)( map_locks, - (Word*)(void*)&gla, (Word*)(void*)&lk )) { + (Word*)(HChar*)&gla, (Word*)(HChar*)&lk )) { tl_assert(is_sane_LockN(lk)); if (gla < firstA || gla > lastA) continue; @@ -4991,7 +5002,7 @@ static void shadow_mem_make_NoAccess ( Thread* thr, Addr aIN, SizeT len ) HG_(initIterFM)( map_shmem ); while (HG_(nextIterFM)( map_shmem, - (Word*)(void*)&ga, (Word*)(void*)&sm )) { + (Word*)(HChar*)&ga, (Word*)(HChar*)&sm )) { tl_assert(sm); stats_SMs++; /* Skip this SecMap if the summary bit indicates it is safe to @@ -5655,7 +5666,7 @@ void evh__HG_PTHREAD_JOIN_POST ( ThreadId stay_tid, Thread* quit_thr ) stats_SMs = stats_SMs_scanned = stats_reExcls = 0; HG_(initIterFM)( map_shmem ); while (HG_(nextIterFM)( map_shmem, - (Word*)(void*)&ga, (Word*)(void*)&sm )) { + (Word*)(HChar*)&ga, (Word*)(HChar*)&sm )) { SecMapIter itr; UInt* w32p; tl_assert(sm); @@ -6150,7 +6161,7 @@ static void evh__HG_PTHREAD_COND_WAIT_POST ( ThreadId tid, edge back to it. */ signalling_seg = NULL; found = HG_(lookupFM)( map_cond_to_Segment, - NULL, (Word*)(void*)&signalling_seg, + NULL, (Word*)(HChar*)&signalling_seg, (Word)cond ); if (found) { tl_assert(is_sane_Segment(signalling_seg)); @@ -6346,7 +6357,7 @@ static void push_Segment_for_sem ( void* sem, Segment* seg ) { tl_assert(seg); map_sem_to_Segment_stack_INIT(); if (HG_(lookupFM)( map_sem_to_Segment_stack, - NULL, (Word*)(void*)&xa, (Word)sem )) { + NULL, (Word*)(HChar*)&xa, (Word)sem )) { tl_assert(xa); VG_(addToXA)( xa, &seg ); } else { @@ -6361,7 +6372,7 @@ static Segment* mb_pop_Segment_for_sem ( void* sem ) { Segment* seg; map_sem_to_Segment_stack_INIT(); if (HG_(lookupFM)( map_sem_to_Segment_stack, - NULL, (Word*)(void*)&xa, (Word)sem )) { + NULL, (Word*)(HChar*)&xa, (Word)sem )) { /* xa is the stack for this semaphore. */ Word sz = VG_(sizeXA)( xa ); tl_assert(sz >= 0); @@ -6569,8 +6580,8 @@ static void laog__show ( Char* who ) { HG_(initIterFM)( laog ); me = NULL; links = NULL; - while (HG_(nextIterFM)( laog, (Word*)(void*)&me, - (Word*)(void*)&links )) { + while (HG_(nextIterFM)( laog, (Word*)(HChar*)&me, + (Word*)(HChar*)&links )) { tl_assert(me); tl_assert(links); VG_(printf)(" node %p:\n", me); @@ -6607,7 +6618,7 @@ static void laog__add_edge ( Lock* src, Lock* dst ) { /* Update the out edges for src */ keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)src )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)src )) { WordSetID outs_new; tl_assert(links); tl_assert(keyW == (Word)src); @@ -6623,7 +6634,7 @@ static void laog__add_edge ( Lock* src, Lock* dst ) { /* Update the in edges for dst */ keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)dst )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)dst )) { WordSetID inns_new; tl_assert(links); tl_assert(keyW == (Word)dst); @@ -6674,7 +6685,7 @@ static void laog__del_edge ( Lock* src, Lock* dst ) { /* Update the out edges for src */ keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)src )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)src )) { tl_assert(links); tl_assert(keyW == (Word)src); links->outs = HG_(delFromWS)( univ_laog, links->outs, (Word)dst ); @@ -6682,7 +6693,7 @@ static void laog__del_edge ( Lock* src, Lock* dst ) { /* Update the in edges for dst */ keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)dst )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)dst )) { tl_assert(links); tl_assert(keyW == (Word)dst); links->inns = HG_(delFromWS)( univ_laog, links->inns, (Word)src ); @@ -6695,7 +6706,7 @@ static WordSetID /* in univ_laog */ laog__succs ( Lock* lk ) { LAOGLinks* links; keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)lk )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)lk )) { tl_assert(links); tl_assert(keyW == (Word)lk); return links->outs; @@ -6710,7 +6721,7 @@ static WordSetID /* in univ_laog */ laog__preds ( Lock* lk ) { LAOGLinks* links; keyW = 0; links = NULL; - if (HG_(lookupFM)( laog, &keyW, (Word*)(void*)&links, (Word)lk )) { + if (HG_(lookupFM)( laog, &keyW, (Word*)(HChar*)&links, (Word)lk )) { tl_assert(links); tl_assert(keyW == (Word)lk); return links->inns; @@ -6731,8 +6742,8 @@ static void laog__sanity_check ( Char* who ) { me = NULL; links = NULL; if (0) VG_(printf)("laog sanity check\n"); - while (HG_(nextIterFM)( laog, (Word*)(void*)&me, - (Word*)(void*)&links )) { + while (HG_(nextIterFM)( laog, (Word*)(HChar*)&me, + (Word*)(HChar*)&links )) { tl_assert(me); tl_assert(links); HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->inns ); @@ -6866,7 +6877,7 @@ static void laog__pre_thread_acquires_lock ( key.dst_ec = NULL; found = NULL; if (HG_(lookupFM)( laog_exposition, - (Word*)(void*)&found, NULL, (Word)&key )) { + (Word*)(HChar*)&found, NULL, (Word)&key )) { tl_assert(found != &key); tl_assert(found->src_ga == key.src_ga); tl_assert(found->dst_ga == key.dst_ga); @@ -7504,7 +7515,7 @@ Bool hg_handle_client_request ( ThreadId tid, UWord* args, UWord* ret) (void*)args[1]); map_pthread_t_to_Thread_INIT(); found = HG_(lookupFM)( map_pthread_t_to_Thread, - NULL, (Word*)(void*)&thr_q, (Word)args[1] ); + NULL, (Word*)(HChar*)&thr_q, (Word)args[1] ); /* Can this fail? It would mean that our pthread_join wrapper observed a successful join on args[1] yet that thread never existed (or at least, it never lodged an @@ -7656,7 +7667,7 @@ static HChar* string_table_strdup ( HChar* str ) { tl_assert(string_table); } if (HG_(lookupFM)( string_table, - NULL, (Word*)(void*)©, (Word)str )) { + NULL, (Word*)(HChar*)©, (Word)str )) { tl_assert(copy); if (0) VG_(printf)("string_table_strdup: %p -> %p\n", str, copy ); return copy; @@ -7690,7 +7701,7 @@ static Lock* mk_LockP_from_LockN ( Lock* lkn ) yaWFM = HG_(newFM)( hg_zalloc, hg_free, lock_unique_cmp ); tl_assert(yaWFM); } - if (!HG_(lookupFM)( yaWFM, NULL, (Word*)(void*)&lkp, (Word)lkn)) { + if (!HG_(lookupFM)( yaWFM, NULL, (Word*)(HChar*)&lkp, (Word)lkn)) { lkp = hg_zalloc( sizeof(Lock) ); *lkp = *lkn; lkp->admin = NULL; diff --git a/helgrind/hg_wordset.c b/helgrind/hg_wordset.c index ed9a5b8b21..4324aa80cc 100644 --- a/helgrind/hg_wordset.c +++ b/helgrind/hg_wordset.c @@ -281,7 +281,7 @@ static WordSet add_or_dealloc_WordVec( WordSetU* wsu, WordVec* wv_new ) causes failures on a 64-bit platform. */ tl_assert(wv_new->owner == wsu); have = HG_(lookupFM)( wsu->vec2ix, - (Word*)(void*)&wv_old, (Word*)&ix_old, + (Word*)(HChar*)&wv_old, (Word*)&ix_old, (Word)wv_new ); if (have) { tl_assert(wv_old != wv_new);