libprofile: allocateOneNode() can overflow CurrentVNode
| Bugzilla Link | 27882 |
| Resolution | FIXED |
| Resolved on | May 25, 2016 16:39 |
| Version | unspecified |
| OS | All |
| CC | @vns-mn,@hfinkel,@silvasean,@vedantk |
Extended Description
In allocateOneNode(), we increment CurrentVNode without first checking if it's greater than EndVnode. This could eventually cause CurrentVNode to overflow and return a bad vnode:
106 Node = COMPILER_RT_PTR_FETCH_ADD(ValueProfNode, CurrentVNode, 1);
107 if (Node >= EndVNode) {
108 if (OutOfNodesWarnings++ < MAX_VP_WARNS) {
109 PROF_WARN("Unable to track new values: %s. "
110 " Consider using option -mllvm -vp-counters-per-site=
112 "Running out of static counters");
113 }
114 return 0;
115 }
116 return Node;
Sean suggested limiting increments of CurrentVNode to #threads past EndVNode.
Another option is to attempt to CmpExchange CurrentVnode to its next value while (CurrentVNode + 1 < EndVNode). We'd return if the CmpExchange succeeds. If the loop exits we'd return NULL.
My latest change with early checking should make the problem go away -- redundant update (once) can still happen due to racing, but that should not be an issue.
Sorry, David addressed this with r270766 already.