int main () { int never_allocated; int *free_twice = malloc (sizeof (int)); int *use_after_free = malloc (sizeof (int)); int *never_free = malloc (sizeof (int)); int array1[100]; int *array2 = malloc (100 * sizeof (int)); // valgrind does not bounds-check static arrays read_element(array1, -1); read_element(array1, 100); // but it does bounds-check dynamic arrays read_element(array2, -1); read_element(array2, 100); // and it catches use after free free(use_after_free); *use_after_free = 17; // never_free is definitely lost *never_free = 17; // the following line would generate a warning // free(&never_allocated); // but this one doesn't // free_anything(&never_allocated); -- ERROR free(free_twice); // free(free_twice); -- ERROR return 0; }
bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx) { smx_synchro_t remote_act = nullptr; switch (req->call) { case SIMCALL_COMM_WAIT: /* FIXME: check also that src and dst processes are not suspended */ remote_act = simcall_comm_wait__get__comm(req); break; case SIMCALL_COMM_WAITANY: { read_element( mc_model_checker->process(), &remote_act, remote(simcall_comm_waitany__get__comms(req)), idx, sizeof(remote_act)); } break; case SIMCALL_COMM_TESTANY: { read_element( mc_model_checker->process(), &remote_act, remote(simcall_comm_testany__get__comms(req)), idx, sizeof(remote_act)); } break; default: return true; } s_smx_synchro_t synchro; mc_model_checker->process().read_bytes( &synchro, sizeof(synchro), remote(remote_act)); return synchro.comm.src_proc && synchro.comm.dst_proc; }
/***************************************************************************** * GdipCreateRegionRgnData [GDIPLUS.@] */ GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region) { const struct region_data_header *region_data_header; struct memory_buffer mbuf; GpStatus status; INT count; TRACE("(%p, %d, %p)\n", data, size, region); if (!data || !size) return InvalidParameter; init_memory_buffer(&mbuf, data, size); region_data_header = buffer_read(&mbuf, sizeof(*region_data_header)); if (!region_data_header || !VALID_MAGIC(region_data_header->header.magic)) return InvalidParameter; status = GdipCreateRegion(region); if (status != Ok) return status; count = 0; status = read_element(&mbuf, *region, &(*region)->node, &count); if (status == Ok && !count) status = InvalidParameter; if (status != Ok) { GdipDeleteRegion(*region); *region = NULL; } return status; }
SMevent SMreadPostAsCompactPre::read_event() { if ((have_new + have_triangle + have_finalized) == 0) { return read_element(); } if (have_new) { v_idx = t_idx[new_vertices[next_new]]; VecCopy3fv(v_pos_f, t_pos_f[new_vertices[next_new]]); have_new--; next_new++; v_count++; return SM_VERTEX; } else if (have_triangle) { have_triangle = 0; f_count++; return SM_TRIANGLE; } else if (have_finalized) { final_idx = t_idx[finalized_vertices[next_finalized]]; have_finalized--; next_finalized++; return SM_FINALIZED; } return SM_ERROR; }
GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region) { GpStatus status; struct memory_buffer mbuf; const struct region_header *region_header; INT count; if (!data || !size) return InvalidParameter; TRACE("%p, %d, %p\n", data, size, region); init_memory_buffer(&mbuf, data, size); region_header = buffer_read(&mbuf, sizeof(struct region_header)); if (!region_header || region_header->magic != VERSION_MAGIC) return InvalidParameter; status = GdipCreateRegion(region); if (status != Ok) return status; count = 0; status = read_element(&mbuf, *region, &(*region)->node, &count); if (status == Ok && !count) status = InvalidParameter; if (status != Ok) GdipDeleteRegion(*region); return status; }
static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root) { xmlNodePtr channel = root->children; xmlNodePtr n; int items = 0; DBGP("parsing rss 2.0 or <1 doc"); while (channel && (channel->type != XML_ELEMENT_NODE || strcmp((const char *) channel->name, "channel"))) { channel = channel->next; } if (!channel) { return 0; } for (n = channel->children; n; n = n->next) { if (n->type == XML_ELEMENT_NODE && !strcmp((const char *) n->name, "item")) { ++items; } } if (res->version) free(res->version); res->version = strndup("2.0", text_buffer_size); if (res->items) free_rss_items(res); res->items = malloc(items * sizeof(PRSS_Item)); res->item_count = 0; for (n = channel->children; n; n = n->next) { read_element(res, n); } return 1; }
bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx) { smx_activity_t remote_act = nullptr; switch (req->call) { case SIMCALL_COMM_WAIT: /* FIXME: check also that src and dst processes are not suspended */ remote_act = simcall_comm_wait__get__comm(req); break; case SIMCALL_COMM_WAITANY: { read_element( mc_model_checker->process(), &remote_act, remote(simcall_comm_waitany__get__comms(req)), idx, sizeof(remote_act)); } break; case SIMCALL_COMM_TESTANY: remote_act = mc_model_checker->process().read(remote( simcall_comm_testany__get__comms(req) + idx)); break; default: return true; } simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_comm; mc_model_checker->process().read(temp_comm, remote( static_cast<simgrid::kernel::activity::Comm*>(remote_act))); simgrid::kernel::activity::Comm* comm = temp_comm.getBuffer(); return comm->src_proc && comm->dst_proc; }
SMevent SMreader_off::read_event() { if (have_finalized) { final_idx = finalized_vertices[next_finalized]; have_finalized--; next_finalized++; return SM_FINALIZED; } else { return read_element(); } }
//! \cond dmz::Boolean dmz::NetModulePacketCodecBasic::HeaderElement::read_header ( Unmarshal &data, Handle &packetHandle) { Boolean result (True); if (read_element (data, packetHandle)) { HeaderElement *current (next); while (current) { result = current->read_element (data, packetHandle); if (!result) { current = 0; } else { current = current->next; } } } else { result = False; } return result; }
static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root) { int items = 0; xmlNodePtr n; DBGP("parsing rss 1.0 doc"); for (n = root->children; n; n = n->next) { if (n->type == XML_ELEMENT_NODE) { if (!strcmp((const char *) n->name, "item")) { ++items; } else if (!strcmp((const char *) n->name, "channel")) { xmlNodePtr i; for (i = n->children; i; i = i->next) { read_element(res, i); } } } } if (res->version) free(res->version); res->version = strndup("1.0", text_buffer_size); if (res->items) free_rss_items(res); res->items = malloc(items * sizeof(PRSS_Item)); res->item_count = 0; for (n = root->children; n; n = n->next) { if (n->type == XML_ELEMENT_NODE && !strcmp((const char *) n->name, "item")) { read_item(&res->items[res->item_count++], n->children); } } return 1; }
static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count) { GpStatus status; const DWORD *type; type = buffer_read(mbuf, sizeof(*type)); if (!type) return Ok; TRACE("type %#x\n", *type); node->type = *type; switch (node->type) { case CombineModeReplace: case CombineModeIntersect: case CombineModeUnion: case CombineModeXor: case CombineModeExclude: case CombineModeComplement: { region_element *left, *right; left = heap_alloc_zero(sizeof(region_element)); if (!left) return OutOfMemory; right = heap_alloc_zero(sizeof(region_element)); if (!right) { heap_free(left); return OutOfMemory; } status = read_element(mbuf, region, left, count); if (status == Ok) { status = read_element(mbuf, region, right, count); if (status == Ok) { node->elementdata.combine.left = left; node->elementdata.combine.right = right; region->num_children += 2; return Ok; } } heap_free(left); heap_free(right); return status; } case RegionDataRect: { const GpRectF *rc; rc = buffer_read(mbuf, sizeof(*rc)); if (!rc) { ERR("failed to read rect data\n"); return InvalidParameter; } node->elementdata.rect = *rc; *count += 1; return Ok; } case RegionDataPath: { GpPath *path; const struct path_header *path_header; const BYTE *types; path_header = buffer_read(mbuf, sizeof(*path_header)); if (!path_header) { ERR("failed to read path header\n"); return InvalidParameter; } if (!VALID_MAGIC(path_header->magic)) { ERR("invalid path header magic %#x\n", path_header->magic); return InvalidParameter; } /* Windows always fails to create an empty path in a region */ if (!path_header->count) { TRACE("refusing to create an empty path in a region\n"); return GenericError; } status = GdipCreatePath(FillModeAlternate, &path); if (status) return status; node->elementdata.path = path; if (!lengthen_path(path, path_header->count)) return OutOfMemory; path->pathdata.Count = path_header->count; if (path_header->flags & ~FLAGS_INTPATH) FIXME("unhandled path flags %#x\n", path_header->flags); if (path_header->flags & FLAGS_INTPATH) { const packed_point *pt; DWORD i; pt = buffer_read(mbuf, sizeof(*pt) * path_header->count); if (!pt) { ERR("failed to read packed %u path points\n", path_header->count); return InvalidParameter; } for (i = 0; i < path_header->count; i++) { path->pathdata.Points[i].X = (REAL)pt[i].X; path->pathdata.Points[i].Y = (REAL)pt[i].Y; } } else { const GpPointF *ptf; ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count); if (!ptf) { ERR("failed to read %u path points\n", path_header->count); return InvalidParameter; } memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count); } types = buffer_read(mbuf, path_header->count); if (!types) { ERR("failed to read %u path types\n", path_header->count); return InvalidParameter; } memcpy(path->pathdata.Types, types, path_header->count); if (path_header->count & 3) { if (!buffer_read(mbuf, 4 - (path_header->count & 3))) { ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3)); return InvalidParameter; } } *count += 1; return Ok; } case RegionDataEmptyRect: case RegionDataInfiniteRect: *count += 1; return Ok; default: FIXME("element type %#x is not supported\n", *type); break; } return InvalidParameter; }
VALUE ox_parse(char *xml, ParseCallbacks pcb, char **endp, int trace, Effort effort) { struct _PInfo pi; int body_read = 0; if (0 == xml) { raise_error("Invalid arg, xml string can not be null", xml, 0); } if (DEBUG <= trace) { printf("Parsing xml:\n%s\n", xml); } /* initialize parse info */ pi.str = xml; pi.s = xml; pi.h = 0; pi.pcb = pcb; pi.obj = Qnil; pi.circ_array = 0; pi.encoding = 0; pi.trace = trace; pi.effort = effort; while (1) { next_non_white(&pi); // skip white space if ('\0' == *pi.s) { break; } if (body_read && 0 != endp) { *endp = pi.s; break; } if ('<' != *pi.s) { // all top level entities start with < raise_error("invalid format, expected <", pi.str, pi.s); } pi.s++; // past < switch (*pi.s) { case '?': // prolog pi.s++; read_instruction(&pi); break; case '!': /* comment or doctype */ pi.s++; if ('\0' == *pi.s) { raise_error("invalid format, DOCTYPE or comment not terminated", pi.str, pi.s); } else if ('-' == *pi.s) { pi.s++; // skip - if ('-' != *pi.s) { raise_error("invalid format, bad comment format", pi.str, pi.s); } else { pi.s++; // skip second - read_comment(&pi); } } else if (0 == strncmp("DOCTYPE", pi.s, 7)) { pi.s += 7; read_doctype(&pi); } else { raise_error("invalid format, DOCTYPE or comment expected", pi.str, pi.s); } break; case '\0': raise_error("invalid format, document not terminated", pi.str, pi.s); default: read_element(&pi); body_read = 1; break; } } return pi.obj; }
/* Entered after the '<' and the first character after that. Returns status * code. */ static void read_element(PInfo pi) { struct _Attr attrs[MAX_ATTRS]; Attr ap = attrs; char *name; char *ename; char *end; char c; long elen; int hasChildren = 0; int done = 0; ename = read_name_token(pi); end = pi->s; elen = end - ename; next_non_white(pi); c = *pi->s; *end = '\0'; if ('/' == c) { /* empty element, no attributes and no children */ pi->s++; if ('>' != *pi->s) { //printf("*** '%s' ***\n", pi->s); raise_error("invalid format, element not closed", pi->str, pi->s); } pi->s++; /* past > */ ap->name = 0; pi->pcb->add_element(pi, ename, attrs, hasChildren); pi->pcb->end_element(pi, ename); return; } /* read attribute names until the close (/ or >) is reached */ while (!done) { if ('\0' == c) { next_non_white(pi); c = *pi->s; } switch (c) { case '\0': raise_error("invalid format, document not terminated", pi->str, pi->s); case '/': // Element with just attributes. pi->s++; if ('>' != *pi->s) { raise_error("invalid format, element not closed", pi->str, pi->s); } pi->s++; ap->name = 0; pi->pcb->add_element(pi, ename, attrs, hasChildren); pi->pcb->end_element(pi, ename); return; case '>': // has either children or a value pi->s++; hasChildren = 1; done = 1; ap->name = 0; pi->pcb->add_element(pi, ename, attrs, hasChildren); break; default: // Attribute name so it's an element and the attribute will be // added to it. ap->name = read_name_token(pi); end = pi->s; next_non_white(pi); if ('=' != *pi->s++) { raise_error("invalid format, no attribute value", pi->str, pi->s); } *end = '\0'; // terminate name // read value next_non_white(pi); ap->value = read_quoted_value(pi); if (0 != strchr(ap->value, '&')) { if (0 != collapse_special((char*)ap->value)) { raise_error("invalid format, special character does not end with a semicolon", pi->str, pi->s); } } ap++; if (MAX_ATTRS <= (ap - attrs)) { raise_error("too many attributes", pi->str, pi->s); } break; } c = '\0'; } if (hasChildren) { char *start; done = 0; // read children while (!done) { start = pi->s; next_non_white(pi); c = *pi->s++; if ('\0' == c) { raise_error("invalid format, document not terminated", pi->str, pi->s); } if ('<' == c) { switch (*pi->s) { case '!': /* better be a comment or CDATA */ pi->s++; if ('-' == *pi->s && '-' == *(pi->s + 1)) { pi->s += 2; read_comment(pi); } else if (0 == strncmp("[CDATA[", pi->s, 7)) { pi->s += 7; read_cdata(pi); } else { raise_error("invalid format, invalid comment or CDATA format", pi->str, pi->s); } break; case '/': pi->s++; name = read_name_token(pi); end = pi->s; next_non_white(pi); c = *pi->s; *end = '\0'; if (0 != strcmp(name, ename)) { raise_error("invalid format, elements overlap", pi->str, pi->s); } if ('>' != c) { raise_error("invalid format, element not closed", pi->str, pi->s); } pi->s++; pi->pcb->end_element(pi, ename); return; case '\0': raise_error("invalid format, document not terminated", pi->str, pi->s); default: // a child element read_element(pi); break; } } else { // read as TEXT pi->s = start; //pi->s--; read_text(pi); //read_reduced_text(pi); // to exit read_text with no errors the next character must be < if ('/' == *(pi->s + 1) && 0 == strncmp(ename, pi->s + 2, elen) && '>' == *(pi->s + elen + 2)) { // close tag after text so treat as a value pi->s += elen + 3; pi->pcb->end_element(pi, ename); return; } } } } }
uint64_t force_ldm_stalls(chain_t **C, int element_size, int access_size, int mem_refs, // number of pointers/elements to chase uint64_t max_nelems, // max number of available elements/pointers int it_n, // seed to calculate the first pointer to chase, used to avoid repeating // pointers during consecutive calls unsigned long *time_diff_ns) { uint64_t j, i; int nchains = SEED_IN; uint64_t sumv[MAX_NUM_CHAINS]; uint64_t nextp[MAX_NUM_CHAINS]; char *buf; uint64_t buf_size = 16384; int count = 0; uint64_t start; uint64_t it_limit; struct timespec time_start, time_end; assert(nchains < MAX_NUM_CHAINS); if (mem_refs <= 0) return 0; buf = (char*) malloc(buf_size); assert(buf != NULL); if (max_nelems > mem_refs) { it_limit = max_nelems / mem_refs; } else { it_limit = 1; } it_n = it_n % it_limit; start = it_n * mem_refs; if ((start + mem_refs) > max_nelems) { start = 0; } /* chase the pointers */ if (nchains == 1) { clock_gettime(CLOCK_MONOTONIC, &time_start); sumv[0] = 0; // chase pointers until the 'mem_refs' count, the pointer chasing will restart from beginning if 'mem_refs' // is greater than 'nelems' for (count = 0, i = start; count < mem_refs; i = element(C[0], i)->val, ++count) { __asm__(""); sumv[0] += element(C[0], i)->val; if (access_size > element_size) { read_element(C[0], i, buf, buf_size); } } clock_gettime(CLOCK_MONOTONIC, &time_end); } // else { // for (j=0; j < nchains; j++) { // sumv[j] = 0; // nextp[j] = 0; // } // for (; 0 != element(C[0], nextp[0])->val; ) { // for (j=0; j < nchains; j++) { // sumv[j] += element(C[j], nextp[j])->val; // if (access_size > element_size) { // read_element(C[j], nextp[j], buf, buf_size); // } // nextp[j] = element(C[j], nextp[j])->val; // } // } // } *time_diff_ns = ((time_end.tv_sec * 1000000000) + time_end.tv_nsec) - ((time_start.tv_sec * 1000000000) + time_start.tv_nsec); free(buf); return sumv[0]; }